[geos-commits] r3912 - in trunk: include/geos/geom src/geom tests/unit/geom
svn_geos at osgeo.org
svn_geos at osgeo.org
Thu Aug 22 00:07:06 PDT 2013
Author: strk
Date: 2013-08-22 00:07:06 -0700 (Thu, 22 Aug 2013)
New Revision: 3912
Modified:
trunk/include/geos/geom/Triangle.h
trunk/src/geom/Triangle.cpp
trunk/tests/unit/geom/TriangleTest.cpp
Log:
circumcentre() and det() methods added to class Triangle
Includes test for circumcentre()
Contributed by Vishal Tiwari
Modified: trunk/include/geos/geom/Triangle.h
===================================================================
--- trunk/include/geos/geom/Triangle.h 2013-08-22 06:35:35 UTC (rev 3911)
+++ trunk/include/geos/geom/Triangle.h 2013-08-22 07:07:06 UTC (rev 3912)
@@ -47,6 +47,43 @@
* @param resultPoint the point into which to write the inCentre of the triangle
*/
void inCentre(Coordinate& resultPoint);
+
+ /**
+ * Computes the circumcentre of a triangle. The circumcentre is the centre of
+ * the circumcircle, the smallest circle which encloses the triangle. It is
+ * also the common intersection point of the perpendicular bisectors of the
+ * sides of the triangle, and is the only point which has equal distance to
+ * all three vertices of the triangle.
+ * <p>
+ * The circumcentre does not necessarily lie within the triangle. For example,
+ * the circumcentre of an obtuse isoceles triangle lies outside the triangle.
+ * <p>
+ * This method uses an algorithm due to J.R.Shewchuk which uses normalization
+ * to the origin to improve the accuracy of computation. (See <i>Lecture Notes
+ * on Geometric Robustness</i>, Jonathan Richard Shewchuk, 1999).
+ *
+ * @param resultPoint the point into which to write the inCentre of the triangle
+ */
+ void circumcentre(Coordinate& resultPoint);
+
+private:
+
+ /**
+ * Computes the determinant of a 2x2 matrix. Uses standard double-precision
+ * arithmetic, so is susceptible to round-off error.
+ *
+ * @param m00
+ * the [0,0] entry of the matrix
+ * @param m01
+ * the [0,1] entry of the matrix
+ * @param m10
+ * the [1,0] entry of the matrix
+ * @param m11
+ * the [1,1] entry of the matrix
+ * @return the determinant
+ */
+ double det(double m00 , double m01 , double m10 , double m11) const;
+
};
Modified: trunk/src/geom/Triangle.cpp
===================================================================
--- trunk/src/geom/Triangle.cpp 2013-08-22 06:35:35 UTC (rev 3911)
+++ trunk/src/geom/Triangle.cpp 2013-08-22 07:07:06 UTC (rev 3912)
@@ -33,5 +33,30 @@
result = Coordinate(inCentreX, inCentreY);
}
+void Triangle::circumcentre(Coordinate& result)
+{
+ double cx = p2.x;
+ double cy = p2.y;
+ double ax = p0.x - cx;
+ double ay = p0.y - cy;
+ double bx = p1.x - cx;
+ double by = p1.y - cy;
+
+ double denom = 2 * det(ax,ay,bx,by);
+ double numx = det(ay , ax * ax + ay * ay , by , bx * bx + by * by);
+ double numy = det(ax, ax * ax + ay * ay, bx, bx * bx + by * by);
+
+ double ccx = cx - numx / denom;
+ double ccy = cy + numy / denom;
+
+ result = Coordinate(ccx,ccy);
+}
+
+double Triangle::det(double m00 , double m01 , double m10 , double m11) const
+{
+ return m00 * m11 - m01 * m10;
+}
+
+
} // namespace geos::geom
} // namespace geos
Modified: trunk/tests/unit/geom/TriangleTest.cpp
===================================================================
--- trunk/tests/unit/geom/TriangleTest.cpp 2013-08-22 06:35:35 UTC (rev 3911)
+++ trunk/tests/unit/geom/TriangleTest.cpp 2013-08-22 07:07:06 UTC (rev 3912)
@@ -117,4 +117,57 @@
ensure( center.y < 4.3 );
ensure( 0 != ISNAN( center.z ) );
}
+ // Test circumcentre()
+ template<>
+ template<>
+ void object::test<6>()
+ {
+ using geos::geom::Triangle;
+ using geos::geom::Coordinate;
+
+ Coordinate x1(5,7);
+ Coordinate x2(6,6);
+ Coordinate x3(2,-2);
+
+ Coordinate y1(3,3);
+ Coordinate y2(9,10);
+ Coordinate y3(6,7);
+
+ Coordinate a1(5,10);
+ Coordinate a2(11,23);
+ Coordinate a3(22,19);
+
+ Triangle t1(x1,x2,x3);
+
+ Triangle t2(y1,y2,y3);
+
+ Triangle t3(a1,a2,a3);
+
+ // For t1:
+ Coordinate c1(0,0);
+ t1.circumcentre(c1);
+ ensure_equals(c1.x ,2 );
+ ensure_equals(c1.y ,3 );
+ ensure( 0 != ISNAN( c1.z ) );
+
+ //For t2:
+ Coordinate c2(0,0);
+ t2.circumcentre(c2);
+ ensure_equals(c2.x ,30.5 );
+ ensure_equals(c2.y ,- 14.5 );
+ ensure( 0 != ISNAN( c2.z ) );
+
+
+ //For t3:
+ Coordinate c3(0,0);
+ t3.circumcentre(c3);
+ ensure( std::fabs(c3.x - 13.0) < 1 );
+ ensure( c3.y > 13.7 );
+ ensure( c3.y < 13.8 );
+ ensure( 0 != ISNAN( c3.z ) );
+ // cout << "CicumCenter of triangle ABC:: " << c1.x << " " << c1.y << endl;
+
+ // std::cout << "CicumCenter of triangle DEF:: " << c2.x << " " << c2.y << std::endl;
+ }
+
} // namespace tut
More information about the geos-commits
mailing list