-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlineBres.cpp
More file actions
115 lines (109 loc) · 2.18 KB
/
lineBres.cpp
File metadata and controls
115 lines (109 loc) · 2.18 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <stdio.h>
#include <math.h>
#include <GL/glut.h>
#include<iostream>
using namespace std;
double X1, Y1, X2, Y2;
int round_value(float v)
{
return floor(v + 0.5);
}
int sign(double num)
{
int r;
if(num<0)
r=-1;
else
r=1;
}
void LineBresen(void)
{
double dx=abs(X2-X1),X,Y,p;
double dy=abs(Y2-Y1);
int s1,s2,change;
float a,b,p0;
/* Find out whether to increment x or y */
s1=sign(X2-X1);
s2=sign(Y2-Y1);
if(dy>dx)
{
dx+=dy;
dy=dx-dy;
dx-=dy;
change=1;
}
else
change=0;
p0=2*dy-dx;
a=2*dy;
b=2*dy-2*dx;
X=X1;
Y=Y1;
/* Clears buffers to preset values */
glClear(GL_COLOR_BUFFER_BIT);
/* Plot the points */
glBegin(GL_POINTS);
/* Plot the first point */
glVertex2d(X,Y);
int i;
/* For every step, find an intermediate vertex */
for(i=0;i<dx;i++)
{
if(p0<0){
if(change=1)
Y+=s2;
else
{
X+=s1;
p0=p0+a;
}
}
else{
Y+=s2;
X+=s1;
p=p+b;
}
glVertex2d(round_value(X), round_value(Y));
cout<<"x="<<round_value(X);
}
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);
}
int main(int argc, char **argv)
{
cout<<"Enter two end points of the line to be drawn:\n";
cout<<"\n************************************";
cout<<"\nEnter Point1( X1 , Y1):\n";
cin>>X1;
cin>>Y1;
cout<<"\n************************************";
cout<<"\nEnter Point1( X2 , Y2):\n";
cin>>X2;
cin>>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("Line Drawing using DDA");
/* Initialize drawing colors */
Init();
/* Call the displaying function */
glutDisplayFunc(LineBresen);
/* Keep displaying untill the program is closed */
glutMainLoop();
}