Assignment 03
Assignment 03
#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];
p[i].x=pp[0].x;
p[i].y=pp[0].y;
n=j;
}
p[i].x=pp[0].x;
p[i].y=pp[0].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();
}
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);
}
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;
-----------------------------------------------------------------------------------
------
-----------------------------------------------------------------------------------
------
-----------------------------------------------------------------------------------
------
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
typedef struct
{
float x;
float y;
} Vertex;
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;
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;
}
}
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;
}
}
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);
glFlush();
}
// 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);
}
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
-----------------------------------------------------------------------------------
------
-----------------------------------------------------------------------------------
------
-----------------------------------------------------------------------------------
------
#include <GL/glut.h>
#include<bits/stdc++.h>
using namespace std;
int radius,centerX,centerY;
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();
}
-----------------------------------------------------------------------------------
------
-----------------------------------------------------------------------------------
------
-----------------------------------------------------------------------------------
------
4. Mid point Circle Drawing Algorithm Source Code
#include <GL/glut.h>
#include<bits/stdc++.h>
using namespace std;
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();
}