<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>line clipping | Student Projects</title>
	<atom:link href="https://studentprojects.in/tag/line-clipping/feed/" rel="self" type="application/rss+xml" />
	<link>https://studentprojects.in</link>
	<description>Microcontroller projects, Circuit Diagrams, Project Ideas</description>
	<lastBuildDate>Fri, 02 Oct 2009 10:19:36 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.7</generator>
	<item>
		<title>C Program to implement the Cohen-Sutherland line-clipping algorithm.</title>
		<link>https://studentprojects.in/software-development/c-tutorials/c/graphics/c-program-to-implement-the-cohen-sutherland-line-clipping-algorithm/</link>
					<comments>https://studentprojects.in/software-development/c-tutorials/c/graphics/c-program-to-implement-the-cohen-sutherland-line-clipping-algorithm/#comments</comments>
		
		<dc:creator><![CDATA[Editorial Team]]></dc:creator>
		<pubDate>Fri, 02 Oct 2009 10:19:36 +0000</pubDate>
				<category><![CDATA[Graphics]]></category>
		<category><![CDATA[c graphics]]></category>
		<category><![CDATA[rectangle functions]]></category>
		<category><![CDATA[line functions]]></category>
		<category><![CDATA[clipping]]></category>
		<category><![CDATA[line clipping]]></category>
		<guid isPermaLink="false">http://studentprojects.in/?p=832</guid>

					<description><![CDATA[<p>C Program to implement the Cohen-Sutherland line-clipping algorithm. Make provision to specify the input line, window for clipping and view port for displaying the clipped image.  (Use built-in line and rectangle functions).</p>
<p>The post <a href="https://studentprojects.in/software-development/c-tutorials/c/graphics/c-program-to-implement-the-cohen-sutherland-line-clipping-algorithm/">C Program to implement the Cohen-Sutherland line-clipping algorithm.</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>C Program to implement the Cohen-Sutherland line-clipping algorithm. Make provision to specify the input line, window for clipping and view port for displaying the clipped image.  (Use built-in line and rectangle functions).</p>
<pre lang="c" escaped="true" line="1">
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#define MAX 20

enum { TOP = 0x1, BOTTOM = 0x2, RIGHT = 0x4, LEFT = 0x8 };

enum { FALSE, TRUE };
typedef unsigned int outcode;

outcode compute_outcode(int x, int y,
		int xmin, int ymin, int xmax, int ymax)
{
    outcode oc = 0;

    if (y > ymax)
	oc |= TOP;
    else if (y < ymin)
	oc |= BOTTOM;


    if (x > xmax)
	oc |= RIGHT;
    else if (x < xmin)
	oc |= LEFT;

    return oc;
}

void cohen_sutherland (double x1, double y1, double x2, double y2,
		double xmin, double ymin, double xmax, double ymax)
{
    int accept;
    int done;
    outcode outcode1, outcode2;

    accept = FALSE;
    done = FALSE;

    outcode1 = compute_outcode (x1, y1, xmin, ymin, xmax, ymax);
    outcode2 = compute_outcode (x2, y2, xmin, ymin, xmax, ymax);
    do
    {
	if (outcode1 == 0 &#038;&#038; outcode2 == 0)
	{
	    accept = TRUE;
	    done = TRUE;
	}
	else if (outcode1 &#038; outcode2)
	{
	    done = TRUE;
	}
	else
	{
	    double x, y;
	    int outcode_ex = outcode1 ? outcode1 : outcode2;
	    if (outcode_ex &#038; TOP)
	    {
		x = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1);
		y = ymax;
	    }

	    else if (outcode_ex &#038; BOTTOM)
	    {
		x = x1 + (x2 - x1) * (ymin - y1) / (y2 - y1);
		y = ymin;
	    }
	    else if (outcode_ex &#038; RIGHT)
	    {
		y = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1);
		x = xmax;
	    }
	    else
	    {
		y = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1);
		x = xmin;
	    }
	    if (outcode_ex == outcode1)
	    {
		x1 = x;
		y1 = y;
		outcode1 = compute_outcode (x1, y1, xmin, ymin, xmax, ymax);
	    }
	    else
	    {
		x2 = x;
		y2 = y;
		outcode2 = compute_outcode (x2, y2, xmin, ymin, xmax, ymax);
	    }
	}
    } while (done == FALSE);

    if (accept == TRUE)
	line (x1, y1, x2, y2);
}



void main()
{
    int n;
    int i, j;
    int ln[MAX][4];
    int clip[4];
    int gd = DETECT, gm;

    printf ("Enter the number of lines to be clipped");
    scanf ("%d", &#038;n);

    printf ("Enter the x- and y-coordinates of the line-endpoints:\n");
    for (i=0; i<n; i++)
	for (j=0; j<4; j++)
	    scanf ("%d", &#038;ln[i][j]);

    printf ("Enter the x- and y-coordinates of the left-top and right-");
    printf ("bottom corners\nof the clip window:\n");
    for (i=0; i<4; i++)
	scanf ("%d", &#038;clip[i]);

    initgraph (&#038;gd, &#038;gm, "..//bgi");

    rectangle (clip[0], clip[1], clip[2], clip[3]);
    for (i=0; i<n; i++)
	line (ln[i][0], ln[i][1], ln[i][2], ln[i][3]);
    getch();
    cleardevice();
    rectangle (clip[0], clip[1], clip[2], clip[3]);
    for (i=0; i<n; i++)
    {
	cohen_sutherland (ln[i][0], ln[i][1], ln[i][2], ln[i][3],
	    clip[0], clip[1], clip[2], clip[3]);
	getch();
    }
    closegraph();
}
</pre><p>The post <a href="https://studentprojects.in/software-development/c-tutorials/c/graphics/c-program-to-implement-the-cohen-sutherland-line-clipping-algorithm/">C Program to implement the Cohen-Sutherland line-clipping algorithm.</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://studentprojects.in/software-development/c-tutorials/c/graphics/c-program-to-implement-the-cohen-sutherland-line-clipping-algorithm/feed/</wfw:commentRss>
			<slash:comments>18</slash:comments>
		
		
			</item>
	</channel>
</rss>
