-
Notifications
You must be signed in to change notification settings - Fork 0
/
exp4bresstairs.txt
152 lines (115 loc) · 2.93 KB
/
exp4bresstairs.txt
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include<stdio.h>
#include<math.h>
#include<GL/freeglut.h>
#include<GL/gl.h>
//GLOBAL VARIABLES
int xf1, yf1, xf2, yf2;
bool selected = false;
int l;
//FUNCTION DEFINITIONS
//plot functin to plot a pixel
void plot(int x, int y){
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
glFlush();
}//end of plot function
//bresenham line drawing algorihm
void bresenham(int x1, int y1, int x2, int y2){
int x = x1;
int y = y1;
int i;
int dx = x2 - x1;
int dy = y2 - y1;
int p = 2*dy - dx;
int x_inc = x2>=x1 ? 1 : -1;
int y_inc = y2>=y1 ? 1 : -1;
if(dx >= dy){
plot(x,y);
for(i=0;i<dx;i++){
if(p>0){
y = y + y_inc;
p = p - 2*dx;
}
x = x + x_inc;
p = p + 2*dy;
plot(x,y);
}
}//end of if
else{
plot(x,y);
for(i=0;i<dy;i++){
if(p>0){
x = x + x_inc;
p = p - 2*dy;
}
y = y + y_inc;
p = p + 2*dx;
plot(x,y);
}
}
glFlush();
}//end of bresenham function
//draw staircase pattern
void draw_pattern(){
int h = l/6;
int b = l/6;
glColor3f(1.0,1.0,1.0);
bresenham(xf1, yf1, xf1+l, yf1);
bresenham(xf1+l,yf1, xf1+l, yf1+l);
int x,y;
int i;
x = xf1;
y = yf1;
for(i=0;i<6;i++){
bresenham(x, y, x, y+h);
bresenham(x, y+h, x+b, y+h);
x = x + b;
y = y + h;
}
}//end of draw pattern functio
//intialize function to initialize buffer
void init(){
glClearColor(1.0,0.0,0.0,1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,640,0,480);
glFlush();
}//end of init function
//display function
void display(){
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}//end of display function
//mouse click function
void mouse(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
if(state == GLUT_DOWN){
if(selected == false){
xf1 = x;
yf1 = 480 - y;
glColor3f(0.0,0.0,0.0);
glPointSize(5);
plot(xf1,yf1);
glPointSize(2);
draw_pattern();
}//end of if select
}//end of if state
}//end of if button
glFlush();
}//end of mouse function
//main function
int main(int argc, char **argv){
printf("\nEnter lenght :: ");
scanf("%d", &l);
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); //intitializng diplay mode
glutInitWindowSize(640,480);//initializing window size
glutInitWindowPosition(100,100);//initializing window position
glutCreateWindow("Staircase");
init();
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}//end of main function