Added SetPointsCount in sf::CircleShape

This commit is contained in:
Laurent Gomila 2011-12-10 15:22:21 +01:00
parent 048abbf46f
commit c33fa1d290
2 changed files with 41 additions and 7 deletions

View File

@ -44,10 +44,11 @@ public :
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Default constructor /// \brief Default constructor
/// ///
/// \param radius Radius of the circle /// \param radius Radius of the circle
/// \param pointsCount Number of points composing the circle
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
explicit CircleShape(float radius = 0); explicit CircleShape(float radius = 0, unsigned int pointsCount = 30);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Set the radius of the circle /// \brief Set the radius of the circle
@ -69,11 +70,23 @@ public :
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
float GetRadius() const; float GetRadius() const;
////////////////////////////////////////////////////////////
/// \brief Set the number of points of the circle
///
/// \param count New number of points of the circle
///
/// \see GetPointsCount
///
////////////////////////////////////////////////////////////
void SetPointsCount(unsigned int count);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Get the number of points of the shape /// \brief Get the number of points of the shape
/// ///
/// \return Number of points of the shape /// \return Number of points of the shape
/// ///
/// \see SetPointsCount
///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
virtual unsigned int GetPointsCount() const; virtual unsigned int GetPointsCount() const;
@ -92,7 +105,8 @@ private :
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Member data // Member data
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
float myRadius; ///< Radius of the circle float myRadius; ///< Radius of the circle
unsigned int myPointsCount; ///< Number of points composing the circle
}; };
} // namespace sf } // namespace sf
@ -120,6 +134,15 @@ private :
/// window.Draw(circle); /// window.Draw(circle);
/// \endcode /// \endcode
/// ///
/// Since the graphics card can't draw perfect circles, we have to
/// fake them with multiple triangles connected to each other. The
/// "points count" property of sf::CircleShape defines how many of these
/// triangles to use, and therefore defines the quality of the circle.
///
/// The number of points can also be used for another purpose; with
/// small numbers you can create any regular polygon shape:
/// equilateral triangle, square, pentagon, hexagon, ...
///
/// \see sf::Shape, sf::RectangleShape, sf::ConvexShape /// \see sf::Shape, sf::RectangleShape, sf::ConvexShape
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////

View File

@ -32,9 +32,11 @@
namespace sf namespace sf
{ {
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
CircleShape::CircleShape(float radius) CircleShape::CircleShape(float radius, unsigned int pointsCount) :
myRadius (radius),
myPointsCount(pointsCount)
{ {
SetRadius(radius); Update();
} }
@ -53,17 +55,26 @@ float CircleShape::GetRadius() const
} }
////////////////////////////////////////////////////////////
void CircleShape::SetPointsCount(unsigned int count)
{
myPointsCount = count;
Update();
}
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
unsigned int CircleShape::GetPointsCount() const unsigned int CircleShape::GetPointsCount() const
{ {
return 30; return myPointsCount;
} }
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Vector2f CircleShape::GetPoint(unsigned int index) const Vector2f CircleShape::GetPoint(unsigned int index) const
{ {
float angle = index * 2 * 3.141592654f / GetPointsCount(); static const float pi = 3.141592654f;
float angle = index * 2 * pi / myPointsCount - pi / 2;
float x = std::cos(angle) * myRadius; float x = std::cos(angle) * myRadius;
float y = std::sin(angle) * myRadius; float y = std::sin(angle) * myRadius;