Candle v1.0
2D lighting for SFML
sfu::Line Struct Reference

2D Line defined by an origin point and a direction vector. More...

#include <Line.hpp>

Public Member Functions

 Line (const sf::Vector2f &p1, const sf::Vector2f &p2)
 Construct a line that passes through p1 and p2. More...
 
 Line (const sf::Vector2f &p, float angle)
 Construct a line defined by a point and an angle. More...
 
sf::FloatRect getGlobalBounds () const
 Get the global bounding rectangle of the line. More...
 
int relativePosition (const sf::Vector2f &point) const
 Return the relative position of a point to the line. More...
 
float distance (const sf::Vector2f &point) const
 Return the minimum distance of a point to the line. More...
 
bool intersection (const Line &lineB) const
 Returns a boolean if there is intersection of this line to another. More...
 
bool intersection (const Line &lineB, float &normA) const
 Returns the magnitude corresponding of the intersection of this line to another. More...
 
bool intersection (const Line &lineB, float &normA, float &normB) const
 Returns the magnitudes corresponding of the intersection of this line to another. More...
 
sf::Vector2f point (float param) const
 Get a point of the line. More...
 

Public Attributes

sf::Vector2f m_origin
 Origin point of the line.
 
sf::Vector2f m_direction
 Direction vector (not necessarily normalized)
 

Detailed Description

2D Line defined by an origin point and a direction vector.

Constructor & Destructor Documentation

◆ Line() [1/2]

sfu::Line::Line ( const sf::Vector2f &  p1,
const sf::Vector2f &  p2 
)

Construct a line that passes through p1 and p2.

The direction is interpreted as p2 - p1.

Parameters
p1First point
p2Second point

◆ Line() [2/2]

sfu::Line::Line ( const sf::Vector2f &  p,
float  angle 
)

Construct a line defined by a point and an angle.

The direction is interpreted as {cos(angle), sin(angle)}.

Parameters
pOrigin point
angleAngle defining the line

Member Function Documentation

◆ getGlobalBounds()

sf::FloatRect sfu::Line::getGlobalBounds ( ) const

Get the global bounding rectangle of the line.

The returned rectangle is in global coordinates, which means that it takes into account the transformations (translation, rotation, scale, ...) (see SFML).

Returns
Global bounding rectangle in float

◆ relativePosition()

int sfu::Line::relativePosition ( const sf::Vector2f &  point) const

Return the relative position of a point to the line.

If the point is to the right of the direction vector, the value returned is -1. If it is to the left, it is +1. If the point belongs to the line, returns 0.

Parameters
point
Returns
-1, 0 or 1

◆ distance()

float sfu::Line::distance ( const sf::Vector2f &  point) const

Return the minimum distance of a point to the line.

Parameters
point
Returns
The minimum distance of a point to the line.

◆ intersection() [1/3]

bool sfu::Line::intersection ( const Line lineB) const

Returns a boolean if there is intersection of this line to another.

Parameters
lineB
Returns
True, if there is an intersection.
See also
point

◆ intersection() [2/3]

bool sfu::Line::intersection ( const Line lineB,
float &  normA 
) const

Returns the magnitude corresponding of the intersection of this line to another.

If there is an intersection, the output argument normA contains the magnitude required to get the intersection point from this line direction.

Parameters
lineB
normA(Output argument)
Returns
True, if there is an intersection.
See also
point

◆ intersection() [3/3]

bool sfu::Line::intersection ( const Line lineB,
float &  normA,
float &  normB 
) const

Returns the magnitudes corresponding of the intersection of this line to another.

If there is an intersection, the output argument normB contains the magnitude required to get the intersection point from lineB direction and normA, the magnitude required to get the intersection point from this line direction.

Parameters
lineB
normA(Output argument)
normB(Output argument)
Returns
True, if there is an intersection.
See also
point

◆ point()

sf::Vector2f sfu::Line::point ( float  param) const

Get a point of the line.

The point is obtained is m_origin + param * m_direction

Parameters
param
Returns
A point of the line