-
Notifications
You must be signed in to change notification settings - Fork 1
/
q5.cpp
114 lines (103 loc) · 1.78 KB
/
q5.cpp
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 <GL/glut.h>
#include <stdio.h>
#include <math.h>
#include <iostream>
using namespace std;
void init(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,30.0,0.0,30.0);
}
void setPixel(GLint x,GLint y)
{
cout<<x<<", "<<y<<endl;
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}
void line_draw_bresenham(int x0, int y0, int xn, int yn)
{
// Converting inverted co-ordinates of line if necessary.
if(xn <= x0 && yn <= y0)
{
swap(xn,x0);
swap(yn,y0);
}
else if(xn <= x0 && yn >= y0)
{
swap(xn,x0);
swap(yn,y0);
}
int dy = abs(yn - y0);
int dx = abs(xn - x0);
int x = x0;
int y = y0;
int iny = y0 < yn ? 1 : -1;
int inx = x0 < xn ? 1 : -1;
cout<<"Drawing Line from "<<x0<<", "<<y0<<" to "<<xn<<", "<<yn<<endl;
setPixel(x,y);
if(dy <= dx)
{
int twody = 2*dy;
int twodydx = 2*(dy - dx);
int pk = 2*dy - dx;
while(x != xn)
{
if(pk < 0)
{
pk = pk + twody;
}
else
{
y+=iny;
pk = pk + twodydx;
}
x+=inx;
setPixel(x,y);
}
}
else{
int twodx = 2*dx;
int twodxdy = 2*(dx - dy);
int pk = 2*dx - dy;
while(y != yn)
{
if(pk < 0)
{
pk = pk + twodx;
}
else
{
x+=inx;
pk = pk + twodxdy;
}
y+=iny;
setPixel(x,y);
}
}
}
void update()
{
// glTranslatef(8,8,0);
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(5.0f);
glColor3f( 1, 0, 0);
line_draw_bresenham(2,8,16,12);
glColor3f( 0, 1, 0);
line_draw_bresenham(2,8,6,16);
glColor3f( 0, 0, 1);
line_draw_bresenham(6,16,2,6);
glFlush();
}
int main(int argc,char **argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(200,100);
glutInitWindowSize(640,480);
glutCreateWindow("Tutorial-1 Q5");
init();
glutDisplayFunc( update );
glutMainLoop();
return 0;
}