-
Notifications
You must be signed in to change notification settings - Fork 0
/
Line.cpp
149 lines (141 loc) · 3.23 KB
/
Line.cpp
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
148
149
#include "Line.h"
#include <stdlib.h>
#define SGN(a) (((a)<0) ? -1 : 1)
Line::Line(Location loc, int X2, int Y2) : x1(loc.getX()), y1(loc.getY()), x2(X2), y2(Y2)
{
//cerr << x1 << ',' << y1 << ',' << x2 << ',' << x2 << '#';
line = vector<Location>(); //this is a list of the spots that are in the line.
dx = x2 - x1; //change in x and y.
dy = y2 - y1;
ax = abs(dx) << 1; //absolute values of dx and dy multiplied by 2.
ay = abs(dy) << 1;
sx = SGN(dx); //signs of x and y.
sy = SGN(dy);
int t;
if (dy == 0) sy = 0;
if (dx == 0) sx = 0;
Location cur(loc); //current location.
int xmin = (x1 < x2 ? x1 : x2), ymin = (y1 < y2 ? y1 : y2),
xmax = (x1 > x2 ? x1 : x2), ymax = (y1 > y2 ? y1 : y2);
xmin -= abs(dx);
ymin -= abs(dy);
xmax += abs(dx);
ymax += abs(dy); //minimums and maximums for x and y.
if (ax == ay)
{
do
{
cur.move(sx, sy);
if (!cur.correct())
return;
addLocation(cur);
}
while ((cur.getX() != x2 || cur.getY() != y2) &&
(cur.getY() >= xmin && cur.getY() <= xmax && cur.getY() >= ymin && cur.getY() <= ymax));
}
else if (ax > ay)
{
t = ay - (ax >> 1);
do
{
if (t >= 0)
{
cur.move(0, sy);
t -= ax;
}
cur.move(sx, 0);
t += ay;
if (!cur.correct())
return;
addLocation(cur);
}
while ((cur.getX() != x2 || cur.getY() != y2) &&
(cur.getX() >= xmin && cur.getX() <= xmax && cur.getY() >= ymin && cur.getY() <= ymax));
}
else
{
t = ax - (ay >> 1);
do
{
if (t >= 0)
{
cur.move(sx, 0);
t -= ay;
}
cur.move(0, sy);
t += ax;
if (!cur.correct())
return;
addLocation(cur);
}
while ((cur.getX() != x2 || cur.getY() != y2) &&
(cur.getX() >= xmin && cur.getX() <= xmax && cur.getY() >= ymin && cur.getY() <= ymax));
}
}
int Line::getX(int index)
{
int x = x1;
int cur = 0;
if (ax >= ay)
{
return x + (index + 1) * sx;
}
else
{
//cerr << "!";
//getch();
int t;
t = ax - (ay >> 1);
do
{
if (t >= 0)
{
x += sx;
t -= ay;
}
t += ax;
cur++;
}
while (cur <= index);
}
return x;
}
int Line::getY(int index)
{
int y = y1;
int cur = 0;
if (ax <= ay)
{
return y + (index + 1) * sy;
}
else
{
//cerr << "!";
//getch();
int t;
t = ay - (ax >> 1);
do
{
if (t >= 0)
{
y += sy;
t -= ax;
}
t += ay;
cur++;
}
while (cur <= index);
}
return y;
}
void Line::cutOff(int index)
{
x2 = getX(index);
y2 = getY(index);
while (line.size() >= index)
line.pop_back();
}
Line::~Line()
{
//dtor
}