forked from BabesGotByte/Graphics_LabCodes
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAnswer3b.c
122 lines (103 loc) · 2.71 KB
/
Answer3b.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
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
#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
#define PI 3.14159265
int xc, yc, r, angle;
float convert = 180.0 / PI;
void init2D(float r, float g, float b) {
glClearColor(r, g, b, 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, 600, 0.0, 600);
}
int abs(int x) {
return ((x > 0) ? x : (-1 * x));
}
void drawCircle(int x, int y, int angle) {
// 1st quad
int X = xc+x, Y = yc+y;
float num = Y - yc, dem = X - xc;
float ang = atan(num / dem) * convert;
if(ang > 45) ang = 45.0;
if (ang <= angle) {
glVertex2f(X, Y); // 0 - 45
}
X = xc+y, Y = yc+x;
int ang1 = 90 - ang;
if (ang1 <= angle) {
glVertex2f(X, Y); // 45 - 90
}
// 2nd quad
X = xc-y, Y = yc+x;
int ang2 = 90 + ang;
if (ang2 <= angle) {
glVertex2f(X, Y); // 90 - 135
}
X = xc-x, Y = yc+y;
int ang3 = 180 - ang;
if (ang3 <= angle) {
glVertex2f(X, Y); // 135 - 180
}
// 3rd quad
X = xc-x, Y = yc-y;
int ang4 = 180 + ang;
if (ang4 <= angle) {
glVertex2f(X, Y); // 180 - 225
}
X = xc-y, Y = yc-x;
int ang5 = 270 - ang;
if (ang5 < angle) {
glVertex2f(X, Y); // 225 - 270
}
// 4th quad
X = xc+y, Y = yc-x;
int ang6 = 270 + ang;
if (ang6 <= angle) {
glVertex2f(X, Y); // 270 - 315
}
X = xc+x, Y = yc-y;
int ang7 = 360 - ang;
if (ang7 <= angle) {
glVertex2f(X, Y); // 315 - 360
}
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
glColor3f(0.0, 0.7, 0.1);
glVertex2f(xc, yc);
glEnd();
glColor3f(1.0, 0.7, 0.0);
int x = r, y = 0;
int d = 1 - r;
glPointSize(3);
glBegin(GL_POINTS);
drawCircle(x, y, angle);
while (x >= y) {
y++;
if (d <= 0) {
// Mid-point is inside or on the perimeter
d = d + 2 * y + 1;
} else {
// Mid-point is outside the perimeter
x--;
d = d + 2 * y - 2 * x + 1;
}
drawCircle(x, y, angle);
}
glEnd();
glFlush();
}
void main(int argc, char *argv[]) {
glutInit(&argc,argv);
printf("Enter xc, yc, r, angle:");
scanf("%d %d %d %d", &xc, &yc, &r, &angle);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("Mid-point Algorithm");
init2D(0.1, 0.2, 0.3); // uncomment to draw in pixels otherwise use normalizaed(0-1) value for vertex functions
glutDisplayFunc(display);
glutMainLoop();
}