Created
July 11, 2016 07:25
-
-
Save MORTAL2000/f6774750e997ea1fe4fbd07aa87c0a45 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RoundedRectangleShape : public sf::Shape | |
{ | |
public: | |
explicit RoundedRectangleShape(const sf::Vector2f& size = {}) | |
: size(size) | |
, radius(10.f) | |
, pointCount(4) | |
, totalPoints(4 * 4) | |
{ | |
update(); | |
} | |
unsigned getPointCount() const | |
{ | |
return totalPoints; | |
} | |
sf::Vector2f getPoint(unsigned index) const | |
{ | |
static const float deltaAngle = 90.0f/(pointCount-1)* 0.01745329251f; | |
static const std::array<sf::Vector2f, 4> centerCorners{{ | |
{size.x - radius, radius }, | |
{radius, radius }, | |
{radius, size.y - radius}, | |
{size.x - radius, size.y - radius}, | |
}}; | |
if(index >= totalPoints) return {}; | |
const unsigned centerIndex = index/pointCount; | |
const auto center = centerCorners[centerIndex]; | |
sf::Vector2f point; | |
point.x = center.x + radius*std::cos(deltaAngle*(index-centerIndex)); | |
point.y = center.y - radius*std::sin(deltaAngle*(index-centerIndex)); | |
return point; | |
} | |
private: | |
sf::Vector2f size; | |
float radius; | |
unsigned pointCount; | |
unsigned totalPoints; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment