Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

CG4

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 24

EXPERIMENT NO.

: 4
Aim: To draw an ellipse using Bresenhams
Algorithm
#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int h,k;
float a,b;

void printhead()
{
char sap[10] = {'5','0','0','0','4','1','1','9','8'};

glRasterPos2f(-80.0,80.0);
for (int i=0;i<9;i++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10,sap[i]);

glRasterPos2f(-80.0,90.0);
for (int i=0;i<6;i++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10,name[i]);

void printname(float x, float y, char str[], int s)


{
glRasterPos2f(x,y);
for (int i=0;i<s;i++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10,str[i]);
}
void display(void)
{
double I,J;
int i,j;
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 0.0, 0.0);
glBegin(GL_POINTS);
glVertex2s(h,k);
for( i=0 ; i<=a ; i+=1)
{
J=sqrt(1 - (i*i)/(a*a))*b;
j=(int)(J);
glVertex2s(h+i,k+j);
glVertex2s(h-i,k+j);
glVertex2s(h-i,k-j);
glVertex2s(h+i,k-j);
}
glColor3f (1.0, 1.0, 1.0);
for(int i=-100 ; i<=100 ; i++)
{
glVertex2s(i,0);
glVertex2s(0,i);
}
for(int i=-2; i<=2 ; i++)
{
glVertex2s(95+i,4+i);
glVertex2s(95-i,4+i);
}
for(int i=0; i<=2 ; i++)
{
glVertex2s(4+i,95+i);
glVertex2s(4-i,95+i);
glVertex2s(4,95-i);
}
glEnd();
char name[7]={'E','L','L','I','P','S','E'};
printname(10.0,20.0,name,7);
printhead();
glFlush();
}
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glOrtho(-100.0, 100.0, -100.0, 100.0, -1.0, 1.0);
}
int main(int argc, char** argv)
{
printf("Enter the center of ellipse:\n");
scanf("%d %d",&h,&k);
printf("Enter the parameters a & b:\n");
scanf("%f %f",&a,&b);
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("500041198 : Ellipse ");
init ();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

EXPERIMENT-7
Clipping (Line, Polygon)

2D Transformation:

#include<stdio.h>
#include<graphics.h>
#include<math.h>
# define ROUND(a) ((int)(a+0.5))
# define absx(x) x+320
# define absy(y) 240-y
void DrawCordinates()
{
line(320,0,320,640);
line(0,240,640,240);
}
void main()
{
int gd=DETECT,gm;
int ang,sx,sy,c,k=0,x1,y1,sh,shx,shy,c1;
int poly[20],choice,i,tdx,tdy,shchoice;
int x,y;
initgraph(&gd,&gm,"C:\\TurboC3\\bgi");
DrawCordinates();
poly[0]=absx(20);
poly[1]=absy(20);
poly[2]=absx(70);
poly[3]=absy(20);
poly[4]=absx(70);
poly[5]=absy(70);
poly[6]=absx(20);
poly[7]=absy(70);
poly[8]=poly[0];
poly[9]=poly[1];
drawpoly(5,poly);
printf("This square will be used for 2D transaformations in this program\nPress
Any Key To Continue...");
closegraph();
do
{

printf("\n****************MENU***************\n");
printf("\n1.Translation\n2.Rotation\n3.Scaling\n4.Reflection\n5.Exit");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
clrscr();
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
DrawCordinates();
drawpoly(5,poly);
printf("\nEnter translation vectors (tx,ty):");
scanf("%d%d",&tdx,&tdy);
for(i=0;i<8;i=i+2)
{
poly[i]=poly[i]+tdx;
poly[i+1]-=tdy;
}
poly[8]=poly[0];
poly[9]=poly[1];
setcolor(RED);
drawpoly(5,poly);

closegraph();
break;
case 2:

initgraph(&gd,&gm,"c:\\turboc3\\bgi");
DrawCordinates();
drawpoly(5,poly);
printf("\nEnter rotation angle:");
scanf("%d",&ang);
for(i=0;i<8;i=i+2)
{
int tx= poly[i]-320;
poly[i]=320+((poly[i]-320)*cos(ang*0.01744)-(240-
poly[i+1])*sin(ang*0.01744));
poly[i+1]=240-((240-poly[i+1])*cos(ang*0.01744)+
(tx)*sin(ang*0.01744));
}
poly[8]=poly[0];
poly[9]=poly[1];
setcolor(RED);
drawpoly(5,poly);

closegraph();
break;
case 3:

initgraph(&gd,&gm,"c:\\turboc3\\bgi");
DrawCordinates();
drawpoly(5,poly);
printf("\nEnter scaling vectors (tx,ty):");
scanf("%d%d",&sx,&sy);
for(i=2;i<8;i=i+2)
{
poly[i]=poly[i]*sx+poly[0]*(1-sx);
poly[i+1]=poly[i+1]*sy+poly[1]*(1-sy);
}
poly[8]=poly[0];
poly[9]=poly[1];
setcolor(RED);
drawpoly(5,poly);

closegraph();
break;
case 4:

initgraph(&gd,&gm,"C:\TurboC++\Disk\TurboC3\BGI");
DrawCordinates();
drawpoly(5,poly);
printf("\n1.Y-axis\n2.X-axis\n3.exit\n");
scanf("%d",&c1);
switch(c1)
{
case 1:
for(k=0;k<8;k=k+2)
{
x1=poly[k];
y1=poly[k+1];
poly[k]=320-(x1-320);
poly[k+1]=y1;
}
poly[8]=poly[0];
poly[9]=poly[1];
setcolor(RED);
drawpoly(5,poly);

break;
case 2:
for(k=0;k<8;k=k+2)
{
x1=poly[k];
y1=poly[k+1];
poly[k]=x1;
poly[k+1]=240-(y1-240);
}
poly[8]=poly[0];
poly[9]=poly[1];
setcolor(RED);
drawpoly(5,poly);

break;
case 3:
break;

}
closegraph();
break;
case 5:
exit(0);
}
}while(choice!=6);
}
3D Transformation
#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<math.h>

void trans();

//void axis();

void scale();

void rotate();

int maxx,maxy,midx,midy;

/*void axis()

// getch();

// cleardevice();

line(midx,0,midx,maxy);

line(0,midy,maxx,midy);

}*/

void main()

int ch;

int gd=DETECT,gm;

detectgraph(&gd,&gm);

initgraph(&gd,&gm,"C:\\TurboC3\\bgi");

printf("\n 1.Translation \n2.Scaling\n 3.Rotation \n 4.exit");

printf("enter your choice");

scanf("%d",&ch);

do

switch(ch)

{
case 1 : trans();

getch();

// closegraph();

break;

case 2 : scale();

getch();

// closegraph();

break;

case 3 : rotate();

getch();

// closegraph();

break;

case 4 : break;

printf("enter your choice");

scanf("%d",&ch);

} while(ch<4);

void trans()

int x,y,z,o,x1,x2,y1,y2;

maxx=getmaxx();

maxy=getmaxy();

midx=maxx/2;

midy=maxy/2;

//axis();
bar3d(midx+50,midy-100,midx+60,midy-90,10,1);

printf("Enter translation factor");

scanf("%d%d",&x,&y);

printf("After translation:");

bar3d(midx+x+50,midy-(y+100),midx+x+60,midy-(y+90),10,1);

void scale()

int x,y,z,o,x1,x2,y1,y2;

maxx=getmaxx();

maxy=getmaxy();

midx=maxx/2;

midy=maxy/2;

//axis();

bar3d(midx+50,midy-100,midx+60,midy-90,5,1);

printf("before translation\n");

printf("Enter scaling factors\n");

scanf("%d %d %d", &x,&y,&z);

printf("After scaling\n");

bar3d(midx+(x*50),midy-(y*100),midx+(x*60),midy-(y*90),5*z,1);

void rotate()

int x,y,z,o,x1,x2,y1,y2;

maxx=getmaxx();

maxy=getmaxy();

midx=maxx/2;

midy=maxy/2;

//axis();

bar3d(midx+50,midy-100,midx+60,midy-90,5,1);
printf("Enter rotating angle");

scanf("%d",&o);

x1=50*cos(o*3.14/180)-100*sin(o*3.14/180);

y1=50*sin(o*3.14/180)+100*cos(o*3.14/180);

x2=60*cos(o*3.14/180)-90*sin(o*3.14/180);

y2=60*sin(o*3.14/180)+90*cos(o*3.14/180);

// axis();

// printf("After rotation about z axis");

// bar3d(midx+x1,midy-y1,midx+x2,midy-y2,5,1);

//axis();

printf("After rotation about x axis");

bar3d(midx+50,midy-x1,midx+60,midy-x2,5,1);

//axis();

printf("After rotation about yaxis");

bar3d(midx+x1,midy-100,midx+x2,midy-90,5,1);

}
EXPERIMENT NO.: 8
Cohen Sutherland line clipping algorithm

#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>

#define X_COORDINATE 0
#define Y_COORDINATE 1
#define Z_COORDINATE 2
#define b0 0
#define b1 1
#define b2 2
#define b3 3

float xBoundaryRight, xBoundaryLeft, yBoundaryUpper, yBoundaryLower;


float p1[3], p2[3], p3[3], p4[3];
/* corners of the clipping rectangle, clock-wise from top left */

float givenLineSegmentP1[3], givenLineSegmentP2[3];


float clippedLineP1[3], clippedLineP2[3];
void display ();
void myInit ();

int main (int argc, char **argv) {

char outP1[4], outP2[4], outTemp[4]; /* b0, b1, b2 and b3 as described in the


text book. */
int i;
float m, c, tempPoint[3];

printf ("Enter X-Boundary (right): ");


scanf ("%f", &xBoundaryRight);
printf ("Enter X-Boundary (left): ");
scanf ("%f", &xBoundaryLeft);
printf ("Enter Y-Boundary (upper): ");
scanf ("%f", &yBoundaryUpper);
printf ("Enter Y-Boundary (lower): ");
scanf ("%f", &yBoundaryLower);

printf ("Enter the first point of the the line segment to be clipped (x, y): ");
scanf ("%f %f", &givenLineSegmentP1[X_COORDINATE],
&givenLineSegmentP1[Y_COORDINATE]);

printf ("Enter the second point of the the line segment to be clipped (x, y): ");
scanf ("%f %f", &givenLineSegmentP2[X_COORDINATE],
&givenLineSegmentP2[Y_COORDINATE]);

givenLineSegmentP2[Z_COORDINATE] = givenLineSegmentP1[Z_COORDINATE]
= 0;

/* Now calcluate the corners of the clipping rectangle */

p1[X_COORDINATE] = xBoundaryLeft;
p1[Y_COORDINATE] = yBoundaryUpper;
p1[Z_COORDINATE] = 0;

p2[X_COORDINATE] = xBoundaryRight;
p2[Y_COORDINATE] = yBoundaryUpper;
p2[Z_COORDINATE] = 0;

p3[X_COORDINATE] = xBoundaryRight;
p3[Y_COORDINATE] = yBoundaryLower;
p3[Z_COORDINATE] = 0;

p4[X_COORDINATE] = xBoundaryLeft;
p4[Y_COORDINATE] = yBoundaryLower;
p4[Z_COORDINATE] = 0;

/* Do the required calculations. */


/* b0 is 1 if point is outside of yBoundaryUpper
b1 is 1 if point is outside of yBoundaryLower
b2 is 1 if point is outside of xBoundaryLeft and
b3 is 1 if point is outside of xBoundaryRight. */

outP1[b0] = (givenLineSegmentP1[Y_COORDINATE] > yBoundaryUpper) ? 1 : 0;


outP1[b1] = (givenLineSegmentP1[Y_COORDINATE] < yBoundaryLower) ? 1 : 0;
outP1[b2] = (givenLineSegmentP1[X_COORDINATE] < xBoundaryLeft) ? 1 : 0;
outP1[b3] = (givenLineSegmentP1[X_COORDINATE] > xBoundaryRight) ? 1 : 0;
outP2[b0] = (givenLineSegmentP2[Y_COORDINATE] > yBoundaryUpper) ? 1 : 0;
outP2[b1] = (givenLineSegmentP2[Y_COORDINATE] < yBoundaryLower) ? 1 : 0;
outP2[b2] = (givenLineSegmentP2[X_COORDINATE] < xBoundaryLeft) ? 1 : 0;
outP2[b3] = (givenLineSegmentP2[X_COORDINATE] > xBoundaryRight) ? 1 : 0;

/* We now have four cases to consider according to the Cohen-Sutherland


Algorithm. */
/* The point may be out of more than one side. In which case, we first clip w.r.t
one side and continue the loop (updating the outP[] and clippedLine[] arrays
*/

for (i = 0; i < 3; i++) clippedLineP1[i] = givenLineSegmentP1[i];


for (i = 0; i < 3; i++) clippedLineP2[i] = givenLineSegmentP2[i];
/* Henceforth, only clippedLineP[] will be used. */
while (1) {
/* case 1 */
if (outP1[b0] == 0 && outP1[b1] == 0 && outP1[b2] == 0 && outP1[b3] == 0
&&
outP2[b0] == 0 && outP2[b1] == 0 && outP2[b2] == 0 && outP2[b3] == 0)
{
break;
}

/* case 2. One point inside and the other outside the rect. Split into two cases.
*/
/* case 2a: outP1 != 0 && outP2 == 0: First point is outside and second inside.
*/
else if ((outP1[b0] != 0 || outP1[b1] != 0 || outP1[b2] != 0 || outP1[b3] != 0) &&
(outP2[b0] == 0 && outP2[b1] == 0 && outP2[b2] == 0 && outP2[b3] ==
0)) {
if (clippedLineP1[X_COORDINATE] == clippedLineP2[X_COORDINATE]) {
/* then m = (y2-y1)/(x2-x1) would be infinity. */
if (outP1[b0] == 1) {
clippedLineP1[Y_COORDINATE] = yBoundaryUpper;
outP1[b0] = 0;
}
else {
clippedLineP1[Y_COORDINATE] = yBoundaryLower;
outP1[b2] = 0;
}
continue;
}
m = clippedLineP1[Y_COORDINATE]-clippedLineP2[Y_COORDINATE];
m /= clippedLineP1[X_COORDINATE]-clippedLineP2[X_COORDINATE];

if (m == 0) {
if (outP1[b2] == 1) {
clippedLineP1[X_COORDINATE] = xBoundaryLeft;
outP1[b2] = 0;
}
else {
clippedLineP1[X_COORDINATE] = xBoundaryRight;
outP1[b3] = 0;
}
continue;
}
/* Line is neither horizontal nor vertical at this point */
/* We try to clip whichever bound exceeds that we see first. After clipping
at that point, we immediately check if the other (if any) has been clipped
too. */

c = clippedLineP1[Y_COORDINATE]-(m*clippedLineP1[X_COORDINATE]);
if (outP1[b0] == 1) {
clippedLineP1[Y_COORDINATE] = yBoundaryUpper;
clippedLineP1[X_COORDINATE] = (yBoundaryUpper-c)/m;
outP1[b0] = 0;
if (clippedLineP1[X_COORDINATE] <= xBoundaryRight &&
clippedLineP1[X_COORDINATE] >= xBoundaryLeft) {
outP1[b2] = outP1[b3] = 0;
break;
}
continue;
}
if (outP1[b1] == 1) {
clippedLineP1[Y_COORDINATE] = yBoundaryLower;
clippedLineP1[X_COORDINATE] = (yBoundaryLower-c)/m;
outP1[b1] = 0;
if (clippedLineP1[X_COORDINATE] <= xBoundaryRight &&
clippedLineP1[X_COORDINATE] >= xBoundaryLeft) {
outP1[b2] = outP1[b3] = 0;
break;
}
continue;
}
if (outP1[b2] == 1) {
clippedLineP1[X_COORDINATE] = xBoundaryLeft;
clippedLineP1[Y_COORDINATE] = (m*xBoundaryLeft)+c;
outP1[b2] = 0;
if (clippedLineP1[Y_COORDINATE] <= yBoundaryUpper &&
clippedLineP1[Y_COORDINATE] >= yBoundaryLower) {
outP1[b0] = outP1[b1] = 0;
break;
}
continue;
}
if (outP1[b3] == 1) {
clippedLineP1[X_COORDINATE] = xBoundaryRight;
clippedLineP1[Y_COORDINATE] = (m*xBoundaryRight)+c;
outP1[b3] = 0;
if (clippedLineP1[Y_COORDINATE] <= yBoundaryUpper &&
clippedLineP1[Y_COORDINATE] >= yBoundaryLower) {
outP1[b0] = outP1[b1] = 0;
break;
}
continue;
}
}
/* Case2b: If first point is inside and second point is outside. */
else if ((outP2[b0] != 0 || outP2[b1] != 0 || outP2[b2] != 0 || outP2[b3] != 0) &&
(outP1[b0] == 0 && outP1[b1] == 0 && outP1[b2] == 0 && outP1[b3] ==
0)) {
/* Exchange first and second points completely including outP[] and
continue. */
for (i = 0; i < 3; i++) tempPoint[i] = clippedLineP1[i];
for (i = 0; i < 3; i++) clippedLineP1[i] = clippedLineP2[i];
for (i = 0; i < 3; i++) clippedLineP2[i] = tempPoint[i];
for (i = 0; i < 4; i++) outTemp[i] = outP1[i];
for (i = 0; i < 4; i++) outP1[i] = outP2[i];
for (i = 0; i < 4; i++) outP2[i] = outTemp[i];
continue;
}

/* Case 3. if (outP1 & outP2 != 0): Both points exceed the same boundary. */
else if ((outP1[b0] && outP2[b0] == 1) || (outP1[b1] && outP2[b1] == 1) ||
(outP1[b2] && outP2[b2] == 1) || (outP1[b3] && outP2[b3] == 1)) {
/* set clippedLineP1 and clippedLineP2 both to 0,0,0 */
for (i = 0; i < 3; i++) clippedLineP2[i] = clippedLineP1[i] = 0;
break;
}
/* case 4. Both endpoints are outside, but of different edges. */
else {
/* Determine the points of intersection of the line with the four boundaries
and determine the outcode for each of these intersection points. We can
then estimate if the line is entirely out of the boundary or not. */
float intersectionP1[3], intersectionP2[3], intersectionP3[3],
intersectionP4[3];
int validIntersections = 0;

/* check for special cases where line is horizontal or vertical. */


if (clippedLineP1[X_COORDINATE] == clippedLineP2[X_COORDINATE]) {
/* The line starts from above yBoundaryUpper and ends below
yBoundaryLower. */
clippedLineP1[Y_COORDINATE] = yBoundaryUpper;
clippedLineP2[Y_COORDINATE] = yBoundaryLower;
break;
}
if (clippedLineP1[Y_COORDINATE] == clippedLineP2[Y_COORDINATE]) {
/* The line starts from left of xBoundaryLeft and ends after xBoundaryRight.
*/
clippedLineP1[X_COORDINATE] = xBoundaryLeft;
clippedLineP2[X_COORDINATE] = xBoundaryRight;
break;
}
/* Line is neither vertical nor horizontal. Do all four intersections. */
m = clippedLineP1[Y_COORDINATE] - clippedLineP2[Y_COORDINATE];
m /= clippedLineP1[X_COORDINATE] - clippedLineP2[X_COORDINATE];
c = clippedLineP1[Y_COORDINATE] - (m*clippedLineP1[X_COORDINATE]);

/* Intersection with yBoundaryUpper */


intersectionP1[Y_COORDINATE] = yBoundaryUpper;
intersectionP1[X_COORDINATE] = (yBoundaryUpper-c)/m;
intersectionP1[Z_COORDINATE] = 0;

/* Intersection with yBoundaryLower */


intersectionP2[Y_COORDINATE] = yBoundaryLower;
intersectionP2[X_COORDINATE] = (yBoundaryLower-c)/m;
intersectionP2[Z_COORDINATE] = 0;

/* Intersection with xBoundaryLeft */


intersectionP3[X_COORDINATE] = xBoundaryLeft;
intersectionP3[Y_COORDINATE] = (m*xBoundaryLeft)+c;
intersectionP3[Z_COORDINATE] = 0;

/* Intersection with xBoundaryRight */


intersectionP4[X_COORDINATE] = xBoundaryRight;
intersectionP4[Y_COORDINATE] = (m*xBoundaryRight)+c;
intersectionP4[Z_COORDINATE] = 0;

/* If all these intersections are "outside", line is outside.


If atleast one intersection is inside, there is one more
intersection that is inside. These two intersections form
the clipped line. (unless the line passes through a corner). */
/* The line cannot pass through the viewing rectange at more
than two points. If there are more than two points (among
the above intersection points) that are inside the viewing
rectangle, some are identical. We need to set clippedLineP1
and clippedLineP2 to these two points (if any). */
/* When we find the 2nd valid intersection, we verify that it is
not the same as the first and then only use it. At the end, if
we have only one valid intersection, we reset both end points to
0,0,0 so that the line is not displayed as clipped. */
/* WE NEED TO FIND UNIQUE INTERSECTION POINTS THAT ARE INSIDE (if
any). */

/* intersectionP1 (intersection with yBoundaryUpper). */


if (intersectionP1[X_COORDINATE] <= xBoundaryRight &&
intersectionP1[X_COORDINATE] >= xBoundaryLeft) {
for (i = 0; i < 3; i++) clippedLineP1[i] = intersectionP1[i];
validIntersections++;
}
/* intersectionP2 (intersection with yBoundaryLower). */
if (intersectionP2[X_COORDINATE] <= xBoundaryRight &&
intersectionP2[X_COORDINATE] >= xBoundaryLeft) {
if (validIntersections == 1) {
/* we need to verify if its the same point as the first one. */
if (intersectionP2[X_COORDINATE] == clippedLineP1[X_COORDINATE] &&
intersectionP2[Y_COORDINATE] == clippedLineP1[Y_COORDINATE] &&
intersectionP2[Z_COORDINATE] == clippedLineP1[Z_COORDINATE])
/* do nothing. */ ;
else {
for (i = 0; i < 3; i++) clippedLineP2[i] = intersectionP2[i];
validIntersections++;
break;
}
}
else {
for (i = 0; i < 3; i++) clippedLineP1[i] = intersectionP2[i];
validIntersections++;
}
}
/* intersectionP3 (intersection with xBoundaryLeft). */
if (intersectionP3[Y_COORDINATE] <= yBoundaryUpper &&
intersectionP3[Y_COORDINATE] >= yBoundaryLower) {
if (validIntersections == 1) {
/* we need to verify if its the same point as the first one. */
if (intersectionP3[X_COORDINATE] == clippedLineP1[X_COORDINATE] &&
intersectionP3[Y_COORDINATE] == clippedLineP1[Y_COORDINATE] &&
intersectionP3[Z_COORDINATE] == clippedLineP1[Z_COORDINATE])
/* do nothing. */ ;
else {
for (i = 0; i < 3; i++) clippedLineP2[i] = intersectionP3[i];
validIntersections++;
break;
}
}
else {
for (i = 0; i < 3; i++) clippedLineP1[i] = intersectionP3[i];
validIntersections++;
}
}
/* intersectionP4 (intersection with xBoundaryRight */
if (intersectionP4[Y_COORDINATE] <= yBoundaryUpper &&
intersectionP4[Y_COORDINATE] >= yBoundaryLower) {
if (validIntersections == 1) {
/* we need to verify if its the same point as the first one. */
if (intersectionP4[X_COORDINATE] == clippedLineP1[X_COORDINATE] &&
intersectionP4[Y_COORDINATE] == clippedLineP1[Y_COORDINATE] &&
intersectionP4[Z_COORDINATE] == clippedLineP1[Z_COORDINATE])
/* do nothing. */ ;
else {
for (i = 0; i < 3; i++) clippedLineP2[i] = intersectionP4[i];
validIntersections++;
break;
}
}
else {
for (i = 0; i < 3; i++) clippedLineP1[i] = intersectionP4[i];
validIntersections++;
}
}

/* If validIntersections <= 1 at this point, we set both clippedLineP1


and clippedLineP2 to 0,0,0. Only one validIntersection means that
the line just passes through a corner of the viewing rectangle. */
if (validIntersections <= 1)
for (i = 0; i < 3; i++) clippedLineP2[i] = clippedLineP1[i] = 0;
break;
}
}

glutInit (&argc, argv);


glutCreateWindow ("Cohen-Sutherland Line Clipping");
glutInitWindowPosition (400, 400);
glutInitWindowSize (640, 480);
glutDisplayFunc (display);
myInit();
glutMainLoop();
}

void myInit () {

glClearColor (1.0, 1.0, 1.0, 1.0);


glPointSize (1);
glColor3f (0.0, 0.0, 0.0);

glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho (-50.0, 50.0, -50.0, 50.0, -50.0, 50.0);
glMatrixMode (GL_MODELVIEW);
}

void display () {
/* Clipped line is displayed in red, and unclipped in black. */

glClear (GL_COLOR_BUFFER_BIT);
glColor3f (0.0, 0.0, 0.0);
glBegin (GL_LINE_LOOP);
glVertex3fv (p1);
glVertex3fv (p2);
glVertex3fv (p3);
glVertex3fv (p4);
glEnd ();
glBegin (GL_LINES);
glVertex3fv (givenLineSegmentP1);
glVertex3fv (givenLineSegmentP2);
glColor3f (1.0, 0.0, 0.0);
glLineWidth (2);
glVertex3fv (clippedLineP1);
glVertex3fv (clippedLineP2);
glEnd();

glFlush();
}
EXPERIMENT NO.: 9
Sutherland Hodgeman polygon clipping algorithm
#include<GL/glut.h>
#include<GL/glu.h>
#include<GL/gl.h>
#include<stdio.h>

struct Point{
float x,y;
} w[4],oVer[4];
int Nout;

void drawPoly(Point p[],int n){


glBegin(GL_POLYGON);
for(int i=0;i<n;i++)
glVertex2f(p[i].x,p[i].y);
glEnd();
}

bool insideVer(Point p){


if((p.x>=w[0].x)&&(p.x<=w[2].x))
if((p.y>=w[0].y)&&(p.y<=w[2].y))
return true;
return false;
}

void addVer(Point p){


oVer[Nout]=p;
Nout=Nout+1;
}

Point getInterSect(Point s,Point p,int edge){


Point in;
float m;
if(w[edge].x==w[(edge+1)%4].x){ //Vertical Line
m=(p.y-s.y)/(p.x-s.x);
in.x=w[edge].x;
in.y=in.x*m+s.y;
}
else{//Horizontal Line
m=(p.y-s.y)/(p.x-s.x);
in.y=w[edge].y;
in.x=(in.y-s.y)/m;
}
return in;
}

void clipAndDraw(Point inVer[],int Nin){


Point s,p,interSec;
for(int i=0;i<4;i++)
{
Nout=0;
s=inVer[Nin-1];
for(int j=0;j<Nin;j++)
{
p=inVer[j];
if(insideVer(p)==true){
if(insideVer(s)==true){
addVer(p);
}
else{
interSec=getInterSect(s,p,i);
addVer(interSec);
addVer(p);
}
}
else{
if(insideVer(s)==true){
interSec=getInterSect(s,p,i);
addVer(interSec);
}
}
s=p;
}
inVer=oVer;
Nin=Nout;
}
drawPoly(oVer,4);
}

void init(){
glClearColor(0.0f,0.0f,0.0f,0.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0,100.0,0.0,100.0,0.0,100.0);
glClear(GL_COLOR_BUFFER_BIT);
w[0].x =20,w[0].y=10;
w[1].x =20,w[1].y=80;
w[2].x =80,w[2].y=80;
w[3].x =80,w[3].y=10;
}
void display(void){
Point inVer[4];
init();
// As Window for Clipping
glColor3f(1.0f,0.0f,0.0f);
drawPoly(w,4);
// As Rect
glColor3f(0.0f,1.0f,0.0f);
inVer[0].x =10,inVer[0].y=40;
inVer[1].x =10,inVer[1].y=60;
inVer[2].x =60,inVer[2].y=60;
inVer[3].x =60,inVer[3].y=40;
drawPoly(inVer,4);
// As Rect
glColor3f(0.0f,0.0f,1.0f);
clipAndDraw(inVer,4);
// Print
glFlush();
}

int main(int argc,char *argv[]){


glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(400,400);
glutInitWindowPosition(100,100);
glutCreateWindow("SASI KANTH completed Polygon Clipping!");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

You might also like