-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTTAirPolygon.cpp
More file actions
101 lines (80 loc) · 1.73 KB
/
TTAirPolygon.cpp
File metadata and controls
101 lines (80 loc) · 1.73 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
/*
* TTAirPolygon.cpp
*
* Created on: 10 de jun de 2018
* Author: almer
*/
#include "TTAirPolygon.h"
namespace TTAIR {
TTAirPolygon::TTAirPolygon(){
}
TTAirPolygon::~TTAirPolygon(){
}
bool TTAirPolygon::contains(int x, int y) {
if (npoints <= 2) {
return false;
}
int hits = 0;
int lastx = xpoints[npoints - 1];
int lasty = ypoints[npoints - 1];
int curx, cury;
// Walk the edges of the polygon
for (int i = 0; i < npoints; lastx = curx, lasty = cury, i++) {
curx = xpoints[i];
cury = ypoints[i];
if (cury == lasty) {
continue;
}
int leftx;
if (curx < lastx) {
if (x >= lastx) {
continue;
}
leftx = curx;
} else {
if (x >= curx) {
continue;
}
leftx = lastx;
}
double test1, test2;
if (cury < lasty) {
if (y < cury || y >= lasty) {
continue;
}
if (x < leftx) {
hits++;
continue;
}
test1 = x - curx;
test2 = y - cury;
} else {
if (y < lasty || y >= cury) {
continue;
}
if (x < leftx) {
hits++;
continue;
}
test1 = x - lastx;
test2 = y - lasty;
}
if (test1 < (test2 / (lasty - cury) * (lastx - curx))) {
hits++;
}
}
Serial.println("CONTAINS?");
return ((hits & 1) != 0);
}
void TTAirPolygon::addPoint(int x, int y) {
if (npoints <MIN_LENGTH){
xpoints[npoints] = x;
ypoints[npoints] = y;
npoints++;
Serial.println("Adicionados: ");
Serial.println(npoints);
}else {
Serial.println("ERRO ADDPoint");
}
}
} /* namespace TTAIR */