Part: add function GetCircleCenter from CommandCreateGeo.cpp of the Sketcher module to the Geom2dCircle class in the Part module

This commit is contained in:
wmayer
2021-12-17 13:01:59 +01:00
parent 47b064e7fd
commit 955fe455cb
2 changed files with 48 additions and 0 deletions

View File

@@ -909,6 +909,51 @@ PyObject *Geom2dCircle::getPyObject(void)
return new Circle2dPy(static_cast<Geom2dCircle*>(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)

View File

@@ -43,6 +43,7 @@
#include <memory>
#include <Base/Persistence.h>
#include <Base/Tools2D.h>
#include <Mod/Part/PartGlobal.h>
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;
};