-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdda.c
68 lines (67 loc) · 1.68 KB
/
dda.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdio.h>
#include <math.h>
#include <GL/glut.h>
double X1, Y1, X2, Y2;
float round_value(float v)
{
return (v + 0.5);
}
void DDAline()
{
double dx=(X2-X1);
double dy=(Y2-Y1);
double slope=dy/dx;
double c=Y1-slope*X1;
/* Clears buffers to preset values */
glClear(GL_COLOR_BUFFER_BIT);
/* Plot the points */
glBegin(GL_POINTS);
/* Plot the first point */
glVertex2d(X1,Y1);
int k;
/* For every step, find an intermediate vertex */
for(int x=X1;x<X2;x++)
{
int y=slope*x+c;
y=round_value(y);
glVertex2d(round_value(x), round_value(y));
}
glEnd();
glFlush();
}
void Init()
{
/* Set clear color to white */
glClearColor(1.0,1.0,1.0,0);
/* Set fill color to black */
glColor3f(0.0,0.0,0.0);
/* glViewport(0 , 0 , 640 , 480); */
/* glMatrixMode(GL_PROJECTION); */
/* glLoadIdentity(); */
gluOrtho2D(0 , 640 , 0 , 480);
}
void main(int argc, char **argv)
{
printf("Enter two end points of the line to be drawn:\n");
printf("\n************************************");
printf("\nEnter Point1( X1 , Y1):\n");
scanf("%lf%lf",&X1,&Y1);
printf("\n************************************");
printf("\nEnter Point1( X2 , Y2):\n");
scanf("%lf%lf",&X2,&Y2);
/* Initialise GLUT library */
glutInit(&argc,argv);
/* Set the initial display mode */
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
/* Set the initial window position and size */
glutInitWindowPosition(0,0);
glutInitWindowSize(640,480);
/* Create the window with title "DDA_Line" */
glutCreateWindow("Draw a line using DDA algorithm");
/* Initialize drawing colors */
Init();
/* Call the displaying function */
glutDisplayFunc(DDAline);
/* Keep displaying untill the program is closed */
glutMainLoop();
}