-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshapes.c
144 lines (112 loc) · 3.55 KB
/
shapes.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "functions.h"
void line(bmp_file *bmp, set *drawset, pt2D A, pt2D B) {
if (A.x == B.x) {
horizontal_line(bmp, drawset, A, B);
} else {
if (A.y == B.y) {
vertical_line(bmp, drawset, A, B);
} else {
oblique_line(bmp, drawset, A, B);
}
}
}
void rectangle(bmp_file *bmp, set *drawset, pt2D A, uint width, uint height) {
pt2D B = {0, 0}, C = {0, 0}, D = {0, 0};
B.x = A.x;
B.y = A.y + width;
C.x = B.x + height;
C.y = B.y;
D.x = C.x;
D.y = A.y;
line(bmp, drawset, A, B);
line(bmp, drawset, B, C);
line(bmp, drawset, C, D);
line(bmp, drawset, D, A);
}
void triangle(bmp_file *bmp, set *drawset, pt2D A, pt2D B, pt2D C) {
line(bmp, drawset, A, B);
line(bmp, drawset, B, C);
line(bmp, drawset, C, A);
}
int check_pt(int y, int x, int width, int height) {
if (y >= 0 && y < width && x >= 0 && x < height)
return 1;
return 0;
}
void paint(bmp_file *bmp, set *drawset, pt2D A) {
int xstart = (int) A.x - drawset->l_w / 2;
int xfinal = (int) A.x + drawset->l_w / 2;
int ystart = (int) A.y - drawset->l_w / 2;
int yfinal = (int) A.y + drawset->l_w / 2;
for (int i = xstart; i <= xfinal; i++) {
for (int j = ystart; j <= yfinal; j++) {
if (check_pt(j, i, bmp->infoh.w, bmp->infoh.h) == 1) {
ch_clr(&bmp->pic.map[i][j], drawset->draw_clr);
}
}
}
}
void ch_clr(color *color0, color new_color) {
*color0 = new_color;
}
void vertical_line(bmp_file *bmp, set *drawset, pt2D A, pt2D B) {
uint start = 0, final = 0;
final = maxim(A.x, B.x);
if (A.x == final)
start = B.x;
else
start = A.x;
uint i = start;
for (; i <= final && i < bmp->infoh.h; i++) {
pt2D color_point = {A.y, i};
paint(bmp, drawset, color_point);
}
}
void horizontal_line(bmp_file *bmp, set *drawset, pt2D A, pt2D B) {
uint start = 0, final = 0;
final = maxim(A.y, B.y);
if (A.y == final)
start = B.y;
else
start = A.y;
uint i = start;
for (; i <= final && i < bmp->infoh.w; i++) {
pt2D color_point = {i, A.x};
paint(bmp, drawset, color_point);
}
}
void oblique_line(bmp_file *bmp, set *drawset, pt2D A, pt2D B) {
paint(bmp, drawset, A);
paint(bmp, drawset, B);
uint maxx = maxim(A.x, B.x);
uint maxy = maxim(A.y, B.y);
uint diffx = maxx - (A.x + B.x - maxx);
uint diffy = maxy - (A.y + B.y - maxy);
if (diffx == maxim(diffx, diffy)) {
uint final = maxx - 1;
uint start = A.x + B.x - maxx + 1;
int i = (int)start;
int m = (int)(B.x - A.x);
int n = (int)(B.y - A.y);
for (; i <= final && i < bmp->infoh.h; i++) {
int j = (n * (i - (int)A.x) + (int) A.y * m) / m;
if (j < bmp->infoh.w) {
pt2D color_point = {j, i};
paint(bmp, drawset, color_point);
}
}
} else {
uint final = maxy - 1;
uint start = A.y + B.y - maxy + 1;
int i = (int)start;
int m = (int)(B.x - A.x);
int n = (int)(B.y - A.y);
for (; i <= final && i < bmp->infoh.w; i++) {
int j = (m * (i - (int)A.y) + (int)A.x * n) / n;
if (j < bmp->infoh.h) {
pt2D color_point = {i, j};
paint(bmp, drawset, color_point);
}
}
}
}