-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScreen.cpp
More file actions
83 lines (73 loc) · 1.94 KB
/
Screen.cpp
File metadata and controls
83 lines (73 loc) · 1.94 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
#include "Screen.h"
#include <iostream>
// this function guarantees consistent behavior in both positive and negative case, moreover, allows a tolerance at each boundary.
using std::lround;
void Screen::drawPixel(int i, int j) {
coreDrawPixel(i, j);
}
void Screen::drawPoint(Point p) {
int i = lround((p.x - left) / dx), j = lround((p.y - bottom) / dy);
bitmap.at<Vec3b>(i, j) = color.value();
}
void Screen::clear(Color color) {
for (int h = 0; h < height; h++) {
for (int w = 0; w < width; w++) {
bitmap.at<Vec3b>(h, w) = color.value();
}
}
}
void Screen::show() {
cv::imshow("Image", bitmap);
cv::waitKey();
}
void Screen::writeImage(const char *filename) {
cv::imwrite(filename, bitmap, std::vector < int > {0});
}
void Screen::coreDrawPixel(int i, int j) {
if (i < 0 || j < 0 || i >= width || j >= height) {
return;
}
bitmap.at<Vec3b>(height - 1 - j, i) = color.value();
}
void Screen::drawLine(Point p0, Point p1) {
if (p0.x > p1.x) {
drawLine(p1, p0);
return;
}
else {
if (abs(p1.y - p0.y) < abs(p1.x - p0.x)) {
Real k = (p1.y - p0.y) / (p1.x - p0.x);
int i0 = lround((p0.x - left) / dx), i1 = lround((p1.x - left) / dx);
Real j = (p0.y - bottom);
for (i0; i0 < i1; i0++) {
coreDrawPixel(i0, lround(j / dy));
j += k * dx;
}
}
else if (p1.y > p0.y) {
Real k = (p1.x - p0.x) / (p1.y - p0.y);
int i0 = lround((p0.y - bottom) / dy), i1 = lround((p1.y - bottom) / dy);
Real j = (p0.x - left);
for (i0; i0 < i1; i0++) {
coreDrawPixel(lround(j / dx), i0);
j += k * dy;
}
}
else {
Real k = (p0.x - p1.x) / (p0.y - p1.y);
int i0 = lround((p1.y - bottom) / dy), i1 = lround((p0.y - bottom) / dy);
Real j = (p1.x - left);
for (i0; i0 < i1; i0++) {
coreDrawPixel(lround(j / dx), i0);
j += k * dy;
}
}
}
}
void Screen::drawPolygon(const vector<Point>& p) {
int l = p.size();
for (int i = 0; i < l - 1; i++) {
drawLine(p[i], p[i + 1]);
}
drawLine(p[l - 1], p[0]);
}