wPlot2D version: 0.1.0
A lightweight C++ library for clear and customizable 2D scientific plots.
Loading...
Searching...
No Matches
wPlot2D::FunctionEntity Class Reference

Represents a mathematical function as a drawable entity in a 2D plot. More...

#include <wFunctionEntity.hpp>

Inheritance diagram for wPlot2D::FunctionEntity:
Inheritance graph

Public Member Functions

 FunctionEntity (const sf::Vector2f origin, const sf::Vector2f scale, std::function< double(double) > func)
 Construct a new FunctionEntity.
virtual ~FunctionEntity ()=default
 Virtual destructor.
sf::Vector2f getPosition () const
 Get the position (origin) of the function in pixel space.
sf::Color getColor () const
 Get the color of the function curve.
float getThickness () const
 Get the line thickness of the function curve.
wEngine::LineStyleComponent::LineStyle getLineStyle () const
 Get the line style of the function curve.
float getDashLength () const
 Get the dash length when the line style is Dashed.
float getGapLength () const
 Get the gap length between dashes or dots.
sf::Vector2f getOffset () const
 Get the current offset applied to the function curve.
float getRotation () const
 Get the current rotation angle of the function curve.
void setPosition (sf::Vector2f position)
 Set the position (origin) of the function in pixel space.
void setColor (sf::Color color)
 Set the color of the function curve.
void setThickness (float thickness)
 Set the line thickness of the function curve.
void setLineStyle (wEngine::LineStyleComponent::LineStyle style)
 Set the line style of the function curve.
void setDashLength (float dashLength)
 Set the length of each dash when the line style is Dashed.
void setGapLength (float gapLength)
 Set the length of the gap between dashes or dots.
void setOffset (float offsetX, float offsetY)
 Set an offset applied to the function curve.
void setRotation (float angleDegrees)
 Set the rotation angle of the function curve.
void setScale (sf::Vector2f scale)
 Sets the scaling factors for the function graph.
void addExcludedInterval (double min, double max)
 Add an excluded interval where the function should not be drawn.
void clearExcludedIntervals ()
 Clear all excluded intervals.
void alignToYAxis (float normalizedOffsetX=0.0f, float normalizedOffsetY=0.0f)
 Rotates the function by 90 degrees and swaps scales accordingly.
void drawFunction (sf::RenderWindow &window, double startX, double endX, size_t nbPoints=1000)
 Draw the function on the target window.
Public Member Functions inherited from wEngine::Entity
 Entity ()
virtual ~Entity ()
unsigned int getEntityID () const
 Returns the unique ID associated with this entity.
void clearComponents ()
 Removes all components currently attached to the entity.
template<typename T, typename... Args>
std::shared_ptr< T > addComponent (Args &&... args)
 Adds a new component of type T to the entity.
template<typename T>
void removeComponent ()
 Removes the component of type T from the entity.
template<typename T>
bool hasComponent () const noexcept
 Checks whether the entity has a component of type T.
template<typename T>
std::shared_ptr< T > getComponent () const
 Retrieves the component of type T attached to the entity.
template<typename T>
std::shared_ptr< T > requireComponent (const std::string &context="") const
 Retrieves the component of type T and throws if it's missing.
template<typename Interface>
std::shared_ptr< Interface > getInterfaceComponent () const
 Returns the first component that implements the specified interface.

Additional Inherited Members

Static Public Member Functions inherited from wEngine::Entity
static void resetEntityIDCounter ()
 Resets the global entity ID counter to zero.

Detailed Description

Represents a mathematical function as a drawable entity in a 2D plot.

A FunctionEntity manages all components required to render a curve:

  • Origin and scale (mapping logical space to pixels).
  • Color, thickness, and line style (solid, dashed, dotted).
  • Offset and rotation of the curve.
  • Discontinuities handled by excluded intervals.

The function is sampled at evenly spaced x-values and rendered as a polyline. Excluded intervals and invalid values (NaN, Inf) split the curve into separate segments.

Author
Wilfried Koch

Constructor & Destructor Documentation

◆ FunctionEntity()

wPlot2D::FunctionEntity::FunctionEntity ( const sf::Vector2f origin,
const sf::Vector2f scale,
std::function< double(double) > func )

Construct a new FunctionEntity.

Parameters
originLogical origin of the coordinate system (pixels).
scaleScale factors for x and y (pixels per unit).
funcFunction of type double(double) to be plotted.

◆ ~FunctionEntity()

virtual wPlot2D::FunctionEntity::~FunctionEntity ( )
virtualdefault

Virtual destructor.

Member Function Documentation

◆ getPosition()

sf::Vector2f wPlot2D::FunctionEntity::getPosition ( ) const
nodiscard

Get the position (origin) of the function in pixel space.

Returns
The current origin as an sf::Vector2f.

◆ getColor()

sf::Color wPlot2D::FunctionEntity::getColor ( ) const
nodiscard

Get the color of the function curve.

Returns
The current curve color.

◆ getThickness()

float wPlot2D::FunctionEntity::getThickness ( ) const
nodiscard

Get the line thickness of the function curve.

Returns
The thickness in pixels.

◆ getLineStyle()

wEngine::LineStyleComponent::LineStyle wPlot2D::FunctionEntity::getLineStyle ( ) const
nodiscard

Get the line style of the function curve.

Returns
The current line style (Solid, Dashed, or Dotted).

◆ getDashLength()

float wPlot2D::FunctionEntity::getDashLength ( ) const
nodiscard

Get the dash length when the line style is Dashed.

Returns
Dash length in pixels.
Note
This value has no effect if the style is not Dashed.

◆ getGapLength()

float wPlot2D::FunctionEntity::getGapLength ( ) const
nodiscard

Get the gap length between dashes or dots.

Returns
Gap length in pixels.
Note
This setting affects both Dashed and Dotted line styles.

◆ getOffset()

sf::Vector2f wPlot2D::FunctionEntity::getOffset ( ) const
nodiscard

Get the current offset applied to the function curve.

Returns
A 2D vector containing the (x,y) offset in pixels.

◆ getRotation()

float wPlot2D::FunctionEntity::getRotation ( ) const
nodiscard

Get the current rotation angle of the function curve.

Returns
The rotation angle in degrees.

◆ setPosition()

void wPlot2D::FunctionEntity::setPosition ( sf::Vector2f position)

Set the position (origin) of the function in pixel space.

Parameters
positionThe new origin as an sf::Vector2f.

◆ setColor()

void wPlot2D::FunctionEntity::setColor ( sf::Color color)

Set the color of the function curve.

Parameters
colorThe new color as an sf::Color.

◆ setThickness()

void wPlot2D::FunctionEntity::setThickness ( float thickness)

Set the line thickness of the function curve.

Parameters
thicknessThe new line thickness in pixels.

◆ setLineStyle()

void wPlot2D::FunctionEntity::setLineStyle ( wEngine::LineStyleComponent::LineStyle style)

Set the line style of the function curve.

Parameters
styleThe new style (Solid, Dashed, or Dotted).

◆ setDashLength()

void wPlot2D::FunctionEntity::setDashLength ( float dashLength)

Set the length of each dash when the line style is Dashed.

Parameters
dashLengthThe dash length in pixels (must be > 0).
Exceptions
std::invalid_argumentif dashLength <= 0.
Note
This setting has no effect if the line style is not Dashed.

◆ setGapLength()

void wPlot2D::FunctionEntity::setGapLength ( float gapLength)

Set the length of the gap between dashes or dots.

Parameters
gapLengthThe gap length in pixels (must be >= 0).
Exceptions
std::invalid_argumentif gapLength < 0.
Note
This setting affects both Dashed and Dotted line styles.

◆ setOffset()

void wPlot2D::FunctionEntity::setOffset ( float offsetX,
float offsetY )

Set an offset applied to the function curve.

The offset is applied after scaling and rotation, allowing the curve to be shifted horizontally and vertically relative to its logical origin.

Parameters
offsetXHorizontal offset in pixels.
offsetYVertical offset in pixels.

◆ setRotation()

void wPlot2D::FunctionEntity::setRotation ( float angleDegrees)

Set the rotation angle of the function curve.

The rotation is applied around the logical origin of the graph. The angle is expressed in degrees, with positive values corresponding to counter-clockwise rotation.

Parameters
angleDegreesRotation angle in degrees.

◆ setScale()

void wPlot2D::FunctionEntity::setScale ( sf::Vector2f scale)

Sets the scaling factors for the function graph.

This method updates the ScaleComponent so the user can control how much the x and y coordinates are stretched on screen.

Parameters
scaleThe new scaling vector (scale.x, scale.y).

◆ addExcludedInterval()

void wPlot2D::FunctionEntity::addExcludedInterval ( double min,
double max )

Add an excluded interval where the function should not be drawn.

Useful to handle discontinuities such as vertical asymptotes.

Parameters
minLeft bound of the interval.
maxRight bound of the interval.

◆ clearExcludedIntervals()

void wPlot2D::FunctionEntity::clearExcludedIntervals ( )

Clear all excluded intervals.

◆ alignToYAxis()

void wPlot2D::FunctionEntity::alignToYAxis ( float normalizedOffsetX = 0.0f,
float normalizedOffsetY = 0.0f )

Rotates the function by 90 degrees and swaps scales accordingly.

This is a common operation when we want to interpret the function's values along the X-axis instead of the Y-axis (or vice versa). The method:

  • Sets a 90 degrees rotation.
  • Swaps scale.x and scale.y to preserve unit consistency.
  • Applies an optional normalized offset for alignment.
Parameters
normalizedOffsetXRelative offset along X after rotation, expressed in units of (oldScaleX / oldScaleY).
normalizedOffsetYRelative offset along Y after rotation, expressed in units of (oldScaleY / oldScaleX).

◆ drawFunction()

void wPlot2D::FunctionEntity::drawFunction ( sf::RenderWindow & window,
double startX,
double endX,
size_t nbPoints = 1000 )

Draw the function on the target window.

Parameters
windowRender target.
startXStart of the logical x-range.
endXEnd of the logical x-range.
nbPointsNumber of points to sample (default: 1000).

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