OpenVSP API Documentation 3.52.0
Loading...
Searching...
No Matches
Vec2D Functions

API functions that utilize the vec2d class are grouped here. For details of the class, including member functions, see vec2d. vec2d is used for the two dimensional problems that come up inside three dimensional ones – parameter space coordinates, projections into a plane, and polygon tests. Click here to return to the main page. More...

Classes

class  vec2d

Functions

 vec2d::vec2d (double xx, double yy)
double & vec2d::operator[] (int i)
vec2dvec2d::set_xy (double xx, double yy)
vec2dvec2d::set_x (double xx)
vec2dvec2d::set_y (double yy)
double vec2d::x () const
double vec2d::y () const
double vec2d::mag () const
void vec2d::normalize ()

Friends

vec2d vec2d::operator+ (const vec2d &a, const vec2d &b)
vec2d vec2d::operator- (const vec2d &a, const vec2d &b)
vec2d vec2d::operator* (const vec2d &a, double b)
vec2d vec2d::operator* (const vec2d &a, const vec2d &b)
vec2d vec2d::operator/ (const vec2d &a, double b)
double vec2d::dist (const vec2d &a, const vec2d &b)
double vec2d::dist_squared (const vec2d &a, const vec2d &b)
double vec2d::cross (const vec2d &a, const vec2d &b)
double vec2d::dot (const vec2d &a, const vec2d &b)
double vec2d::angle (const vec2d &a, const vec2d &b)
double vec2d::cos_angle (const vec2d &a, const vec2d &b)
int vec2d::seg_seg_intersect (const vec2d &pnt_A, const vec2d &pnt_B, const vec2d &pnt_C, const vec2d &pnt_D, vec2d &int_pnt, double &t1, double &t2)
vec2d vec2d::proj_pnt_on_line_seg (const vec2d &line_A, const vec2d &line_B, const vec2d &pnt)
double vec2d::proj_pnt_on_line_u (const vec2d &line_A, const vec2d &line_B, const vec2d &pnt)
bool vec2d::PointInPolygon (const vec2d &R, const std::vector< vec2d > &pnts)
double vec2d::det (const vec2d &p0, const vec2d &p1, const vec2d &offset)
double vec2d::poly_area (const std::vector< vec2d > &pnt_vec)
vec2d vec2d::poly_centroid (const std::vector< vec2d > &pnt_vec)
double vec2d::orient2d (const vec2d &p0, const vec2d &p1, const vec2d &p)
void vec2d::bi_lin_interp (const vec2d &p0, const vec2d &p1, const vec2d &p2, vec2d const &p3, double s, double t, vec2d &p_out)
int vec2d::inverse_bi_lin_interp (const vec2d &p0, const vec2d &p1, const vec2d &p2, vec2d const &p3, const vec2d &p, double &s, double &t, double &s2, double &t2)

Detailed Description


Class Documentation

◆ vec2d

class vec2d

vec2d is typically used to describe coordinate points and vectors in 2D space. Both elements in the vector are of type double.

Definition at line 61 of file Vec2d.h.

Public Member Functions

 vec2d (double xx, double yy)
vec2doperator= (double a)
double & operator[] (int i)
const double & operator[] (int i) const
vec2dset_xy (double xx, double yy)
vec2dset_x (double xx)
vec2dset_y (double yy)
double x () const
double y () const
double * data ()
vec2d transform (float mat[3][3])
vec2d transform (double mat[3][3])
vec2doperator+= (const vec2d &b)
vec2doperator-= (const vec2d &b)
vec2doperator*= (double b)
double mag () const
void normalize ()

Public Attributes

double v [2]

Friends

vec2d operator+ (const vec2d &a, const vec2d &b)
vec2d operator- (const vec2d &a, const vec2d &b)
vec2d operator* (const vec2d &a, double b)
vec2d operator* (double b, const vec2d &a)
vec2d operator* (const vec2d &a, const vec2d &b)
vec2d operator/ (const vec2d &a, double b)
double dist (const vec2d &a, const vec2d &b)
double dist_squared (const vec2d &a, const vec2d &b)
double cross (const vec2d &a, const vec2d &b)
double dot (const vec2d &a, const vec2d &b)
double angle (const vec2d &a, const vec2d &b)
double cos_angle (const vec2d &a, const vec2d &b)
int seg_seg_intersect (const vec2d &pnt_A, const vec2d &pnt_B, const vec2d &pnt_C, const vec2d &pnt_D, vec2d &int_pnt, double &t1, double &t2)
vec2d proj_pnt_on_line_seg (const vec2d &line_A, const vec2d &line_B, const vec2d &pnt)
double proj_pnt_on_line_u (const vec2d &line_A, const vec2d &line_B, const vec2d &pnt)
void encode (double x_min, double y_min, double x_max, double y_max, const vec2d &pnt, int code[4])
void clip_seg_rect (double x_min, double y_min, double x_max, double y_max, vec2d &pnt1, vec2d &pnt2, int &visible)
bool PointInPolygon (const vec2d &R, const std::vector< vec2d > &pnts)
double det (const vec2d &p0, const vec2d &p1, const vec2d &offset)
double poly_area (const std::vector< vec2d > &pnt_vec)
vec2d poly_centroid (const std::vector< vec2d > &pnt_vec)
double orient2d (const vec2d &p0, const vec2d &p1, const vec2d &p)
void bi_lin_interp (const vec2d &p0, const vec2d &p1, const vec2d &p2, vec2d const &p3, double s, double t, vec2d &p_out)
int inverse_bi_lin_interp (const vec2d &p0, const vec2d &p1, const vec2d &p2, vec2d const &p3, const vec2d &p, double &s, double &t, double &s2, double &t2)

Member Function Documentation

◆ data()

double * vec2d::data ( )
inline

Definition at line 322 of file Vec2d.h.

◆ operator[]()

const double & vec2d::operator[] ( int i) const
inline

Definition at line 152 of file Vec2d.h.

Member Data Documentation

◆ v

double vec2d::v[2]

Definition at line 66 of file Vec2d.h.

Function Documentation

◆ mag()

double vec2d::mag ( ) const

Get the magnitude of a vec2d

vec2d a( 3.0, 4.0 );
if ( abs( a.mag() - 5.0 ) > 1e-12 ) { Print( "ERROR: mag" ); __failure++; }
See also
normalize
Returns
double Magnitude

◆ normalize()

void vec2d::normalize ( )

Scale a vec2d to unit length, in place

vec2d a( 3.0, 4.0 );
a.normalize();
if ( abs( a.mag() - 1.0 ) > 1e-12 ) { Print( "ERROR: normalize" ); __failure++; }
if ( abs( a.x() - 0.6 ) > 1e-12 ) { Print( "ERROR: normalize" ); __failure++; }
See also
mag

◆ operator[]()

double & vec2d::operator[] ( int i)
inline

Index a vec2d by coordinate, 0 for X and 1 for Y. An index outside that range raises an IndexError, which is also what lets list() and tuple() walk a vec2d.

vec2d a( 3.0, 4.0 );
if ( abs( a[0] - 3.0 ) > 1e-12 ) { Print( "ERROR: vec2d opIndex" ); __failure++; }
if ( abs( a[1] - 4.0 ) > 1e-12 ) { Print( "ERROR: vec2d opIndex" ); __failure++; }
Parameters
[in]iint Coordinate index, 0 or 1
Returns
double Coordinate value

Definition at line 148 of file Vec2d.h.

◆ set_x()

vec2d & vec2d::set_x ( double xx)

Set the X coordinate (index 0) of the vec2d

vec2d a( 0.0, 4.0 );
a.set_x( 2.0 );
if ( abs( a.x() - 2.0 ) > 1e-12 ) { Print( "ERROR: set_x" ); __failure++; }
if ( abs( a.y() - 4.0 ) > 1e-12 ) { Print( "ERROR: set_x" ); __failure++; }
See also
set_xy, set_y
Parameters
[in]xxdouble New X value
Returns
vec2d Updated vec2d

◆ set_xy()

vec2d & vec2d::set_xy ( double xx,
double yy )

Set both coordinates of the vec2d

vec2d a();
a.set_xy( 2.0, 4.0 );
if ( abs( a.x() - 2.0 ) > 1e-12 ) { Print( "ERROR: set_xy" ); __failure++; }
if ( abs( a.y() - 4.0 ) > 1e-12 ) { Print( "ERROR: set_xy" ); __failure++; }
See also
set_x, set_y
Parameters
[in]xxdouble New X value
[in]yydouble New Y value
Returns
vec2d Updated vec2d

◆ set_y()

vec2d & vec2d::set_y ( double yy)

Set the Y coordinate (index 1) of the vec2d

vec2d a( 2.0, 0.0 );
a.set_y( 4.0 );
if ( abs( a.y() - 4.0 ) > 1e-12 ) { Print( "ERROR: set_y" ); __failure++; }
if ( abs( a.x() - 2.0 ) > 1e-12 ) { Print( "ERROR: set_y" ); __failure++; }
See also
set_xy, set_x
Parameters
[in]yydouble New Y value
Returns
vec2d Updated vec2d

◆ vec2d()

vec2d::vec2d ( double xx,
double yy )

Construct a vec2d from its two coordinates. The default constructor leaves the coordinates uninitialized, so prefer this one unless the value is about to be overwritten.

vec2d a( 3.0, 4.0 );
if ( abs( a.x() - 3.0 ) > 1e-12 ) { Print( "ERROR: vec2d" ); __failure++; }
if ( abs( a.y() - 4.0 ) > 1e-12 ) { Print( "ERROR: vec2d" ); __failure++; }
Parameters
[in]xxdouble X coordinate
[in]yydouble Y coordinate

◆ x()

double vec2d::x ( ) const

Get the X coordinate (index 0) of the vec2d

vec2d a( 3.0, 4.0 );
if ( abs( a.x() - 3.0 ) > 1e-12 ) { Print( "ERROR: x" ); __failure++; }
See also
y
Returns
double X value

◆ y()

double vec2d::y ( ) const

Get the Y coordinate (index 1) of the vec2d

vec2d a( 3.0, 4.0 );
if ( abs( a.y() - 4.0 ) > 1e-12 ) { Print( "ERROR: y" ); __failure++; }
See also
x
Returns
double Y value

Friends

◆ angle

double angle ( const vec2d & a,
const vec2d & b )
friend

Calculate the angle between two vec2d, in radians. The result is unsigned, in [0, pi].

vec2d a( 1.0, 0.0 );
vec2d b( 0.0, 1.0 );
double PI = 3.14159265358979323846;
if ( abs( angle( a, b ) - 0.5 * PI ) > 1e-12 ) { Print( "ERROR: angle" ); __failure++; }
friend double angle(const vec2d &a, const vec2d &b)
See also
cos_angle
Parameters
[in]avec2d First vector
[in]bvec2d Second vector
Returns
double Angle in radians

◆ bi_lin_interp

void bi_lin_interp ( const vec2d & p0,
const vec2d & p1,
const vec2d & p2,
vec2d const & p3,
double s,
double t,
vec2d & p_out )
friend

Interpolate a point inside the quadrilateral p0, p1, p2, p3. The parameter s runs from the p0-p3 edge to the p1-p2 edge and t runs from the p0-p1 edge to the p3-p2 edge, both over [0, 1].

vec2d p;
bi_lin_interp( vec2d( 0.0, 0.0 ), vec2d( 1.0, 0.0 ), vec2d( 1.0, 1.0 ), vec2d( 0.0, 1.0 ), 0.25, 0.75, p );
if ( abs( p.x() - 0.625 ) > 1e-12 ) { Print( "ERROR: bi_lin_interp" ); __failure++; }
if ( abs( p.y() - 0.75 ) > 1e-12 ) { Print( "ERROR: bi_lin_interp" ); __failure++; }
friend void bi_lin_interp(const vec2d &p0, const vec2d &p1, const vec2d &p2, vec2d const &p3, double s, double t, vec2d &p_out)
double x() const
double y() const
See also
inverse_bi_lin_interp
Parameters
[in]p0vec2d First corner
[in]p1vec2d Second corner
[in]p2vec2d Third corner
[in]p3vec2d Fourth corner
[in]sdouble First parameter, in [0, 1]
[in]tdouble Second parameter, in [0, 1]
[out]p_outvec2d Interpolated point

◆ cos_angle

double cos_angle ( const vec2d & a,
const vec2d & b )
friend

Calculate the cosine of the angle between two vec2d. Cheaper than angle, which has to take an arc cosine, and enough on its own when the angle is only being compared.

vec2d a( 1.0, 0.0 );
vec2d b( 0.0, 1.0 );
if ( abs( cos_angle( a, b ) ) > 1e-12 ) { Print( "ERROR: cos_angle" ); __failure++; }
if ( abs( cos_angle( a, a ) - 1.0 ) > 1e-12 ) { Print( "ERROR: cos_angle" ); __failure++; }
friend double cos_angle(const vec2d &a, const vec2d &b)
double cos_angle(const vec3d &a, const vec3d &b)
See also
angle
Parameters
[in]avec2d First vector
[in]bvec2d Second vector
Returns
double Cosine of the angle between the vectors

◆ cross

double cross ( const vec2d & a,
const vec2d & b )
friend

Calculate the 2D cross product of two vec2d. In two dimensions the cross product is the single scalar a.x * b.y - a.y * b.x, which is the signed area of the parallelogram they span.

vec2d a( 1.0, 0.0 );
vec2d b( 0.0, 1.0 );
if ( abs( cross( a, b ) - 1.0 ) > 1e-12 ) { Print( "ERROR: cross" ); __failure++; }
if ( abs( cross( b, a ) + 1.0 ) > 1e-12 ) { Print( "ERROR: cross" ); __failure++; }
friend double cross(const vec2d &a, const vec2d &b)
vec3d cross(const vec3d &a, const vec3d &b)
See also
dot
Parameters
[in]avec2d First vector
[in]bvec2d Second vector
Returns
double Signed area spanned by the two vectors

◆ det

double det ( const vec2d & p0,
const vec2d & p1,
const vec2d & offset )
friend

Twice the signed area of the triangle p0, p1, offset. Positive when the three points turn counter-clockwise, so the sign says which side of the line p0-p1 the third point is on.

double d = det( vec2d( 0.0, 0.0 ), vec2d( 1.0, 0.0 ), vec2d( 0.0, 1.0 ) );
if ( d <= 0.0 ) { Print( "ERROR: det" ); __failure++; }
friend double det(const vec2d &p0, const vec2d &p1, const vec2d &offset)
See also
orient2d
Parameters
[in]p0vec2d First point
[in]p1vec2d Second point
[in]offsetvec2d Third point
Returns
double Twice the signed area of the triangle

◆ dist

double dist ( const vec2d & a,
const vec2d & b )
friend

Calculate the distance between two vec2d

vec2d a( 0.0, 0.0 );
vec2d b( 3.0, 4.0 );
if ( abs( dist( a, b ) - 5.0 ) > 1e-12 ) { Print( "ERROR: dist" ); __failure++; }
friend double dist(const vec2d &a, const vec2d &b)
See also
dist_squared
Parameters
[in]avec2d First point
[in]bvec2d Second point
Returns
double Distance

◆ dist_squared

double dist_squared ( const vec2d & a,
const vec2d & b )
friend

Calculate the square of the distance between two vec2d. Cheaper than dist when the answer is only being compared against another distance.

vec2d a( 0.0, 0.0 );
vec2d b( 3.0, 4.0 );
if ( abs( dist_squared( a, b ) - 25.0 ) > 1e-12 ) { Print( "ERROR: dist_squared" ); __failure++; }
friend double dist_squared(const vec2d &a, const vec2d &b)
See also
dist
Parameters
[in]avec2d First point
[in]bvec2d Second point
Returns
double Squared distance

◆ dot

double dot ( const vec2d & a,
const vec2d & b )
friend

Calculate the dot product of two vec2d

vec2d a( 1.0, 2.0 );
vec2d b( 3.0, 4.0 );
if ( abs( dot( a, b ) - 11.0 ) > 1e-12 ) { Print( "ERROR: dot" ); __failure++; }
friend double dot(const vec2d &a, const vec2d &b)
See also
cross
Parameters
[in]avec2d First vector
[in]bvec2d Second vector
Returns
double Dot product

◆ inverse_bi_lin_interp

int inverse_bi_lin_interp ( const vec2d & p0,
const vec2d & p1,
const vec2d & p2,
vec2d const & p3,
const vec2d & p,
double & s,
double & t,
double & s2,
double & t2 )
friend

Recover the parameters s and t that place a point inside the quadrilateral p0, p1, p2, p3 – the inverse of bi_lin_interp. The problem is quadratic, so it can have two answers, returned as (s, t) and (s2, t2); the return value says how many were found. A configuration that degenerates for the point being asked about can return none.

vec2d p;
bi_lin_interp( vec2d( 0.0, 0.0 ), vec2d( 1.0, 0.0 ), vec2d( 1.0, 1.0 ), vec2d( 0.0, 1.0 ), 0.25, 0.75, p );
double s, t, s2, t2;
int n = inverse_bi_lin_interp( vec2d( 0.0, 0.0 ), vec2d( 1.0, 0.0 ), vec2d( 1.0, 1.0 ), vec2d( 0.0, 1.0 ), p, s, t, s2, t2 );
if ( n <= 0 ) { Print( "ERROR: inverse_bi_lin_interp" ); __failure++; }
if ( abs( s - 0.25 ) > 1e-9 ) { Print( "ERROR: inverse_bi_lin_interp" ); __failure++; }
if ( abs( t - 0.75 ) > 1e-9 ) { Print( "ERROR: inverse_bi_lin_interp" ); __failure++; }
void Print(const std::string &data, bool new_line=true)
friend int inverse_bi_lin_interp(const vec2d &p0, const vec2d &p1, const vec2d &p2, vec2d const &p3, const vec2d &p, double &s, double &t, double &s2, double &t2)
See also
bi_lin_interp
Parameters
[in]p0vec2d First corner
[in]p1vec2d Second corner
[in]p2vec2d Third corner
[in]p3vec2d Fourth corner
[in]pvec2d Point to locate
[out]sdouble First parameter of the first solution
[out]tdouble Second parameter of the first solution
[out]s2double First parameter of the second solution
[out]t2double Second parameter of the second solution
Returns
int Number of solutions found

◆ operator* [1/2]

vec2d operator* ( const vec2d & a,
const vec2d & b )
friend

Component-wise multiplication of two vec2d objects. This is not a dot or a cross product; see dot and cross for those.

vec2d a( 1.0, 2.0 );
vec2d b( 3.0, 4.0 );
vec2d c = a * b;
if ( abs( c.x() - 3.0 ) > 1e-12 ) { Print( "ERROR: vec2d opMul_r" ); __failure++; }
if ( abs( c.y() - 8.0 ) > 1e-12 ) { Print( "ERROR: vec2d opMul_r" ); __failure++; }
See also
dot, cross
Parameters
[in]avec2d First vector
[in]bvec2d Second vector
Returns
vec2d Component-wise product

◆ operator* [2/2]

vec2d operator* ( const vec2d & a,
double b )
friend

Scalar multiplication operator for a vec2d, performed by the multiplication of each vec2d component and the scalar

vec2d a( 1.0, 2.0 );
vec2d c = a * 1.5;
if ( abs( c.x() - 1.5 ) > 1e-12 ) { Print( "ERROR: vec2d opMul" ); __failure++; }
if ( abs( c.y() - 3.0 ) > 1e-12 ) { Print( "ERROR: vec2d opMul" ); __failure++; }
Parameters
[in]avec2d Vector
[in]bdouble Scalar
Returns
vec2d Scaled vector

◆ operator+

vec2d operator+ ( const vec2d & a,
const vec2d & b )
friend

Addition operator for two vec2d objects, performed by the addition of each corresponding component

vec2d a( 1.0, 2.0 );
vec2d b( 3.0, 4.0 );
vec2d c = a + b;
if ( abs( c.x() - 4.0 ) > 1e-12 ) { Print( "ERROR: vec2d opAdd" ); __failure++; }
if ( abs( c.y() - 6.0 ) > 1e-12 ) { Print( "ERROR: vec2d opAdd" ); __failure++; }
Parameters
[in]avec2d First vector
[in]bvec2d Second vector
Returns
vec2d Component-wise sum

◆ operator-

vec2d operator- ( const vec2d & a,
const vec2d & b )
friend

Subtraction operator for two vec2d objects, performed by the subtraction of each corresponding component

vec2d a( 3.0, 4.0 );
vec2d b( 1.0, 2.0 );
vec2d c = a - b;
if ( abs( c.x() - 2.0 ) > 1e-12 ) { Print( "ERROR: vec2d opSub" ); __failure++; }
if ( abs( c.y() - 2.0 ) > 1e-12 ) { Print( "ERROR: vec2d opSub" ); __failure++; }
Parameters
[in]avec2d First vector
[in]bvec2d Second vector
Returns
vec2d Component-wise difference

◆ operator/

vec2d operator/ ( const vec2d & a,
double b )
friend

Scalar division operator for a vec2d, performed by the division of each vec2d component by the scalar

vec2d a( 3.0, 6.0 );
vec2d c = a / 1.5;
if ( abs( c.x() - 2.0 ) > 1e-12 ) { Print( "ERROR: vec2d opDiv" ); __failure++; }
if ( abs( c.y() - 4.0 ) > 1e-12 ) { Print( "ERROR: vec2d opDiv" ); __failure++; }
Parameters
[in]avec2d Vector
[in]bdouble Scalar
Returns
vec2d Divided vector

◆ orient2d

double orient2d ( const vec2d & p0,
const vec2d & p1,
const vec2d & p )
friend

Report which side of the directed line p0-p1 the point p falls on. Positive when p is to the left, negative to the right, and zero when the three are collinear.

if ( orient2d( vec2d( 0.0, 0.0 ), vec2d( 1.0, 0.0 ), vec2d( 0.0, 1.0 ) ) <= 0.0 ) { Print( "ERROR: orient2d" ); __failure++; }
if ( orient2d( vec2d( 0.0, 0.0 ), vec2d( 1.0, 0.0 ), vec2d( 0.0, -1.0 ) ) >= 0.0 ) { Print( "ERROR: orient2d" ); __failure++; }
Definition Vec2d.h:62
friend double orient2d(const vec2d &p0, const vec2d &p1, const vec2d &p)
See also
det
Parameters
[in]p0vec2d First point of the line
[in]p1vec2d Second point of the line
[in]pvec2d Point to test
Returns
double Positive to the left of the line, negative to the right, zero when collinear

◆ PointInPolygon

bool PointInPolygon ( const vec2d & R,
const std::vector< vec2d > & pnts )
friend

Test whether a point lies inside a polygon. The polygon is given as its vertices in order; it does not have to be convex and does not have to repeat its first point at the end.

array<vec2d> square = { vec2d( 0.0, 0.0 ), vec2d( 1.0, 0.0 ), vec2d( 1.0, 1.0 ), vec2d( 0.0, 1.0 ) };
if ( !PointInPolygon( vec2d( 0.5, 0.5 ), square ) ) { Print( "ERROR: PointInPolygon" ); __failure++; }
if ( PointInPolygon( vec2d( 1.5, 0.5 ), square ) ) { Print( "ERROR: PointInPolygon" ); __failure++; }
friend bool PointInPolygon(const vec2d &R, const std::vector< vec2d > &pnts)
See also
poly_area, poly_centroid
Parameters
[in]Rvec2d Point to test
[in]pntsvector<vec2d> Vertices of the polygon, in order
Returns
bool True if the point is inside the polygon

◆ poly_area

double poly_area ( const std::vector< vec2d > & pnt_vec)
friend

Calculate the area enclosed by a polygon, given as its vertices in order. The result is unsigned, so the winding direction does not matter. Repeating the first point at the end is allowed but not required.

array<vec2d> square = { vec2d( 0.0, 0.0 ), vec2d( 1.0, 0.0 ), vec2d( 1.0, 1.0 ), vec2d( 0.0, 1.0 ) };
if ( abs( poly_area( square ) - 1.0 ) > 1e-12 ) { Print( "ERROR: poly_area" ); __failure++; }
See also
poly_centroid, PointInPolygon
Parameters
[in]pnt_vecvector<vec2d> Vertices of the polygon, in order
Returns
double Enclosed area

◆ poly_centroid

vec2d poly_centroid ( const std::vector< vec2d > & pnt_vec)
friend

Calculate the centroid of a polygon, given as its vertices in order. This is the centroid of the enclosed area, not the average of the vertices.

array<vec2d> square = { vec2d( 0.0, 0.0 ), vec2d( 1.0, 0.0 ), vec2d( 1.0, 1.0 ), vec2d( 0.0, 1.0 ) };
vec2d c = poly_centroid( square );
if ( abs( c.x() - 0.5 ) > 1e-12 ) { Print( "ERROR: poly_centroid" ); __failure++; }
if ( abs( c.y() - 0.5 ) > 1e-12 ) { Print( "ERROR: poly_centroid" ); __failure++; }
See also
poly_area
Parameters
[in]pnt_vecvector<vec2d> Vertices of the polygon, in order
Returns
vec2d Centroid of the enclosed area

◆ proj_pnt_on_line_seg

vec2d proj_pnt_on_line_seg ( const vec2d & line_A,
const vec2d & line_B,
const vec2d & pnt )
friend

Project a point onto a line segment. The result is clamped to the segment, so a point that projects past either end comes back as that end point.

vec2d p = proj_pnt_on_line_seg( vec2d( 0.0, 0.0 ), vec2d( 2.0, 0.0 ), vec2d( 1.0, 1.0 ) );
if ( abs( p.x() - 1.0 ) > 1e-12 ) { Print( "ERROR: proj_pnt_on_line_seg" ); __failure++; }
if ( abs( p.y() ) > 1e-12 ) { Print( "ERROR: proj_pnt_on_line_seg" ); __failure++; }
vec2d q = proj_pnt_on_line_seg( vec2d( 0.0, 0.0 ), vec2d( 2.0, 0.0 ), vec2d( 5.0, 1.0 ) );
if ( abs( q.x() - 2.0 ) > 1e-12 ) { Print( "ERROR: proj_pnt_on_line_seg" ); __failure++; }
friend vec2d proj_pnt_on_line_seg(const vec2d &line_A, const vec2d &line_B, const vec2d &pnt)
See also
proj_pnt_on_line_u
Parameters
[in]line_Avec2d First point of the segment
[in]line_Bvec2d Second point of the segment
[in]pntvec2d Point to project
Returns
vec2d Projected point on the segment

◆ proj_pnt_on_line_u

double proj_pnt_on_line_u ( const vec2d & line_A,
const vec2d & line_B,
const vec2d & pnt )
friend

Project a point onto a line and return where along it the projection falls, running 0 at the first point to 1 at the second. Unlike proj_pnt_on_line_seg the result is not clamped, so a point beyond the segment gives a parameter outside [0, 1].

double u = proj_pnt_on_line_u( vec2d( 0.0, 0.0 ), vec2d( 2.0, 0.0 ), vec2d( 1.0, 1.0 ) );
if ( abs( u - 0.5 ) > 1e-12 ) { Print( "ERROR: proj_pnt_on_line_u" ); __failure++; }
friend double proj_pnt_on_line_u(const vec2d &line_A, const vec2d &line_B, const vec2d &pnt)
See also
proj_pnt_on_line_seg
Parameters
[in]line_Avec2d First point of the line
[in]line_Bvec2d Second point of the line
[in]pntvec2d Point to project
Returns
double Parameter of the projection along the line

◆ seg_seg_intersect

int seg_seg_intersect ( const vec2d & pnt_A,
const vec2d & pnt_B,
const vec2d & pnt_C,
const vec2d & pnt_D,
vec2d & int_pnt,
double & t1,
double & t2 )
friend

Intersect two line segments, AB and CD. Reports whether they cross, and where. The parameters t1 and t2 locate the intersection along each segment, running 0 at the first point to 1 at the second.

vec2d pnt;
double t1, t2;
int hit = seg_seg_intersect( vec2d( 0.0, 0.0 ), vec2d( 2.0, 0.0 ), vec2d( 1.0, -1.0 ), vec2d( 1.0, 1.0 ), pnt, t1, t2 );
if ( hit == 0 ) { Print( "ERROR: seg_seg_intersect" ); __failure++; }
if ( abs( pnt.x() - 1.0 ) > 1e-12 ) { Print( "ERROR: seg_seg_intersect" ); __failure++; }
if ( abs( t1 - 0.5 ) > 1e-12 ) { Print( "ERROR: seg_seg_intersect" ); __failure++; }
if ( abs( t2 - 0.5 ) > 1e-12 ) { Print( "ERROR: seg_seg_intersect" ); __failure++; }
friend int seg_seg_intersect(const vec2d &pnt_A, const vec2d &pnt_B, const vec2d &pnt_C, const vec2d &pnt_D, vec2d &int_pnt, double &t1, double &t2)
Parameters
[in]pnt_Avec2d First point of the first segment
[in]pnt_Bvec2d Second point of the first segment
[in]pnt_Cvec2d First point of the second segment
[in]pnt_Dvec2d Second point of the second segment
[out]int_pntvec2d Intersection point
[out]t1double Parameter of the intersection along AB, in [0, 1]
[out]t2double Parameter of the intersection along CD, in [0, 1]
Returns
int Nonzero if the segments intersect