Skip to content

Commit 6413261

Browse files
committed
Regularize internal names, add Triangle unit test
1 parent edf9b0d commit 6413261

File tree

2 files changed

+64
-18
lines changed

2 files changed

+64
-18
lines changed

modules/core/src/main/java/org/locationtech/jts/geom/Triangle.java

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,13 @@ public static HCoordinate perpendicularBisector(Coordinate a, Coordinate b)
113113
* @return the circumradius of the triangle
114114
*/
115115
public static double circumradius(Coordinate a, Coordinate b, Coordinate c) {
116-
double A = a.distance(b);
117-
double B = b.distance(c);
118-
double C = c.distance(a);
116+
double lenAB = a.distance(b);
117+
double lenBC = b.distance(c);
118+
double lenCA = c.distance(a);
119119
double area = area(a, b, c);
120120
if (area == 0.0)
121121
return Double.POSITIVE_INFINITY;
122-
return (A * B * C) / (4 * area);
122+
return (lenAB * lenBC * lenCA) / (4 * area);
123123
}
124124

125125
/**
@@ -262,7 +262,7 @@ private static double det(double m00, double m01, double m10, double m11)
262262
* the point at which the bisectors of the triangle's angles meet. It is the
263263
* centre of the triangle's <i>incircle</i>, which is the unique circle that
264264
* is tangent to each of the triangle's three sides
265-
* (and hence the maximum inscribed circle).
265+
* (and hence the Maximum Inscribed Circle).
266266
* <p>
267267
* The incentre always lies within the triangle.
268268
*
@@ -276,14 +276,13 @@ private static double det(double m00, double m01, double m10, double m11)
276276
*/
277277
public static Coordinate inCentre(Coordinate a, Coordinate b, Coordinate c)
278278
{
279-
// the lengths of the sides, labelled by their opposite vertex
280-
double len0 = b.distance(c);
281-
double len1 = a.distance(c);
282-
double len2 = a.distance(b);
283-
double circum = len0 + len1 + len2;
284-
285-
double inCentreX = (len0 * a.x + len1 * b.x + len2 * c.x) / circum;
286-
double inCentreY = (len0 * a.y + len1 * b.y + len2 * c.y) / circum;
279+
double lenAB = a.distance(b);
280+
double lenBC = b.distance(c);
281+
double lenCA = c.distance(a);
282+
double circum = lenBC + lenCA + lenAB;
283+
284+
double inCentreX = (lenBC * a.x + lenCA * b.x + lenAB * c.x) / circum;
285+
double inCentreY = (lenBC * a.y + lenCA * b.y + lenAB * c.y) / circum;
287286
return new Coordinate(inCentreX, inCentreY);
288287
}
289288

@@ -368,9 +367,9 @@ public static Coordinate angleBisector(Coordinate a, Coordinate b,
368367
* Uses the fact that the lengths of the parts of the split segment are
369368
* proportional to the lengths of the adjacent triangle sides
370369
*/
371-
double len0 = b.distance(a);
372-
double len2 = b.distance(c);
373-
double frac = len0 / (len0 + len2);
370+
double lenBA = b.distance(a);
371+
double lenBC = b.distance(c);
372+
double frac = lenBA / (lenBA + lenBC);
374373
double dx = c.x - a.x;
375374
double dy = c.y - a.y;
376375

@@ -393,8 +392,7 @@ public static Coordinate angleBisector(Coordinate a, Coordinate b,
393392
*/
394393
public static double area(Coordinate a, Coordinate b, Coordinate c)
395394
{
396-
return Math
397-
.abs(((c.x - a.x) * (b.y - a.y) - (b.x - a.x) * (c.y - a.y)) / 2);
395+
return Math.abs(((c.x - a.x) * (b.y - a.y) - (b.x - a.x) * (c.y - a.y)) / 2);
398396
}
399397

400398
/**

modules/core/src/test/java/org/locationtech/jts/geom/TriangleTest.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212
package org.locationtech.jts.geom;
1313

14+
import org.locationtech.jts.algorithm.Distance;
1415
import org.locationtech.jts.io.WKTReader;
1516

1617
import junit.framework.TestCase;
@@ -167,6 +168,24 @@ public void testCentroid() throws Exception
167168
(10.0 + 20.0 + 15.0) / 3.0, (10.0 + 10.0 + 20.0) / 3.0));
168169
}
169170

171+
public void testInCentre() throws Exception
172+
{
173+
// right triangle
174+
checkInCentre("POLYGON((10 10, 20 20, 20 10, 10 10))",
175+
new Coordinate(17.071067811865476, 12.928932188134524) );
176+
// CCW right tri
177+
checkInCentre("POLYGON((10 10, 20 10, 20 20, 10 10))",
178+
new Coordinate(17.071067811865476, 12.928932188134524) );
179+
// acute
180+
checkInCentre("POLYGON((10 10, 20 10, 15 20, 10 10))",
181+
new Coordinate(14.999999999999998, 13.090169943749475));
182+
// obtuse
183+
checkInCentre("POLYGON ((10 10, 20 10, 40 20, 10 10))",
184+
new Coordinate(19.63104841334295, 11.56290400148567));
185+
}
186+
187+
//==========================================================
188+
170189
public void checkCentroid(String wkt, Coordinate expectedValue)
171190
throws Exception
172191
{
@@ -185,6 +204,35 @@ public void checkCentroid(String wkt, Coordinate expectedValue)
185204
assertEquals(expectedValue.toString(), centroid.toString());
186205
}
187206

207+
public void checkInCentre(String wkt, Coordinate expectedValue)
208+
throws Exception
209+
{
210+
double tolerance = 0.00001;
211+
212+
Geometry g = reader.read(wkt);
213+
Coordinate[] pt = g.getCoordinates();
214+
215+
Coordinate centre = Triangle.inCentre(pt[0], pt[1], pt[2]);
216+
//System.out.println("(Static) centroid = " + centroid);
217+
checkEqualXY(expectedValue, centre, tolerance);
218+
checkEquidistantToEdges(centre, pt, tolerance);
219+
220+
// Test Instance version
221+
//
222+
Triangle t = new Triangle(pt[0], pt[1], pt[2]);
223+
Coordinate centreTri = t.inCentre();
224+
checkEqualXY(expectedValue, centreTri, tolerance);
225+
}
226+
227+
private void checkEquidistantToEdges(Coordinate centre, Coordinate[] pt, double tolerance) {
228+
//-- inCenter must be equidistant from edges
229+
double radius0 = Distance.pointToLinePerpendicular(centre, pt[0], pt[1]);
230+
double radius1 = Distance.pointToLinePerpendicular(centre, pt[1], pt[2]);
231+
double radius2 = Distance.pointToLinePerpendicular(centre, pt[2], pt[3]);
232+
assertEquals(radius0, radius1, tolerance);
233+
assertEquals(radius0, radius2, tolerance);
234+
}
235+
188236
public void checkCircumCentre(String wkt, Coordinate expectedValue)
189237
throws Exception
190238
{

0 commit comments

Comments
 (0)