Jpp 19.3.0-rc.3
the software that should make you happy
Loading...
Searching...
No Matches
JGEOMETRY2D::JSegment2D Class Reference

Line segment in two dimensions. More...

#include <JSegment2D.hh>

Inheritance diagram for JGEOMETRY2D::JSegment2D:
std::pair< JFirst_t, JSecond_t >

Public Member Functions

 JSegment2D ()
 Default constructor.
 
 JSegment2D (const JVector2D &A, const JVector2D &B)
 Constructor.
 
double getLengthSquared () const
 Get length squared.
 
double getLength () const
 Get length.
 
bool intersects (const JSegment2D &segment) const
 Test whether two line segments intersect.
 
JVector2D getIntersection (const JSegment2D &segment) const
 Get intersection of two line segments.
 
double getDistanceSquared (const JVector2D &point) const
 Get squared of distance to point.
 
double getDistance (const JVector2D &point) const
 Get distance to point.
 
double getDot (const JSegment2D &segment) const
 Get dot product.
 

Friends

JReaderoperator>> (JReader &in, JSegment2D &segment)
 Read segment from input.
 
JWriteroperator<< (JWriter &out, const JSegment2D &segment)
 Write segment to output.
 

Detailed Description

Line segment in two dimensions.

Definition at line 36 of file JSegment2D.hh.

Constructor & Destructor Documentation

◆ JSegment2D() [1/2]

JGEOMETRY2D::JSegment2D::JSegment2D ( )
inline

Default constructor.

Definition at line 43 of file JSegment2D.hh.

43 :
45 {}
std::pair< JPosition2D, JPosition2D > JSegment2D_t
Type definition of line segment in two dimensions.
Definition JSegment2D.hh:30

◆ JSegment2D() [2/2]

JGEOMETRY2D::JSegment2D::JSegment2D ( const JVector2D & A,
const JVector2D & B )
inline

Constructor.

Parameters
Astart position
Bend position

Definition at line 54 of file JSegment2D.hh.

55 :
56 JSegment2D_t(A,B)
57 {}

Member Function Documentation

◆ getLengthSquared()

double JGEOMETRY2D::JSegment2D::getLengthSquared ( ) const
inline

Get length squared.

Returns
square of length

Definition at line 65 of file JSegment2D.hh.

66 {
67 return JVector2D(this->second - this->first).getLengthSquared();
68 }

◆ getLength()

double JGEOMETRY2D::JSegment2D::getLength ( ) const
inline

Get length.

Returns
length

Definition at line 76 of file JSegment2D.hh.

77 {
78 return sqrt(getLengthSquared());
79 }
double getLengthSquared() const
Get length squared.
Definition JSegment2D.hh:65

◆ intersects()

bool JGEOMETRY2D::JSegment2D::intersects ( const JSegment2D & segment) const
inline

Test whether two line segments intersect.

Parameters
segmentline segment
Returns
true if two line segment intersect; else false

Definition at line 88 of file JSegment2D.hh.

89 {
90 return (getCCW(this->first, segment.first, this->second) != getCCW(this->first, segment.second, this->second) &&
91 getCCW(segment.first, this->first, segment.second) != getCCW(segment.first, this->second, segment.second));
92 }
bool getCCW(const T &a, const T &b, const T &c)
Check sequence of three points in X-Y plane.

◆ getIntersection()

JVector2D JGEOMETRY2D::JSegment2D::getIntersection ( const JSegment2D & segment) const
inline

Get intersection of two line segments.

Parameters
segmentline segment
Returns
intersection point

Definition at line 101 of file JSegment2D.hh.

102 {
103 JVector2D da(this->second - this->first);
104 JVector2D db(segment.second - segment.first);
105
106 const double gp = JMATH::getPerpDot(da, db);
107
108 if (gp != 0.0) {
109
110 da.mul(JMATH::getPerpDot(segment.second, segment.first));
111 db.mul(JMATH::getPerpDot(this->second, this->first));
112
113 db.sub(da);
114 db.div(gp);
115
116 return db;
117
118 } else {
119 throw JDivisionByZero("JSegment2D::getIntersection()");
120 }
121 }
double getPerpDot(const JFirst_t &first, const JSecond_t &second)
Get perpendicular dot product of objects.

◆ getDistanceSquared()

double JGEOMETRY2D::JSegment2D::getDistanceSquared ( const JVector2D & point) const
inline

Get squared of distance to point.

Parameters
pointpoint
Returns
square of distance

Definition at line 130 of file JSegment2D.hh.

131 {
132 JVector2D D(this->second - this->first);
133
134 const double gp = D.getLengthSquared();
135
136 if (gp != 0.0) {
137
138 const JVector2D U(point - this->first);
139
140 double u = D.getDot(U);
141
142 if (u < 0.0)
143 u = 0.0;
144 else if (u > gp)
145 u = 1.0;
146 else
147 u /= gp;
148
149 D.mul(u);
150 D.sub(U);
151
152 return D.getLengthSquared();
153
154 } else {
155
156 return first.getDistanceSquared(point);
157 }
158 }

◆ getDistance()

double JGEOMETRY2D::JSegment2D::getDistance ( const JVector2D & point) const
inline

Get distance to point.

Parameters
pointpoint
Returns
distance

Definition at line 167 of file JSegment2D.hh.

168 {
169 return sqrt(getDistanceSquared(point));
170 }
double getDistanceSquared(const JVector2D &point) const
Get squared of distance to point.

◆ getDot()

double JGEOMETRY2D::JSegment2D::getDot ( const JSegment2D & segment) const
inline

Get dot product.

Parameters
segmentsegment
Returns
dot product

Definition at line 179 of file JSegment2D.hh.

180 {
181 return JVector2D(this->second - this->first).getDot(segment.second - segment.first);
182 }

Friends And Related Symbol Documentation

◆ operator>>

JReader & operator>> ( JReader & in,
JSegment2D & segment )
friend

Read segment from input.

Parameters
inreader
segmentsegment
Returns
reader

Definition at line 192 of file JSegment2D.hh.

193 {
194 in >> segment.first;
195 in >> segment.second;
196
197 return in;
198 }

◆ operator<<

JWriter & operator<< ( JWriter & out,
const JSegment2D & segment )
friend

Write segment to output.

Parameters
outwriter
segmentsegment
Returns
writer

Definition at line 208 of file JSegment2D.hh.

209 {
210 out << segment.first;
211 out << segment.second;
212
213 return out;
214 }

The documentation for this class was generated from the following file: