-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw_functions.c
109 lines (94 loc) · 3.24 KB
/
draw_functions.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
#include "utils.h"
int min(int a, int b) {
return (a < b) ? a : b;
}
int max(int a, int b) {
return (a > b) ? a : b;
}
// umplerea patratului de latura width din jurul unui pixel principal (point P)
void fill_rect(pen_info pen, unsigned **bitmap, bmp_infoheader ih, point P) {
for (int i = P.x - (pen.width / 2); i <= P.x + (pen.width / 2); ++i) {
for (int j = P.y - (pen.width / 2); j <= P.y + (pen.width / 2); ++j) {
if (i >= 0 && j >= 0 && i < ih.height && j < ih.width) {
bitmap[i][j] = pen.color;
}
if (j >= ih.width) {
break; // stop daca iese din limitele bitmap-ului
}
if (j < 0) {
j = -1; // sare peste pixelii
}
}
if (i >= ih.height) {
break;
}
if (i < 0) {
i = -1;
}
}
}
void draw_line(pen_info pen, unsigned int **bitmap, bmp_infoheader ih) {
point A = pen.A;
point B = pen.B;
point current_point; // pixelul principal curent
unsigned int max_interval = 0;
/* determinarea intervalului maxim
(x max - x min) sau (y max - y min) */
max_interval = abs(A.x - B.x);
if (max_interval < abs(A.y - B.y)) {
max_interval = abs(A.y - B.y);
}
/* determina coordonatele pixelilor principali
si umple patratele din jurul acestora */
if (max_interval == abs(A.x - B.x)) { // intervalul maxim e pe axa x
int minn = min(A.x, B.x);
int maxx = max(A.x, B.x);
for (int i = minn; i <= maxx; ++i) {
current_point.x = i;
current_point.y = (i - A.x) * (B.y - A.y) + A.y * (B.x - A.x);
current_point.y /= (B.x - A.x);
fill_rect(pen, bitmap, ih, current_point);
}
} else { // intervalul maxim e pe axa y
int minn = min(A.y, B.y);
int maxx = max(A.y, B.y);
for (int i = minn; i <= maxx; ++i) {
current_point.y = i;
current_point.x = (i - A.y) * (B.x - A.x) + A.x * (B.y - A.y);
current_point.x /= (B.y - A.y);
fill_rect(pen, bitmap, ih, current_point);
}
}
}
void draw_tri(pen_info pen, unsigned int **bitmap, bmp_infoheader ih) {
point A, B, C; // punctele triunghiului
scanf("%d%d", &A.y, &A.x);
scanf("%d%d", &B.y, &B.x);
scanf("%d%d", &C.y, &C.x);
// deseneaza laturile triunghiului
pen.A = A, pen.B = B;
draw_line(pen, bitmap, ih);
pen.A = A, pen.B = C;
draw_line(pen, bitmap, ih);
pen.A = B, pen.B = C;
draw_line(pen, bitmap, ih);
}
void draw_rect(pen_info pen, unsigned int **bitmap, bmp_infoheader ih) {
point A, B, C, D; // punctele dreptunghiului
int rect_width = 0, rect_height = 0;
scanf("%d%d", &A.y, &A.x);
scanf("%d%d", &rect_width, &rect_height);
// afla coordonatele celorlalte puncte
B.x = A.x, B.y = A.y + rect_width;
C.x = A.x + rect_height, C.y = A.y;
D.x = A.x + rect_height, D.y = A.y + rect_width;
// deseneaza laturile dreptunghiului
pen.A = A, pen.B = B;
draw_line(pen, bitmap, ih);
pen.A = A, pen.B = C;
draw_line(pen, bitmap, ih);
pen.A = B, pen.B = D;
draw_line(pen, bitmap, ih);
pen.A = C, pen.B = D;
draw_line(pen, bitmap, ih);
}