-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDDA.cpp
126 lines (102 loc) · 2.52 KB
/
DDA.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
115
116
117
118
119
120
121
122
123
124
125
126
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<GL/glut.h>
#include<math.h>
#define ROUND(x)((int)(x+0.5))
using namespace std;
int x1,x2,z1,z2;
void draw_pixel(int x, int y)
{
glColor3f(1.0,0.0,0.0);
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}
void drawline(int X1, int Y1, int X2, int Y2)
{
float x,y,dx,dy,length; int i;
dx=abs(X2-X1);
dy=abs(Y2-Y1);
if(dx>=dy)
length=dx;
else length=dy;
dx=(X2-X1)/length;
dy=(Y2-Y1)/length;
x=X1;
y=Y1;
i=1;
while(i<=length)
{
draw_pixel(ROUND(x),ROUND(y));
x=x+dx;
y=y+dy;
i=i+1;
}
glFlush();
}
void drawpatt(int ax, int ay, int bx, int by, int cx, int cy, int dx, int dy, int n)
{
int m1x, m1y, m2x, m2y, m3x, m3y, m4x, m4y;
drawline(ax,ay,bx,by);
drawline(bx,by,cx,cy);
drawline(cx,cy,dx,dy);
drawline(dx,dy,ax,ay);
//midpoint calculations;
m1x=(ax+bx)/2;
m1y=(ay+by)/2;
m2x=(bx+cx)/2;
m2y=(by+cy)/2;
m3x=(cx+dx)/2;
m3y=(cy+dy)/2;
m4x=(dx+ax)/2;
m4y=(dy+ay)/2;
n--; if(n!=0)
{
drawpatt(m1x, m1y, m2x, m2y, m3x, m3y, m4x, m4y,n);
}
}
void display(void)
{
float x,y,dx,dy,length;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINES);
glVertex2i(-200,0);
glVertex2i(200,0);
glVertex2i(0,-200);
glVertex2i(0,200);
glEnd();
drawpatt(x1,z1,x1,z2,x2,z2,x2,x1,5);
}
void Init(void)
{
glClearColor(1,1,1,0);
gluOrtho2D(-200.0,200.0,-200.0,200.0);
}
int main(int argc, char **argv)
{
/* cout<<"\n Enter the value of left bottom x1::"; cin<<x1;
cout<<"\n Enter the value of left bottom y1:"; cin<<y1;
cout<<"\n Enter the value of right top x2:"; cin<<x2;
cout<<"\n Enter the value of right top y2:"; cin<<y2; */
printf("Enter the value of left bottom x1:");
scanf("%d",&x1);
printf("Enter the value of left bottom y1:");
scanf("%d",&z1);
printf("Enter the value of right top x2:");
scanf("%d",&x2);
printf("Enter the value of right top y2:");
scanf("%d",&z2);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("Pattern Drawing");
Init();
glClear(GL_COLOR_BUFFER_BIT);
glutDisplayFunc(display);
glFlush();
glutMainLoop();
return 0;
}