diff --git a/src/Mod/Part/App/Geometry2d.cpp b/src/Mod/Part/App/Geometry2d.cpp index 4d2da2c7d7..59646bede5 100644 --- a/src/Mod/Part/App/Geometry2d.cpp +++ b/src/Mod/Part/App/Geometry2d.cpp @@ -909,6 +909,51 @@ PyObject *Geom2dCircle::getPyObject(void) return new Circle2dPy(static_cast(this->clone())); } +/* +Find the centerpoint of a circle drawn through any 3 points: + +Given points p1-3, draw 2 lines: S12 and S23 which each connect two points. From the +midpoint of each line, draw a perpendicular line (S12p/S23p) across the circle. These +lines will cross at the centerpoint. + +Mathematically, line S12 will have a slope of m12 which can be determined. Therefore, +the slope m12p is -1/m12. Line S12p will have an equation of y = m12p*x + b12p. b12p can +be solved for using the midpoint of the line. This can be done for both lines. Since +both S12p and S23p cross at the centerpoint, solving the two equations together will give +the location of the centerpoint. +*/ +Base::Vector2d Geom2dCircle::getCircleCenter (const Base::Vector2d &p1, const Base::Vector2d &p2, const Base::Vector2d &p3) +{ + Base::Vector2d u = p2-p1; + Base::Vector2d v = p3-p2; + Base::Vector2d w = p1-p3; + + double uu = u*u; + double vv = v*v; + double ww = w*w; + + if (uu * vv * ww == 0) + THROWM(Base::ValueError,"Two points are coincident"); + + double uv = -(u*v); + double vw = -(v*w); + double uw = -(u*w); + + double w0 = (2 * sqrt(uu * ww - uw * uw) * uw / (uu * ww)); + double w1 = (2 * sqrt(uu * vv - uv * uv) * uv / (uu * vv)); + double w2 = (2 * sqrt(vv * ww - vw * vw) * vw / (vv * ww)); + + double wx = w0 + w1 + w2; + + if( wx == 0) + THROWM(Base::ValueError,"Points are collinear"); + + double x = (w0*p1.x + w1*p2.x + w2*p3.x)/wx; + double y = (w0*p1.y + w1*p2.y + w2*p3.y)/wx; + + return Base::Vector2d(x, y); +} + // ------------------------------------------------- TYPESYSTEM_SOURCE(Part::Geom2dArcOfCircle, Part::Geom2dArcOfConic) diff --git a/src/Mod/Part/App/Geometry2d.h b/src/Mod/Part/App/Geometry2d.h index 183f5a7e2a..358169f03d 100644 --- a/src/Mod/Part/App/Geometry2d.h +++ b/src/Mod/Part/App/Geometry2d.h @@ -43,6 +43,7 @@ #include #include #include +#include namespace Part { @@ -267,6 +268,8 @@ public: const Handle(Geom2d_Geometry)& handle() const; + static Base::Vector2d getCircleCenter (const Base::Vector2d &p1, const Base::Vector2d &p2, const Base::Vector2d &p3); + private: Handle(Geom2d_Circle) myCurve; };