-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpen.cpp
More file actions
127 lines (101 loc) · 2.8 KB
/
pen.cpp
File metadata and controls
127 lines (101 loc) · 2.8 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
#include "pen.h"
#include <iostream>
void drawLine(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {
int width = abs(x0 - x1);
int height = abs(y0 - y1);
if(x0 > x1) {
swap(x0, x1);
swap(y0, y1);
}
if(width > height)
{
for(int x = x0; x < x1; x++)
{
int y = y0 + ((x - x0) * height / width);
image.set(x, y, color);
}
}
else
{
for(int y = y0; y < y1; y++)
{
int x = x0 + ((y - y0) * width / height);
image.set(x, y, color);
}
}
// bool steep = false;
// if(abs(x0 - x1) < abs(y0 - y1)) {
// swap(x0, y0);
// swap(x1, y1);
// steep = true;
// }
// if(x0 > x1) {
// swap(x0, x1);
// swap(y0, y1);
// }
// for(int x = x0; x < x1; x++) {
// float t = (x - x0) / (float)(x1 - x0);
// int y = y0 * (1. - t) + y1 * t;
// if(steep) {
// image.set(y, x, color);
// } else {
// image.set(x, y, color);
// }
// }
// int deltaX = abs(x1 - x0);
// int deltaY = abs(y1 - y0);
// int error = 0;
// int deltaError = deltaY;
// int y = y0;
// for(int x = x0; x < x1; x++) {
// image.set(x, y, color);
// error += deltaError;
// if(2 * error >= deltaX) {
// y -= 1;
// error -= deltaX;
// }
// }
}
void drawRect(int x, int y, int width, int height, TGAImage &image, TGAColor color)
{
drawLine(x, y, width - x, y, image, color);
drawLine(x, y, x, height - y, image, color);
drawLine(width, y, width - x, height - y, image, color);
drawLine(x, height, width - x, height - y, image, color);
}
void drawSquare(int x, int y, int width, TGAImage &image, TGAColor color)
{
drawRect(x, y, width, width, image, color);
}
void drawTriangle(int x0, int y0, int x1, int y1, int x2, int y2, TGAImage &image, TGAColor color)
{
drawLine(x0, y0, x1, y1, image, color);
drawLine(x1, y1, x2, y2, image, color);
drawLine(x2, y2, x0, y0, image, color);
}
void drawCircle(int x1, int y1, int r, TGAImage &image, TGAColor color) {
int x = 0;
int y = r;
int delta = 1 - 2 * r;
int error = 0;
while(y >= 0) {
image.set(x1 + x, y1 + y, color);
image.set(x1 + x, y1 - y, color);
image.set(x1 - x, y1 + y, color);
image.set(x1 - x, y1 - y, color);
error = 2 * (delta + y) - 1;
if(delta < 0 && error <= 0) {
delta += 2 * ++x + 1;
continue;
}
error = 2 * (delta - x) - 1;
if(delta > 0 && error > 0) {
delta += 1 - 2 * --y;
continue;
}
else
x++;
delta += 2 * (x - y);
y--;
}
}