forked from jbuckmccready/CavalierContours
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasicpolylinefunctions.cpp
45 lines (37 loc) · 1.64 KB
/
basicpolylinefunctions.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
#include "cavc/polyline.hpp"
using namespace cavc;
int main(int argc, char *argv[]) {
(void)argc;
(void)argv;
// closed polyline representing a circle going counter clockwise
double radius = 10.0;
Polyline<double> ccwCircle;
ccwCircle.addVertex(0, 0, 1);
ccwCircle.addVertex(2.0 * radius, 0, 1);
ccwCircle.isClosed() = true;
// closed polyline representing a circle going clockwise
Polyline<double> cwCircle;
cwCircle.addVertex(0, 0, -1);
cwCircle.addVertex(2.0 * radius, 0, -1);
cwCircle.isClosed() = true;
// path length of polyline
double length = getPathLength(ccwCircle);
assert(utils::fuzzyEqual(length, 2.0 * utils::pi<double>() * radius));
// signed area of closed polyline (area is positive if counter clockwise, negative if clockwise)
double area = getArea(ccwCircle);
assert(utils::fuzzyEqual(area, utils::pi<double>() * radius * radius));
assert(utils::fuzzyEqual(getArea(cwCircle), -area));
// polyline extents
AABB<double> extents = getExtents(ccwCircle);
assert(utils::fuzzyEqual(extents.xMin, 0.0));
assert(utils::fuzzyEqual(extents.yMin, -radius));
assert(utils::fuzzyEqual(extents.xMax, 2.0 * radius));
assert(utils::fuzzyEqual(extents.yMax, radius));
// Closest point on polyline to a point given
ClosestPoint<double> closestPoint(ccwCircle, Vector2<double>(radius, 10.0 * radius));
// index is the starting vertex index of the closest segment (going clockwise so arc starting at
// the second vertex is closest)
assert(closestPoint.index() == 1);
assert(fuzzyEqual(closestPoint.point(), Vector2<double>(radius, radius)));
assert(utils::fuzzyEqual(closestPoint.distance(), 9.0 * radius));
}