-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathAnswer4.c
More file actions
119 lines (101 loc) · 2.83 KB
/
Answer4.c
File metadata and controls
119 lines (101 loc) · 2.83 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
115
116
117
118
#include <GL/glut.h>
#include <math.h>
//#include<iostream.h>
#include <stdlib.h>
#include<stdio.h>
float a, b, c, d,e,f;
int xf, yf,xf1,yf1;
void delay(unsigned int mseconds)
{
clock_t goal = mseconds + clock();
while (goal > clock())
;
}
void setpixel(GLint xCoordinate, GLint yCoordinate) {
glBegin(GL_POINTS);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex2i(xCoordinate, yCoordinate);
glEnd();
glFlush();
}
void DDA(int X1, int Y1, int X2, int Y2) {
int I;
float Length;
float X, Y, Xinc, Yinc;
Length = abs(X2 - X1);
if (abs(Y2 - Y1) > Length)
Length = abs(Y2 - Y1);
Xinc = (X2 - X1) / (Length);
Yinc = (Y2 - Y1) / (Length);
X = X1;
Y = Y1;
for (I = 0; I < Length; I++) {
setpixel((X + 0.5), (Y + 0.5));
X = X + Xinc;
Y = Y + Yinc;
}
}
void drawmyline() {
float thetaSpeed=0.05;
float theta = 0.050,theta2 = 0.05;
while (1) {
glClear(GL_COLOR_BUFFER_BIT);
// update theta anticlockwise rotation
theta = theta + thetaSpeed;
theta2 = theta2+0.2 + thetaSpeed;
// check overflow
if (theta >= (2.0 * 3.14159))
theta = theta - (2.0 * 3.14159);
if (theta2 >= (2.0 * 3.14159))
theta2 = theta2 - (2.0 * 3.14159);
// actual calculations..
xf = c + (int)((float)(a - c) * cos(theta))
- ((float)(b - d) * sin(theta));
yf = d + (int)((float)(a - c) * sin(theta))
+ ((float)(b - d) * cos(theta));
xf1 = e + (int)((float)(a - e) * cos(theta))
- ((float)(b - f) * sin(theta2));
yf1 = f + (int)((float)(a - e) * sin(theta))
+ ((float)(b - f) * cos(theta2));
xf1 = xf1 + (int)((float)(xf - xf1) * cos(theta2))
- ((float)(yf - yf1) * sin(theta2));
yf1 = yf1 + (int)((float)(xf - xf1) * sin(theta2))
+ ((float)(yf - yf1) * cos(theta2));
//printf("%d %d %d %d",c,d,e,f);
DDA(a, b, xf, yf);
DDA(xf, yf, xf1, yf1);
glFlush();
// creating a delay
// so that the point can be noticed
delay(1);
}
}
void init(void) {
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 300.0, 0.0, 300.0);
}
int main(int argc, char **argv) {
//printf("DDA\nEnter co-ordinates to draw line(Co-ordinates in between(0 - " "300))\n");
printf("X1 = ");
scanf("%f",&a);
printf("Y1 = ");
scanf("%f",&b);
printf("X2 = ");
scanf("%f",&c);
printf("Y2 = ");
scanf("%f",&d);
printf("X3 = ");
scanf("%f",&e);
printf("Y3 = ");
scanf("%f",&f);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(200, 200);
glutInitWindowPosition(0, 0);
glutCreateWindow("Digital Differential Analyzer Algorithm");
init();
glutDisplayFunc(drawmyline);
glutMainLoop();
return 0;
}