Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
circle-test.cpp
Go to the documentation of this file.
1/*
5 * Authors:
6 * Krzysztof KosiƄski <tweenk.pl@gmail.com>
7 *
8 * Copyright 2015 Authors
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it either under the terms of the GNU Lesser General Public
12 * License version 2.1 as published by the Free Software Foundation
13 * (the "LGPL") or, at your option, under the terms of the Mozilla
14 * Public License Version 1.1 (the "MPL"). If you do not alter this
15 * notice, a recipient may use your version of this file under either
16 * the MPL or the LGPL.
17 *
18 * You should have received a copy of the LGPL along with this library
19 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * You should have received a copy of the MPL along with this library
22 * in the file COPYING-MPL-1.1
23 *
24 * The contents of this file are subject to the Mozilla Public License
25 * Version 1.1 (the "License"); you may not use this file except in
26 * compliance with the License. You may obtain a copy of the License at
27 * http://www.mozilla.org/MPL/
28 *
29 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
30 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
31 * the specific language governing rights and limitations.
32 */
33
34#include "testing.h"
35#include <2geom/circle.h>
36#include <2geom/line.h>
37
38using namespace Geom;
39
40TEST(CircleTest, Equality) {
41 Circle a(4, 5, 6);
42 Circle b(Point(4, 5), 6);
43 Circle c(4.00000001, 5, 6);
44
45 EXPECT_EQ(a, b);
46 EXPECT_NE(a, c);
47 EXPECT_NE(b, c);
48}
49
50TEST(CircleTest, Nearness) {
51 Circle a(4, 5, 6);
52 Circle b(4.000007, 5, 6);
53 Circle c(4, 5, 6.000007);
54 Circle d(4.000007, 5, 6.000007);
55 Circle e(4, 5, 7);
56
57 EXPECT_TRUE(are_near(a, b, 1e-5));
58 EXPECT_TRUE(are_near(a, c, 1e-5));
59 EXPECT_TRUE(are_near(c, d, 1e-5));
60 EXPECT_FALSE(are_near(a, d, 1e-5));
61 EXPECT_FALSE(are_near(a, e, 1e-2));
62 EXPECT_FALSE(are_near(b, e, 1e-2));
63 EXPECT_FALSE(are_near(c, e, 1e-2));
64}
65
66TEST(CircleTest, UnitCircleTransform) {
67 Circle c(17, 23, 22);
68
69 Point q = c.pointAt(M_PI/2);
70 Point p = Point(0, 1) * c.unitCircleTransform();
71 Point r = q * c.inverseUnitCircleTransform();
72
73 EXPECT_FLOAT_EQ(p[X], q[X]);
74 EXPECT_FLOAT_EQ(p[Y], q[Y]);
75 EXPECT_FLOAT_EQ(r[X], 0);
76 EXPECT_FLOAT_EQ(r[Y], 1);
77}
78
79TEST(CircleTest, Coefficients) {
80 Circle circ(5, 12, 87), circ2;
81
82 Coord a, b, c, d;
83 circ.coefficients(a, b, c, d);
84 circ2.setCoefficients(a, b, c, d);
85
86 EXPECT_TRUE(are_near(circ, circ2, 1e-15));
87
88 for (unsigned i = 0; i < 100; ++i) {
89 Coord t = -5 + 0.111 * i;
90 Point p = circ.pointAt(t);
91 Coord eqres = a * p[X]*p[X] + a*p[Y]*p[Y] + b*p[X] + c*p[Y] + d;
92 EXPECT_NEAR(eqres, 0, 1e-11);
93 }
94}
95
96TEST(CircleTest, CircleIntersection) {
97 Circle a(5, 5, 5), b(15, 5, 5), c(10, 10, 6), d(-5, 5, 2);
98 std::vector<ShapeIntersection> r1, r2, r3;
99
100 r1 = a.intersect(b);
101 ASSERT_EQ(r1.size(), 1u);
102 EXPECT_EQ(r1[0].point(), Point(10,5));
103 EXPECT_intersections_valid(a, b, r1, 1e-15);
104
105 r2 = a.intersect(c);
106 EXPECT_EQ(r2.size(), 2u);
107 EXPECT_intersections_valid(a, c, r2, 1e-15);
108
109 r3 = b.intersect(c);
110 EXPECT_EQ(r3.size(), 2u);
111 EXPECT_intersections_valid(b, c, r3, 4e-15);
112
113 EXPECT_TRUE(a.intersect(d).empty());
114 EXPECT_TRUE(b.intersect(d).empty());
115 EXPECT_TRUE(c.intersect(d).empty());
116}
117
118TEST(CircleTest, LineIntersection) {
119 Circle c(5, 5, 10);
120 Line l1(Point(-5, -20), Point(-5, 20));
121 Line l2(Point(0, 0), Point(10, 2.3));
122 Line l3(Point(20, -20), Point(0, -20));
123
124 EXPECT_TRUE(c.intersects(l1));
125 EXPECT_TRUE(c.intersects(l2));
126 EXPECT_FALSE(c.intersects(l3));
127
128 std::vector<ShapeIntersection> r1, r2, r3;
129
130 r1 = c.intersect(l1);
131 ASSERT_EQ(r1.size(), 1u);
132 EXPECT_EQ(r1[0].point(), Point(-5, 5));
133 EXPECT_intersections_valid(c, l1, r1, 1e-15);
134
135 r2 = c.intersect(l2);
136 EXPECT_EQ(r2.size(), 2u);
137 EXPECT_intersections_valid(c, l2, r2, 1e-14);
138
139 r3 = c.intersect(l3);
140 EXPECT_TRUE(r3.empty());
141}
Circle shape.
Set of all points at a fixed distance from the center.
Definition circle.h:55
void coefficients(Coord &A, Coord &B, Coord &C, Coord &D) const
Definition circle.cpp:72
void setCoefficients(Coord A, Coord B, Coord C, Coord D)
Definition circle.cpp:49
Infinite line on a plane.
Definition line.h:53
Two-dimensional point that doubles as a vector.
Definition point.h:66
double c[8][4]
double Coord
Floating point type used to store coordinates.
Definition coord.h:76
@ Y
Definition coord.h:48
@ X
Definition coord.h:48
Infinite straight line.
Various utility functions.
Definition affine.h:22
TEST(AffineTest, Equality)
bool are_near(Affine const &a1, Affine const &a2, Coord eps=EPSILON)