-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathAnswer1.c
72 lines (63 loc) · 1.46 KB
/
Answer1.c
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
// C program for DDA line generation
#include <stdio.h>
#include <GL/glut.h>
void myInit()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
// used to set up the view volume,
//GL_MODELVIEW can be used to set up viewing
//transformation
glMatrixMode(GL_PROJECTION);
// gluOrtho2D specifies the coordinates to
//be used with the viewport which defaults to the
//window size.
gluOrtho2D(0.0, 1000.0, 0.0, 1000.0);
}
int abs(int n)
{
return ((n > 0) ? n : (n * (-1)));
}
void draw_pixel(int x, int y)
{
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
void DDA(int X0, int Y0, int X1, int Y1)
{
int dx = X1 - X0;
int dy = Y1 - Y0;
int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);
float Xinc = dx / (float)steps;
float Yinc = dy / (float)steps;
float X = X0;
float Y = Y0;
for (int i = 0; i <= steps; i++)
{
// putpixel(X, Y, RED);
draw_pixel(X, Y);
X += Xinc;
Y += Yinc;
// delay(100);
}
}
void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
// glColor3f(1.0, 0.0, 0.0);
DDA(375, 200, 750, 650);
glEnd();
glFlush();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
// glutInitWindowSize(1920, 1280);
glutInitWindowSize(1000, 1000);
glutInitWindowPosition(0, 0);
glutCreateWindow("Bresenham's Line Drawing");
myInit();
glutDisplayFunc(myDisplay);
glutMainLoop();
}