CGV 3rd Week Lab Programs
CGV 3rd Week Lab Programs
#include<stdio.h>
#include<GL/glut.h>
#include<math.h>
//int x1,y1,x2,y2;
int n;
int endpoints[8][4];
int dx = x2 - x1;
int dy = y2 - y1;
int i=0;int steps=0;
void DDA(){
int i;
glClearColor(1.0,1.0,1.0,1.0);
glClear(GL_COLOR_BUFFER_BIT);
for(i=0;i<n;i++){
//x1=endpoints[i][0];y1=endpoints[i][1];x2=endpoints[i]
[2];y2=endpoints[i][3];
draw_line(endpoints[i][0],endpoints[i][1],endpoints[i]
[2],endpoints[i][3]);
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINES);
glVertex2i(endpoints[i][0],endpoints[i][1]);
glVertex2i(endpoints[i][2],endpoints[i][3]);
glEnd();
//printf("The points are %d %d %d %d\n",x1,y1,x2,y2);
glFlush();
}
drawText(220,475,"DDA Line drawing algorithm");
glFlush();
void myInit(){
//glClearColor(1.0,1.0,1.0,1.0);
//glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,500,0,500);
glMatrixMode(GL_MODELVIEW);
}
}
Develop a program to draw a line using Bresenham’s line drawing technique
#include<stdio.h>
#include<GL/glut.h>
int x1,y1,x2,y2;
void draw_pixel(int x, int y)
{ glColor3f(0.0,0.0,1.0);
glPointSize(5);
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}
void Bresenham()
{ glClear(GL_COLOR_BUFFER_BIT);
glClearColor(1.0,1.0,1.0,1.0);
draw_line(x1,y1,x2,y2);
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINES);
glVertex2i(x1,y1);
glVertex2i(x2,y2);
glEnd();
glFlush();
}
void myinit()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,100,0,100);
glMatrixMode(GL_MODELVIEW);
}
void draw_line(int x1,int y1, int x2, int y2)
{
int dx, dy, i, e;
int incx, incy, inc1, inc2;
int x,y;
dx = x2-x1;
dy = y2-y1;
if (dx < 0) dx = -dx;
if (dy < 0) dy = -dy;
incx = 1;
if (x2 < x1) incx = -1;
incy = 1;
if (y2 < y1) incy = -1;
x = x1; y = y1;
y += incy;
draw_pixel(x, y);
}
}
}
void main(int argc,char ** argv)
{ printf("Enter the endpoints of the line segment");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("Bresenham Line Algorithm");
glutDisplayFunc(Bresenham);
myinit();
glutMainLoop();
}