Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Line Shape  (Read 6091 times)

0 Members and 1 Guest are viewing this topic.


  • Full Member
  • ***
  • Posts: 104
    • View Profile
Line Shape
« on: August 31, 2013, 12:14:50 pm »
Following my suggestion in the features section to add a sf::Line again, I looked to the wiki about adding the proposed class and found there is a page on it already.. I believe though the classes presented there are overkill and would suggest these instead:

Deriving from sf::Drawable

A simple class that derives from sf::Drawable only.

class sfLine : public sf::Drawable
    sfLine(const sf::Vector2f& point1, const sf::Vector2f& point2):
        color(sf::Color::Yellow), thickness(5.f)
        sf::Vector2f direction = point2 - point1;
        sf::Vector2f unitDirection = direction/std::sqrt(direction.x*direction.x+direction.y*direction.y);
        sf::Vector2f unitPerpendicular(-unitDirection.y,unitDirection.x);

        sf::Vector2f offset = (thickness/2.f)*unitPerpendicular;

        vertices[0].position = point1 + offset;
        vertices[1].position = point2 + offset;
        vertices[2].position = point2 - offset;
        vertices[3].position = point1 - offset;

        for (int i=0; i<4; ++i)
            vertices[i].color = color;

    void draw(sf::RenderTarget &target, sf::RenderStates states) const

    sf::Vertex vertices[4];
    float thickness;
    sf::Color color;

Deriving from sf::Shape
Header and source for a sf::LineShape, closely modelled after sf::RectangleShape. These can be easily added to one's copy of SFML with minor additions to the relevant CMakeLists.txt and the Graphics master header. Doxygen comments were removed for space.


#include <SFML/Graphics/Export.hpp>
#include <SFML/Graphics/Shape.hpp>

namespace sf
        class SFML_GRAPHICS_API LineShape : public Shape
                public :

                explicit LineShape(const Vector2f& point1, const Vector2f& point2);

                void setThickness(float thickness);

                float getThickness() const;

                float getLength() const;

                virtual unsigned int getPointCount() const;

                virtual Vector2f getPoint(unsigned int index) const;

                private :

    Vector2f m_direction; ///< Direction of the line
    float m_thickness;    ///< Thickness of the line

} // namespace sf


#include <SFML/Graphics/LineShape.hpp>
#include <cmath>

namespace sf

LineShape::LineShape(const Vector2f& point1, const Vector2f& point2):
    m_direction(point2 - point1)    

void LineShape::setThickness(float thickness)
    m_thickness = thickness;

float LineShape::getThickness() const
    return m_thickness;

float LineShape::getLength() const
    return std::sqrt(m_direction.x*m_direction.x+m_direction.y*m_direction.y);

unsigned int LineShape::getPointCount() const
    return 4;

Vector2f LineShape::getPoint(unsigned int index) const
    Vector2f unitDirection = m_direction/getLength();
    Vector2f unitPerpendicular(-unitDirection.y,unitDirection.x);

    Vector2f offset = (m_thickness/2.f)*unitPerpendicular;

    switch (index)
        case 0: return offset;
        case 1: return (m_direction + offset);
        case 2: return (m_direction - offset);
        case 3: return (-offset);

} // namespace sf