1 // File: GeomAPI_Lin2d.cpp
2 // Created: 29 May 2014
3 // Author: Artem ZHIDKOV
5 #include <GeomAPI_Lin2d.h>
6 #include <GeomAPI_Pnt2d.h>
8 #include <gp_Dir2d.hxx>
9 #include <gp_Lin2d.hxx>
10 #include <gp_Pnt2d.hxx>
13 #include <IntAna2d_AnaIntersection.hxx>
15 #define MY_LIN2D static_cast<gp_Lin2d*>(myImpl)
17 static gp_Lin2d* newLine2d(const double theStartX, const double theStartY, const double theEndX,
20 gp_XY aDir(theEndX - theStartX, theEndY - theStartY);
21 gp_Pnt2d aStart(theStartX, theStartY);
22 return new gp_Lin2d(aStart, gp_Dir2d(aDir));
25 GeomAPI_Lin2d::GeomAPI_Lin2d(const double theStartX, const double theStartY, const double theEndX,
27 : GeomAPI_Interface(newLine2d(theStartX, theStartY, theEndX, theEndY))
31 GeomAPI_Lin2d::GeomAPI_Lin2d(const boost::shared_ptr<GeomAPI_Pnt2d>& theStart,
32 const boost::shared_ptr<GeomAPI_Pnt2d>& theEnd)
33 : GeomAPI_Interface(newLine2d(theStart->x(), theStart->y(), theEnd->x(), theEnd->y()))
37 double GeomAPI_Lin2d::distance(const boost::shared_ptr<GeomAPI_Pnt2d>& theOther) const
39 return MY_LIN2D->Distance(theOther->impl<gp_Pnt2d>());
42 const boost::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::intersect(
43 const boost::shared_ptr<GeomAPI_Lin2d>& theLine) const
45 IntAna2d_AnaIntersection anInter(*MY_LIN2D, theLine->impl<gp_Lin2d>());
46 if (!anInter.IsDone() || anInter.IsEmpty())
47 return boost::shared_ptr<GeomAPI_Pnt2d>();
48 const gp_Pnt2d& aResult = anInter.Point(1).Value();
49 return boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aResult.X(), aResult.Y()));
52 const boost::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::project(
53 const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint) const
55 const gp_XY& aDir = MY_LIN2D->Direction().XY();
56 const gp_XY& aLoc = MY_LIN2D->Location().XY();
57 const gp_XY& aPnt = thePoint->impl<gp_Pnt2d>().XY();
58 double aParam = aDir.Dot(aPnt - aLoc);
60 gp_XY aResult = aLoc + aDir * aParam;
61 return boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aResult.X(), aResult.Y()));
64 bool GeomAPI_Lin2d::isRight(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint) const
66 const gp_XY& aDir = MY_LIN2D->Direction().XY();
67 const gp_XY& aLoc = MY_LIN2D->Location().XY();
68 const gp_XY& aPnt = thePoint->impl<gp_Pnt2d>().XY();
70 return aDir.Crossed(aPnt - aLoc) > 0;