-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.c
121 lines (109 loc) · 2.54 KB
/
draw.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
#include "draw.h"
void draw_color(int hex)
{
glColor4f(
bitrange(hex, 24, 8) / 256.0,
bitrange(hex, 16, 8) / 256.0,
bitrange(hex, 8, 8) / 256.0,
bitrange(hex, 0, 8) / 256.0
);
}
void draw_square(double x, double y, double size, int hex)
{
double points[8] = {
x, y,
x, y + size,
x + size, y + size,
x + size, y
};
draw_color(hex);
glBegin(GL_LINES);
for (int i = 0; i < 4; i++)
{
int x = i * 2;
int y = i * 2 + 1;
int tx = (((i + 1) * 2) % (4 * 2));
int ty = (((i + 1) * 2) % (4 * 2)) + 1;
glVertex2f(points[x], points[y]);
glVertex2f(points[tx], points[ty]);
}
glEnd();
}
void draw_circle(double x, double y, double size, int hex)
{
int H = 15;
double delta = PI * 2 / H;
double rad = 0;
draw_color(hex);
glBegin(GL_LINES);
for (int i = 0; i < H; i++)
{
glVertex2f(x + cos(rad) * size, y + sin(rad) * size);
glVertex2f(x + cos(rad + delta) * size, y + sin(rad + delta) * size);
rad += delta;
}
glEnd();
}
void draw_circle_ray(Circle *circle, Rect *rect)
{
double nx, ny;
vec2_rxdelta(rect, circle->cx, circle->cy, &nx, &ny);
glColor4f(1.0, 1.0, 1.0, 1.0);
glBegin(GL_LINES);
glVertex2f(nx, ny);
glVertex2f(circle->cx , circle->cy);
glEnd();
}
void draw_rect_ray(Rect *r1, Rect *r2)
{
double size = r1->size;
double x = r1->cx + size / 2;
double y = r1->cy + size / 2;
double nx, ny;
vec2_rxdelta(r2, x, y, &nx, &ny);
glColor4f(1.0, 1.0, 1.0, 1.0);
glBegin(GL_LINES);
glVertex2f(nx, ny);
glVertex2f(x, y);
glEnd();
}
void draw_rect_normal(Rect *r1, Rect *r2)
{
double s1 = r1->size;
double x1 = r1->cx;
double y1 = r1->cy;
double cx1 = x1 + s1 / 2;
double cy1 = y1 + s1 / 2;
double nx, ny;
vec2_rxdelta(r2, cx1, cy1, &nx, &ny);
double vec[2] = { nx - cx1, ny - cy1 };
glColor4f(1.0, 0.0, 0.0, 1.0);
glBegin(GL_LINES);
if (fabs(vec[0]) > fabs(vec[1]))
{
if (vec[0] < 0)
{
glVertex2f(nx, ny);
glVertex2f(nx + s1, ny);
}
else
{
glVertex2f(nx, ny);
glVertex2f(nx - s1, ny);
}
}
else if (fabs(vec[1]) > fabs(vec[0]))
{
if (vec[1] > 0)
{
glVertex2f(nx, ny);
glVertex2f(nx, ny - s1);
}
else
{
glVertex2f(nx, ny);
glVertex2f(nx, ny + s1);
}
}
glEnd();
}