-
-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathTEdge.cpp
More file actions
134 lines (107 loc) · 4.66 KB
/
TEdge.cpp
File metadata and controls
134 lines (107 loc) · 4.66 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
/*******************************************************************************
* *
* Author : Angus Johnson *
* Version : 5.1.6 *
* Date : 23 May 2013 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2013 *
* *
* License: *
* Use, modification & distribution is subject to Boost Software License Ver 1.*
* http://www.boost.org/LICENSE_1_0.txt *
* *
* Attributions: *
* The code in this library is an extension of Bala Vatti's clipping algorithm:*
* "A generic solution to polygon clipping" *
* Communications of the ACM, Vol 35, Issue 7 (July 1992) pp 56-63. *
* http://portal.acm.org/citation.cfm?id=129906 *
* *
* Computer graphics and geometric modeling: implementation and algorithms *
* By Max K. Agoston *
* Springer; 1 edition (January 4, 2005) *
* http://books.google.com/books?q=vatti+clipping+agoston *
* *
* See also: *
* "Polygon Offsetting by Computing Winding Numbers" *
* Paper no. DETC2005-85513 pp. 565-575 *
* ASME 2005 International Design Engineering Technical Conferences *
* and Computers and Information in Engineering Conference (IDETC/CIE2005) *
* September 24-28, 2005 , Long Beach, California, USA *
* http://www.me.berkeley.edu/~mcmains/pubs/DAC05OffsetPolygon.pdf *
* *
******************************************************************************/
#include "TEdge.h"
#include "Int128.h"
#include "Util.h"
#include "Clipper.h"
#include <cmath>
using namespace ClipperLib;
TEdge::TEdge(TEdge *next, TEdge *prev, const IntPoint &pt, PolyType polyType) :
xcurr(pt.X), ycurr(pt.Y), polyType(polyType), next(next), prev(prev) {
if (next->ycurr <= ycurr) {
xbot = xcurr;
ybot = ycurr;
xtop = next->xcurr;
ytop = next->ycurr;
windDelta = 1;
} else {
xtop = xcurr;
ytop = ycurr;
xbot = next->xcurr;
ybot = next->ycurr;
windDelta = -1;
}
deltaX = xtop - xbot;
deltaY = ytop - ybot;
if (deltaY == 0) dx = CLIPPER_HORIZONTAL;
else dx = (double)(deltaX) / deltaY;
outIdx = -1;
}
bool TEdge::IntersectPoint(TEdge &edge2, IntPoint &ip,
bool UseFullInt64Range) const {
double b1, b2;
if (SlopesEqual(edge2, UseFullInt64Range)) {
if (edge2.ybot > ybot) ip.Y = edge2.ybot;
else ip.Y = ybot;
return false;
}
if (CLIPPER_NEAR_ZERO(dx)) {
ip.X = xbot;
if (CLIPPER_NEAR_EQUAL(edge2.dx, CLIPPER_HORIZONTAL)) ip.Y = edge2.ybot;
else {
b2 = edge2.ybot - (edge2.xbot / edge2.dx);
ip.Y = Round(ip.X / edge2.dx + b2);
}
} else if (CLIPPER_NEAR_ZERO(edge2.dx)) {
ip.X = edge2.xbot;
if (CLIPPER_NEAR_EQUAL(dx, CLIPPER_HORIZONTAL)) ip.Y = ybot;
else {
b1 = ybot - (xbot / dx);
ip.Y = Round(ip.X / dx + b1);
}
} else {
b1 = xbot - ybot * dx;
b2 = edge2.xbot - edge2.ybot * edge2.dx;
double q = (b2 - b1) / (dx - edge2.dx);
ip.Y = Round(q);
if (std::fabs(dx) < std::fabs(edge2.dx))
ip.X = Round(dx * q + b1);
else ip.X = Round(edge2.dx * q + b2);
}
if (ip.Y < ytop || ip.Y < edge2.ytop) {
if (ytop > edge2.ytop) {
ip.X = xtop;
ip.Y = ytop;
return edge2.TopX(ytop) < xtop;
}
ip.X = edge2.xtop;
ip.Y = edge2.ytop;
return TopX(edge2.ytop) > edge2.xtop;
}
return true;
}
bool TEdge::SlopesEqual(const TEdge &e2, bool UseFullInt64Range) const {
if (UseFullInt64Range)
return Int128Mul(deltaY, e2.deltaX) == Int128Mul(deltaX, e2.deltaY);
return deltaY * e2.deltaX == deltaX * e2.deltaY;
}