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

Author Topic: Custom ellipse shape pointCount  (Read 1467 times)

0 Members and 1 Guest are viewing this topic.

Dajcok

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • Email
Custom ellipse shape pointCount
« on: March 16, 2020, 11:18:28 am »
Hello I've made custom ellipse shape according to this code:
#pragma once

#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>

class EllipseShape : public sf::Shape
{
public:

        explicit EllipseShape(const sf::Vector2f& radius = sf::Vector2f(0.f, 0.f)) :
                m_radius(radius)
        {
                update();
        }

        void setRadius(const sf::Vector2f& radius)
        {
                m_radius = radius;
                update();
        }

        const sf::Vector2f& getRadius() const
        {
                return m_radius;
        }

        virtual std::size_t getPointCount() const
        {
                return 30;
        }

        virtual sf::Vector2f getPoint(std::size_t index) const
        {
                static const float pi = 3.141592654f;

                float angle = index * 2 * pi / getPointCount() - pi / 2;
                float x = std::cos(angle) * m_radius.x;
                float y = std::sin(angle) * m_radius.y;

                return sf::Vector2f(m_radius.x + x, m_radius.y + y);
        }

        //void setPointCount(std::size_t count);

private:
        std::size_t m_pointCount;
        sf::Vector2f m_radius;
};

I would like to set point count to this ellipse because it's too edgy when i resize it. I added there void setPointCount(std::size_t count) according to circleshape.hpp but i couldn't find definition of that function. Any ideas how can I define that function so it would add more points to that ellipse ?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Custom ellipse shape pointCount
« Reply #1 on: March 16, 2020, 11:38:00 am »
That's really straight-forward. Have you really thought about it? ;)

Currently, your point count is hard-coded to 30. Changing that to a member variable would be a good starting point. Then, setPointCount could change this variable. And don't forget to trigger an update of the shape after that.

In case you're really lost... implementation of sf::CircleShape is here:
https://github.com/SFML/SFML/blob/master/src/SFML/Graphics/CircleShape.cpp#L59
Laurent Gomila - SFML developer

Dajcok

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • Email
Re: Custom ellipse shape pointCount
« Reply #2 on: March 16, 2020, 12:09:45 pm »
i got it thx very much ;D