-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.cpp
More file actions
executable file
·147 lines (122 loc) · 3.11 KB
/
Copy pathSource.cpp
File metadata and controls
executable file
·147 lines (122 loc) · 3.11 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;
struct Point
{
float x;
float y;
};
void writeImage(const char *filename, char *img, int width, int height)
{
FILE *file = fopen(filename, "wb");
fprintf(file, "P5\n%i %i\n255\n", width, height);
fwrite(img, sizeof(char), width * height, file);
}
int main()
{
int width = 400;
int height = 400;
//cin >> width >> height;
char *img = new char[width*height];
memset(img, 255, width*height);
Point p1, p2;
p1.x = 50;
p1.y = 90;
p2.x = 130;
p2.y = 60;
float thickness = 5.0;
/*cin >> start.x;
cin >> start.y;
cin >> end.x;
cin >> end.y;*/
int length_x = abs(p2.x - p1.x);
int length_y = abs(p2.y - p1.y);
int back = 255;
int line = 0;
float gback = pow(back / 255.0f, 2.2f);
float gline = pow(line / 255.0f, 2.2f);
if (length_x >= length_y) {
Point start = p1.x < p2.x ? p1 : p2;
Point end = p1.x < p2.x ? p2 : p1;
int direction = 1;
if (start.y > end.y) {
direction *= -1;
}
float h = (thickness / 2.0) / cos(atan2(length_y, length_x));
for (int i = 1; i <= length_x - 1; i++)
{
float realIndex = i + 0.5;
float y_f = (float)((realIndex-1)*length_y) / length_x + start.y;
float y_s = (float)(realIndex*length_y) / length_x + start.y;
float maxLine = y_f > y_s ? y_f : y_s;
float minLine = y_f < y_s ? y_f : y_s;
float maxUp = maxLine + h;
float minUp = minLine + h;
float maxDown = maxLine - h;
float minDown = minLine - h;
int iMaxUp = maxUp;
int iMinUp = minUp;
int iMaxDown = maxDown;
int iMinDown = minDown;
if (iMaxUp == iMinUp) {
cout << "Òðàïåöèÿ" << endl;
}
else {
cout << "Òðåóãîëüíèêè" << endl;
}
if (iMaxDown == iMinDown) {
cout << "Òðàïåöèÿ" << endl;
}
else {
cout << "Òðåóãîëüíèêè" << endl;
}
if (iMaxDown == iMinUp) {
}
int y = ceil(start.y);
int x = ceil(start.x);
int realX = i + 0.5;
for (int y_filling = maxDown+1; y_filling < minUp; y_filling++) {
img[y_filling * width + realX + x] = 0;
}
}
/*
for (int i = 0; i <= length_x; i++)
{
float y_f = (float)(i*length_y) / length_x;
int y = y_f;
float f = (y_f - y);
y *= direction;
if (f == 0)
img[(y + start.y)*width + i + start.x] = line;
else {
img[(y + start.y)*width + i + start.x] = pow(((1 - f)*gline + f*gback), 1 / 2.2f) * 255 + 0.5;
img[(y + 1 * direction + start.y)*width + i + start.x] = pow(((1 - f)*gback + f*gline), 1 / 2.2f) * 255 + 0.5;
}
}
}
else {
Point start = p1.y < p2.y ? p1 : p2;
Point end = p1.y < p2.y ? p2 : p1;
int direction = 1;
if (end.x < start.x) {
direction *= -1;
}
for (int i = 0; i <= length_y; i++)
{
float x_f = (float)(i*length_x) / length_y;
int x = x_f;
float f = (x_f - x);
x *= direction;
if (f == 0)
{
img[(i + start.y)*width + x + start.x] = line;
}
else {
img[(i + start.y)*width + x + start.x] = pow(((1 - f)*gline + f*gback), 1 / 2.2f) * 255 + 0.5;
img[(i + start.y)*width + x + 1 * direction + start.x] = pow(((1 - f)*gback + f*gline), 1 / 2.2f) * 255 + 0.5;
}
}*/
}
writeImage("myImage.pgm", img, width, height);
}