0% found this document useful (0 votes)
11 views18 pages

Assignment 03

The document describes the Sutherland-Hodgman and Weiler-Atherton polygon clipping algorithms. The Sutherland-Hodgman algorithm uses 4 clipping functions (left, right, top, bottom) to clip a polygon against a clipping window. Each clipping function iterates through the polygon vertices, handling 3 cases to determine if a vertex is inside or outside the clipping boundary and adds/removes vertices as needed. The Weiler-Atherton algorithm uses a more complex approach that tracks the state of polygon edges and clipping edges to determine intersections. It uses temporary arrays, tags and traversal lists to process intersections and build the clipped polygon.

Uploaded by

Geerbani Shashi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views18 pages

Assignment 03

The document describes the Sutherland-Hodgman and Weiler-Atherton polygon clipping algorithms. The Sutherland-Hodgman algorithm uses 4 clipping functions (left, right, top, bottom) to clip a polygon against a clipping window. Each clipping function iterates through the polygon vertices, handling 3 cases to determine if a vertex is inside or outside the clipping boundary and adds/removes vertices as needed. The Weiler-Atherton algorithm uses a more complex approach that tracks the state of polygon edges and clipping edges to determine intersections. It uses temporary arrays, tags and traversal lists to process intersections and build the clipped polygon.

Uploaded by

Geerbani Shashi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 18

1.

Sutherland-Hodgman Polygon Clipping Algorithm

#include<stdio.h>
#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>
#include<math.h>

typedef struct
{
float x;
float y;
} PT;

int n;

int i,j;

PT p1,p2,p[20],pp[20];

void left() //left clipper


{
i=0;
j=0;

for(i=0; i<n; i++)


{
if(p[i].x<p1.x && p[i+1].x>=p1.x) //Case-1: outside to inside
{
if(p[i+1].x-p[i].x!=0)
{
pp[j].y=(p[i+1].y-p[i].y)/(p[i+1].x-p[i].x)*(p1.x-p[i].x)+p[i].y;
// save point of intersection
}
else
{
pp[j].y=p[i].y;
}
pp[j].x=p1.x;
j++;
pp[j].x=p[i+1].x;
pp[j].y=p[i+1].y;
j++;

if(p[i].x>=p1.x && p[i+1].x>=p1.x) //Case-2: inside to inside


{
pp[j].y=p[i+1].y;
pp[j].x=p[i+1].x;
j++;
}

if(p[i].x>=p1.x && p[i+1].x<p1.x) // Case-3: inside to outside


{
if(p[i+1].x-p[i].x!=0)
{
pp[j].y=(p[i+1].y-p[i].y)/(p[i+1].x-p[i].x)*(p1.x-p[i].x)+p[i].y;
// only save point of intersection
}
else
{
pp[j].y=p[i].y;
}
pp[j].x=p1.x;
j++;
}
}

for(i=0; i<j; i++)


{
p[i].x=pp[i].x;
p[i].y=pp[i].y;
}

p[i].x=pp[0].x;
p[i].y=pp[0].y;
n=j;
}

void right() // right clipper


{
i=0;
j=0;

for(i=0; i<n; i++)


{
if(p[i].x>p2.x && p[i+1].x<=p2.x) //Case-1: outside to inside
{
if(p[i+1].x-p[i].x!=0)
{
pp[j].y=(p[i+1].y-p[i].y)/(p[i+1].x-p[i].x)*(p2.x-p[i].x)+p[i].y;
// save point of intersection
}
else
{
pp[j].y=p[i].y;
}
pp[j].x=p2.x;
j++;
pp[j].x=p[i+1].x;
pp[j].y=p[i+1].y;
j++;
}

if(p[i].x<=p2.x && p[i+1].x<=p2.x) // Case-2: inside to inside


{
pp[j].y=p[i+1].y;
pp[j].x=p[i+1].x;
j++;
}

if(p[i].x<=p2.x && p[i+1].x>p2.x) // Case-3: inside to outside


{
if(p[i+1].x-p[i].x!=0)
{
pp[j].y=(p[i+1].y-p[i].y)/(p[i+1].x-p[i].x)*(p2.x-p[i].x)+p[i].y;
// only save point of intersection
}
else
{
pp[j].y=p[i].y;
}
pp[j].x=p2.x;
j++;
}

for(i=0; i<j; i++)


{
p[i].x=pp[i].x;
p[i].y=pp[i].y;
}

p[i].x=pp[0].x;
p[i].y=pp[0].y;

void top() // top clipper


{
i=0;
j=0;

for(i=0; i<n; i++)


{
if(p[i].y>p2.y && p[i+1].y<=p2.y) //Case-1: outside to inside
{
if(p[i+1].y-p[i].y!=0)
{
pp[j].x=(p[i+1].x-p[i].x)/(p[i+1].y-p[i].y)*(p2.y-p[i].y)+p[i].x;
// save point of intersection
}
else
{
pp[j].x=p[i].x;
}
pp[j].y=p2.y;
j++;
pp[j].x=p[i+1].x;
pp[j].y=p[i+1].y;
j++;
}

if(p[i].y<=p2.y && p[i+1].y<=p2.y) // Case-2: inside to inside


{
pp[j].y=p[i+1].y;
pp[j].x=p[i+1].x;
j++;
}

if(p[i].y<=p2.y && p[i+1].y>p2.y) // Case-3: inside to outside


{
if(p[i+1].y-p[i].y!=0)
{
pp[j].x=(p[i+1].x-p[i].x)/(p[i+1].y-p[i].y)*(p2.y-p[i].y)+p[i].x;
// only save point of intersection
}
else
{
pp[j].x=p[i].x;
}
pp[j].y=p2.y;
j++;
}
}

for(i=0; i<j; i++)


{
p[i].x=pp[i].x;
p[i].y=pp[i].y;
}

p[i].x=pp[0].x;
p[i].y=pp[0].y;
n=j;
}

void bottom() // bottom clipper


{
i=0;
j=0;

for(i=0; i<n; i++)


{
if(p[i].y<p1.y && p[i+1].y>=p1.y) // Case-1: outside to inside
{
if(p[i+1].y-p[i].y!=0)
{
pp[j].x=(p[i+1].x-p[i].x)/(p[i+1].y-p[i].y)*(p1.y-p[i].y)+p[i].x;
// save point of intersection
}
else
{
pp[j].x=p[i].x;
}
pp[j].y=p1.y;
j++;
pp[j].x=p[i+1].x;
pp[j].y=p[i+1].y;
j++;
}

if(p[i].y>=p1.y && p[i+1].y>=p1.y) // Case-2: inside to inside


{
pp[j].x=p[i+1].x;
pp[j].y=p[i+1].y;
j++;
}

if(p[i].y>=p1.y && p[i+1].y<p1.y) // Case-3: inside to outside


{
if(p[i+1].y-p[i].y!=0)
{
pp[j].x=(p[i+1].x-p[i].x)/(p[i+1].y-p[i].y)*(p1.y-p[i].y)+p[i].x;
// only save point of intersection
}
else
{
pp[j].x=p[i].x;
}
pp[j].y=p1.y;
j++;
}
}

for(i=0; i<j; i++)


{
p[i].x=pp[i].x;
p[i].y=pp[i].y;
}
p[i].x=pp[0].x;
p[i].y=pp[0].y;
n=j;
}

void drawpolygon()
{
glColor3f(1.0,0.0,0.0);
for(i=0; i<n-1; i++)
{
glBegin(GL_LINES);
glVertex2d(p[i].x,p[i].y);
glVertex2d(p[i+1].x,p[i+1].y);
glEnd();
}
glBegin(GL_LINES);
glVertex2d(p[i].x,p[i].y);
glVertex2d(p[0].x,p[0].y);
glEnd();
}

void myMouse(int button, int state, int x, int y)


{
if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
{
glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_LINE_LOOP);
glVertex2f(p1.x,p1.y);
glVertex2f(p2.x,p1.y);
glVertex2f(p2.x,p2.y);
glVertex2f(p1.x,p2.y);
glEnd();
left();
right();
top();
bottom();
drawpolygon();
}
glFlush();
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.4,1.0,0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(p1.x, p1.y);
glVertex2f(p2.x,p1.y);
glVertex2f(p2.x,p2.y);
glVertex2f(p1.x,p2.y);
glEnd();
drawpolygon();
glFlush();
}

void init(void)
{
glClearColor(0.0,0.0,0.0,0.0); // clear screen usually black
gluOrtho2D(0,500,0,500);
}

int main(int argc, char**argv)


{
printf("Enter Window Coordinates:\n");
printf("Please Enter two Points:\n");
printf("Enter P1(x,y): ");
scanf("%f", &p1.x);
scanf("%f", &p1.y);

printf("Enter P2(x,y): ");


scanf("%f", &p2.x);
scanf("%f", &p2.y);

printf("\nEnter the no. of vertices: ");


scanf("%d", &n);

for(i=0; i<n; i++)


{
printf("\nEnter V%d(x%d,y%d): ", i+1, i+1, i+1);
scanf("%f", &p[i].x);
scanf("%f", &p[i].y);
}

p[i].x=p[0].x;
p[i].y=p[0].y;

glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(0,0);
glutCreateWindow("Sutherland Hodgman Polygon Clipping Algorithm 2018331502");
init();

glutDisplayFunc(display);
glutMouseFunc(myMouse);
glFlush();
glutMainLoop();
return 0;

-----------------------------------------------------------------------------------
------
-----------------------------------------------------------------------------------
------
-----------------------------------------------------------------------------------
------

2. Weiler-Atherton Clipping Algorithm source code

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

typedef struct
{
float x;
float y;
} Vertex;

Vertex cw[40], sp[40];


int n_cw, n_sp;

void draw_poly(Vertex vlist[], int n)


{
glColor3f(1.0, 1.0, 1.0); // Set color to white
glBegin(GL_LINE_LOOP);
for (int i = 0; i < n; i++)
{
glVertex2f(vlist[i].x, vlist[i].y);
}
glEnd();
}

int in_out(float x, float y, int x1, int y1, int x2, int y2)
{
float p = (y - y1) * (x2 - x1) - (x - x1) * (y2 - y1);
if (p < 0)
return 0; // for out
return 1; // for in
}

void intersection_lineseg(float *x, float *y, int x1, int y1, int x2, int y2, int
xa, int ya, int xb, int yb)
{
*x = -1;
*y = -1;
if (x2 == x1 && xb == xa)
return;
else if (x2 == x1)
{
float m2 = (float)(yb - ya) / (xb - xa);
*x = x1;
*y = ya - m2 * (xa - x1);
}
else if (xb == xa)
{
float m1 = (float)(y2 - y1) / (x2 - x1);
*x = xa;
*y = y1 + m1 * (xa - x1);
}
else
{
float m1 = (float)(y2 - y1) / (x2 - x1);
float m2 = (float)(yb - ya) / (xb - xa);
if (m1 == m2)
return;
*x = (ya - y1 + m1 * x1 - m2 * xa) / (m1 - m2);
*y = (m1 * m2 * (xa - x1) + m2 * y1 - m1 * ya) / (m2 - m1);
}

if ((x1 >= x2 && (*x < x2 || *x > x1)) || (x2 >= x1 && (*x > x2 || *x < x1)) ||
(y1 >= y2 && (*y < y2 || *y > y1)) || (y2 >= y1 && (*y > y2 || *y < y1)) || (xa >=
xb && (*x < xb || *x > xa)) || (xb >= xa && (*x > xb || *x < xa)) || (ya >= yb &&
(*y < yb || *y > ya)) || (yb >= ya && (*y > yb || *y < ya)))
{
*x = -1;
*y = -1;
}
}

void wa_clip()
{
Vertex tempcw[40], tempsp[40];
int tag_sp[40], tag_cw[40], trav_sp[40], trav_cw[40];
float x, y;
int entry_list[10]; // saves indexes only
int e = -1;

// for new cw array


int kc = -1; // first vertex gets added last in the array
for (int i = 0; i < n_cw; i++)
{
Vertex tempi[20][2]; // for ordering intersection points, the 2nd column's
x is for tag
int ti = -1;
for (int j = 0; j < n_sp; j++)
{
intersection_lineseg(&x, &y, cw[i].x, cw[i].y, cw[(i + 1) % n_cw].x,
cw[(i + 1) % n_cw].y,
sp[j].x, sp[j].y, sp[(j + 1) % n_sp].x, sp[(j + 1)
% n_sp].y);
if (x == -1) // or y == -1
continue;
ti++;
tempi[ti][0].x = x;
tempi[ti][0].y = y;
int p1 = in_out(sp[j].x, sp[j].y, cw[i].x, cw[i].y, cw[(i + 1) %
n_cw].x, cw[(i + 1) % n_cw].y);
int p2 = in_out(sp[(j + 1) % n_sp].x, sp[(j + 1) % n_sp].y, cw[i].x,
cw[i].y, cw[(i + 1) % n_cw].x, cw[(i + 1) % n_cw].y);
if (p1 == 1 && p2 == 0)
tempi[ti][1].x = 1;
else
tempi[ti][1].x = 0;
}
if (ti != -1)
{
if (cw[(i + 1) % n_cw].x > cw[i].x) // sort intersection points
{
// increasing x sort
int min_idx;
for (int k = 0; k < ti; k++)
{
min_idx = k;
for (int m = k + 1; m < ti + 1; m++)
{
if (tempi[m][0].x < tempi[min_idx][0].x)
min_idx = m;
}
float temp = tempi[min_idx][0].x;
tempi[min_idx][0].x = tempi[k][0].x;
tempi[k][0].x = temp;
temp = tempi[min_idx][0].y;
tempi[min_idx][0].y = tempi[k][0].y;
tempi[k][0].y = temp;
temp = tempi[min_idx][1].x;
tempi[min_idx][1].x = tempi[k][1].x;
tempi[k][1].x = temp;
}
}
else if (cw[(i + 1) % n_cw].x < cw[i].x)
{
// decreasing x sort
int max_idx;
for (int k = 0; k < ti; k++)
{
max_idx = k;
for (int m = k + 1; m < ti + 1; m++)
{
if (tempi[m][0].x > tempi[max_idx][0].x)
max_idx = m;
}
float temp = tempi[max_idx][0].x;
tempi[max_idx][0].x = tempi[k][0].x;
tempi[k][0].x = temp;
temp = tempi[max_idx][0].y;
tempi[max_idx][0].y = tempi[k][0].y;
tempi[k][0].y = temp;
temp = tempi[max_idx][1].x;
tempi[max_idx][1].x = tempi[k][1].x;
tempi[k][1].x = temp;
}
}
else if (cw[(i + 1) % n_cw].y > cw[i].y)
{
// increasing y sort
int min_idx;
for (int k = 0; k < ti; k++)
{
min_idx = k;
for (int m = k + 1; m < ti + 1; m++)
{
if (tempi[m][0].y < tempi[min_idx][0].y)
min_idx = m;
}
float temp = tempi[min_idx][0].x;
tempi[min_idx][0].x = tempi[k][0].x;
tempi[k][0].x = temp;
temp = tempi[min_idx][0].y;
tempi[min_idx][0].y = tempi[k][0].y;
tempi[k][0].y = temp;
temp = tempi[min_idx][1].x;
tempi[min_idx][1].x = tempi[k][1].x;
tempi[k][1].x = temp;
}
}
else
{
// decreasing y sort
int max_idx;
for (int k = 0; k < ti; k++)
{
max_idx = k;
for (int m = k + 1; m < ti + 1; m++)
{
if (tempi[m][0].y > tempi[max_idx][0].y)
max_idx = m;
}
float temp = tempi[max_idx][0].x;
tempi[max_idx][0].x = tempi[k][0].x;
tempi[k][0].x = temp;
temp = tempi[max_idx][0].y;
tempi[max_idx][0].y = tempi[k][0].y;
tempi[k][0].y = temp;
temp = tempi[max_idx][1].x;
tempi[max_idx][1].x = tempi[k][1].x;
tempi[k][1].x = temp;
}
}

for (int k = 0; k <= ti; k++) // put sorted intersection points in cw


array
{
kc++;
tempcw[kc].x = tempi[k][0].x;
tempcw[kc].y = tempi[k][0].y;
tag_cw[kc] = tempi[k][1].x;
trav_cw[kc] = 0;
}
}

kc++;
tempcw[kc].x = cw[(i + 1) % n_cw].x;
tempcw[kc].y = cw[(i + 1) % n_cw].y;
tag_cw[kc] = -1;
trav_cw[kc] = 0;
}
// for new sp array
int ks = -1; // first vertex gets added last in the array
for (int i = 0; i < n_sp; i++)
{
Vertex tempi[20][2]; // for ordering intersection points, the 2nd column's
x is for tag
int ti = -1;
for (int j = 0; j < n_cw; j++)
{
intersection_lineseg(&x, &y, cw[j].x, cw[j].y, cw[(j + 1) % n_cw].x,
cw[(j + 1) % n_cw].y,
sp[i].x, sp[i].y, sp[(i + 1) % n_sp].x, sp[(i + 1)
% n_sp].y);
if (x == -1) // or y == -1
continue;
ti++;
tempi[ti][0].x = x;
tempi[ti][0].y = y;
int p1 = in_out(sp[i].x, sp[i].y, cw[j].x, cw[j].y, cw[(j + 1) %
n_cw].x, cw[(j + 1) % n_cw].y);
int p2 = in_out(sp[(i + 1) % n_sp].x, sp[(i + 1) % n_sp].y, cw[j].x,
cw[j].y, cw[(j + 1) % n_cw].x, cw[(j + 1) % n_cw].y);
if (p1 == 1 && p2 == 0)
tempi[ti][1].x = 0;
else
tempi[ti][1].x = 1;
}
if (ti != -1)
{
if (sp[(i + 1) % n_sp].x > sp[i].x) // sort intersection points
{
// increasing x sort
int min_idx;
for (int k = 0; k < ti; k++)
{
min_idx = k;
for (int m = k + 1; m < ti + 1; m++)
{
if (tempi[m][0].x < tempi[min_idx][0].x)
min_idx = m;
}
float temp = tempi[min_idx][0].x;
tempi[min_idx][0].x = tempi[k][0].x;
tempi[k][0].x = temp;
temp = tempi[min_idx][0].y;
tempi[min_idx][0].y = tempi[k][0].y;
tempi[k][0].y = temp;
temp = tempi[min_idx][1].x;
tempi[min_idx][1].x = tempi[k][1].x;
tempi[k][1].x = temp;
}
}
else if (sp[(i + 1) % n_sp].x < sp[i].x)
{
// decreasing x sort
int max_idx;
for (int k = 0; k < ti; k++)
{
max_idx = k;
for (int m = k + 1; m < ti + 1; m++)
{
if (tempi[m][0].x > tempi[max_idx][0].x)
max_idx = m;
}
float temp = tempi[max_idx][0].x;
tempi[max_idx][0].x = tempi[k][0].x;
tempi[k][0].x = temp;
temp = tempi[max_idx][0].y;
tempi[max_idx][0].y = tempi[k][0].y;
tempi[k][0].y = temp;
temp = tempi[max_idx][1].x;
tempi[max_idx][1].x = tempi[k][1].x;
tempi[k][1].x = temp;
}
}
else if (sp[(i + 1) % n_sp].y > sp[i].y)
{
// increasing y sort
int min_idx;
for (int k = 0; k < ti; k++)
{
min_idx = k;
for (int m = k + 1; m < ti + 1; m++)
{
if (tempi[m][0].y < tempi[min_idx][0].y)
min_idx = m;
}
float temp = tempi[min_idx][0].x;
tempi[min_idx][0].x = tempi[k][0].x;
tempi[k][0].x = temp;
temp = tempi[min_idx][0].y;
tempi[min_idx][0].y = tempi[k][0].y;
tempi[k][0].y = temp;
temp = tempi[min_idx][1].x;
tempi[min_idx][1].x = tempi[k][1].x;
tempi[k][1].x = temp;
}
}
else
{
// decreasing y sort
int max_idx;
for (int k = 0; k < ti; k++)
{
max_idx = k;
for (int m = k + 1; m < ti + 1; m++)
{
if (tempi[m][0].y > tempi[max_idx][0].y)
max_idx = m;
}
float temp = tempi[max_idx][0].x;
tempi[max_idx][0].x = tempi[k][0].x;
tempi[k][0].x = temp;
temp = tempi[max_idx][0].y;
tempi[max_idx][0].y = tempi[k][0].y;
tempi[k][0].y = temp;
temp = tempi[max_idx][1].x;
tempi[max_idx][1].x = tempi[k][1].x;
tempi[k][1].x = temp;
}
}

for (int k = 0; k <= ti; k++) // put sorted intersection points in sp


array
{
ks++;
tempsp[ks].x = tempi[k][0].x;
tempsp[ks].y = tempi[k][0].y;
tag_sp[ks] = tempi[k][1].x;
trav_sp[ks] = 0;
}
}

ks++;
tempsp[ks].x = sp[(i + 1) % n_sp].x;
tempsp[ks].y = sp[(i + 1) % n_sp].y;
tag_sp[ks] = -1;
trav_sp[ks] = 0;
}

kc++;
tempcw[kc].x = cw[0].x;
tempcw[kc].y = cw[0].y;
tag_cw[kc] = -1;
trav_cw[kc] = 0;

ks++;
tempsp[ks].x = sp[0].x;
tempsp[ks].y = sp[0].y;
tag_sp[ks] = -1;
trav_sp[ks] = 0;

n_cw = kc + 1;
n_sp = ks + 1;

// Traversal
for (int i = 0; i <= e; i++)
{
int done = 0;
int j = entry_list[i];
while (!done)
{
if (trav_sp[j] == 1)
done = 1;
else if (tag_sp[j] == 1 || tag_sp[j] == -1)
{
glBegin(GL_LINES);
glVertex2f(tempsp[j].x, tempsp[j].y);
glVertex2f(tempsp[(j + 1) % n_sp].x, tempsp[(j + 1) % n_sp].y);
glEnd();
trav_sp[j] = 1;
j++;
}
else if (tag_sp[j] == 0)
{
trav_sp[j] = 1;
// Swap
int k;
for (k = 0; k < n_cw; k++) // find location to switch to
{
if (tempcw[k].x == tempsp[j].x && tempcw[k].y == tempsp[j].y)
{
j = k;
break;
}
}

// Swap functions for array swap


Vertex temp;
int temptag;
int travtemp;
for (int t = 0; t < n_cw; t++)
{
// Swap tempcw[t] and tempsp[t]
temp = tempcw[t];
tempcw[t] = tempsp[t];
tempsp[t] = temp;

// Swap tag_cw[t] and tag_sp[t]


temptag = tag_cw[t];
tag_cw[t] = tag_sp[t];
tag_sp[t] = temptag;

// Swap trav_cw[t] and trav_sp[t]


travtemp = trav_cw[t];
trav_cw[t] = trav_sp[t];
trav_sp[t] = travtemp;
}

int n = n_cw;
n_cw = n_sp;
n_sp = n;
}
}
}
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);

// Draw the clipping window


draw_poly(cw, n_cw);

// Draw the subject polygon


draw_poly(sp, n_sp);

// Perform the Weiler-Atherton clipping algorithm


wa_clip();

glFlush();
}

int main(int argc, char **argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutCreateWindow("Weiler-Atherton Clipping 2018331502");

// Accept user input for the clipping window and subject polygon
printf("Enter no. of vertices in the clipping window: ");
scanf("%d", &n_cw);
printf("Enter vertices (x, y) clockwise for the clipping window:\n");
for (int i = 0; i < n_cw; i++)
{
scanf("%f %f", &cw[i].x, &cw[i].y);
}

printf("Enter no. of vertices in the subject polygon: ");


scanf("%d", &n_sp);
printf("Enter vertices (x, y) clockwise for the subject polygon:\n");
for (int i = 0; i < n_sp; i++)
{
scanf("%f %f", &sp[i].x, &sp[i].y);
}

glutDisplayFunc(display);
glutMainLoop();

return 0;
}

-----------------------------------------------------------------------------------
------
-----------------------------------------------------------------------------------
------
-----------------------------------------------------------------------------------
------

3. Bresenham Circle Drawing Algorithm Source Code

#include <GL/glut.h>
#include<bits/stdc++.h>
using namespace std;

int radius,centerX,centerY;

void drawCircle(int x, int y) {


glBegin(GL_POINTS);
glVertex2i(centerX + x, centerY + y);
glVertex2i(centerX + x, centerY - y);
glVertex2i(centerX - x, centerY + y);
glVertex2i(centerX - x, centerY - y);
glVertex2i(centerX + y, centerY + x);
glVertex2i(centerX + y, centerY - x);
glVertex2i(centerX - y, centerY + x);
glVertex2i(centerX - y, centerY - x);
glEnd();
}

void bresenhamCircle() {
int x = 0;
int y = radius;
int decision = 3 - 2 * radius;

while (x <= y) {
drawCircle(x, y);
if (decision < 0) {
decision += 4 * x + 6;
x++;
} else {
decision += 4 * (x - y) + 10;
x++;
y--;
}
}
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
bresenhamCircle();
glFlush();
}

void reshape(int width, int height) {


glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
glMatrixMode(GL_MODELVIEW);
}

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


cout<<"Enter the radius of circle : ";
cin>>radius;
cout<<"Enter the center point of the circle : ";
cin>>centerX>>centerY;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600, 480);
glutInitWindowPosition(0,0);
glutCreateWindow("Bresenham Circle Drawing 2018331502");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glClearColor(0.0, 0.0, 0.0, 0.0);
glutMainLoop();
return 0;
}

-----------------------------------------------------------------------------------
------
-----------------------------------------------------------------------------------
------
-----------------------------------------------------------------------------------
------
4. Mid point Circle Drawing Algorithm Source Code

#include <GL/glut.h>
#include<bits/stdc++.h>
using namespace std;

int radius ,centerX , centerY;

void drawCircle(int x, int y)


{
glBegin(GL_POINTS);
glVertex2i(centerX + x, centerY + y);
glVertex2i(centerX + x, centerY - y);
glVertex2i(centerX - x, centerY + y);
glVertex2i(centerX - x, centerY - y);
glVertex2i(centerX + y, centerY + x);
glVertex2i(centerX + y, centerY - x);
glVertex2i(centerX - y, centerY + x);
glVertex2i(centerX - y, centerY - x);
glEnd();
}

void midpointCircle()
{
int x = 0;
int y = radius;
int decision = 1 - radius;

while (x <= y)
{
drawCircle(x, y);
if (decision < 0)
{
decision += 2 * x + 3;
x++;
}
else
{
decision += 2 * (x - y) + 5;
x++;
y--;
}
}
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
midpointCircle();
glFlush();
}

void reshape(int width, int height)


{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv)


{
cout<<"Enter the radius of circle : ";
cin>>radius;
cout<<"Enter the center point of the circle : ";
cin>>centerX>>centerY;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(0,0);
glutCreateWindow("Midpoint Circle Drawing Algo 2018331502");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glClearColor(0.0, 0.0, 0.0, 0.0);
glutMainLoop();
return 0;
}

You might also like