-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
154 lines (126 loc) · 3.61 KB
/
main.c
File metadata and controls
154 lines (126 loc) · 3.61 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
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
153
154
#include <GL/glut.h>
#include <stdio.h>
void renderSpacedBitmapString(float x, float y, void *font, char *string) {
char *c;
int x1 = x;
for (c = string; *c != '\0'; c++) {
glRasterPos2f(x1, y);
glutBitmapCharacter(font, *c);
x1 = x1 + glutBitmapWidth(font, *c);
}
}
void markString(char *string, int x, int y, int x_offset, int y_offset) {
glColor3f(255.0, 0, 0.0); // red color
renderSpacedBitmapString(x+x_offset, y+y_offset, GLUT_BITMAP_HELVETICA_12, string);
glFlush();
}
void plotDivisionLines() {
glBegin(GL_LINES);
glVertex2d(-320, 0);
glVertex2d(320, 0);
glVertex2d(0, -240);
glVertex2d(0, 240);
glEnd();
}
void plotPoint(int x, int y, int x_offset, int y_offset) {
glBegin(GL_POINTS);
glVertex2d(x + x_offset, y + y_offset);
glEnd();
}
void plotLine(int start_x, int start_y, int end_x, int end_y) {
glBegin(GL_LINES);
glVertex2d(start_x, start_y);
glVertex2d(end_x, end_y);
glEnd();
}
void plotAtAllOctants(int x, int y, int x_offset, int y_offset) {
plotPoint(x, y, x_offset, y_offset);
plotPoint(y, x, x_offset, y_offset);
plotPoint(x, -y, x_offset, y_offset);
plotPoint(y, -x, x_offset, y_offset);
plotPoint(-x, y, x_offset, y_offset);
plotPoint(-y, x, x_offset, y_offset);
plotPoint(-x, -y, x_offset, y_offset);
plotPoint(-y, -x, x_offset, y_offset);
}
void plotCircle(int center_x, int center_y, int radius) {
int x_k = 0;
int y_k = radius;
int p_k = 1 - radius;
plotPoint(center_x, center_y, 0, 0);
plotAtAllOctants(x_k, y_k, center_x, center_y);
while(x_k <= y_k) {
if(p_k < 0) {
plotAtAllOctants(x_k + 1, y_k, center_x, center_y);
x_k += 1;
p_k += (2*x_k) + 1;
}
else{
plotAtAllOctants(x_k + 1, y_k - 1, center_x, center_y);
x_k += 1;
y_k += -1;
p_k += (2*x_k) - (2*y_k) + 1;
}
}
}
void display_figure() {
glClear(GL_COLOR_BUFFER_BIT);
plotCircle(0, 110, 30);
plotCircle(0, 20, 60);
// right-limb 1
plotLine(60, 40, 110, 20);
plotLine(110, 20, 140, 40);
// left-limb 1
plotLine(-60, 40, -110, 20);
plotLine(-110, 20, -140, 40);
// right-limb 2
plotLine(60, 0, 110, -20);
plotLine(110, -20, 140, 0);
// left-limb 2
plotLine(-60, 0, -110, -20);
plotLine(-110, -20, -140, 0);
// right-limb 3
plotLine(40, -30, 90, -50);
plotLine(90, -50, 120, -35);
// left-limb 3
plotLine(-40, -30, -90, -50);
plotLine(-90, -50, -120, -35);
// right-antenna
plotLine(15, 140, 30, 180);
// left-antenna
plotLine(-15, 140, -30, 180);
glFlush();
}
void display_circle() {
glClear(GL_COLOR_BUFFER_BIT);
plotDivisionLines();
plotCircle(0, 0, 80);
markString("C(0,0); R80", 0, 0, 5, 5);
glColor3f(0.0f, 0.0f, 0.0f);
plotCircle(160, 120, 40);
markString("C(160,120); R40", 160, 120, 5, 5);
glFlush();
}
void init() {
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0f, 0.0f, 0.0f);
glPointSize(4);
glLineWidth(4);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-320.0, 320.0, -240.0, 240.0);
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
// circle
// glutCreateWindow("Ex4a - Midpoint Circle");
// glutDisplayFunc(display_circle);
// figure
glutCreateWindow("Ex4b - Figure with Circles");
glutDisplayFunc(display_figure);
init();
glutMainLoop();
return 1;
}