RoundedRectangle

RoundedRectangle est une fonction permettant de générer des sf::Shape ayant une forme de rectangle aux coins arrondis.

Elle s'utilise de la même manière que sf::Shape::Rectangle ou sf::Shape::Circle.

La valeur de l'arrondi est paramétrable grâce au paramètre roundness.

sf::Shape RoundedRectangle(const sf::FloatRect& rectangle, const sf::Color& color, unsigned int roundness, float outline=0.f, const sf::Color& outlineColor = Color(0,0,0)) 
{ 
   // Set the roundess to be less than or equal to the semi-size 
   if(roundness > rectangle.Width/2) 
      roundness = ceil(rectangle.Width/2); 
   if(roundness > rectangle.Height/2) 
      roundness = ceil(rectangle.Height/2); 
 
   if(roundness == 0) 
      return sf::Shape::Rectangle(rectangle, color, outline, outlineColor); 
 
   sf::Shape shape; 
 
   // Top-left corner 
   for(unsigned int i = 0; i <= roundness+1; ++i) 
   { 
      sf::Vector2f pointPosition(cos(M_PI-i*M_PI/(roundness*2+2))*roundness+roundness+rectangle.Left, -sin(M_PI-i*M_PI/(roundness*2+2))*roundness+roundness+rectangle.Top); 
      if(shape.GetPointPosition(shape.GetPointsCount()-1) != pointPosition) 
         shape.AddPoint(pointPosition, color, outlineColor); 
   } 
   // Top-right corner 
   for(unsigned int i = 0; i <= roundness+1; ++i) 
   { 
      sf::Vector2f pointPosition(cos(0.5f*M_PI-(i*M_PI/(roundness*2+2)))*roundness+rectangle.Left+rectangle.Width-roundness, -sin(0.5f*M_PI-i*M_PI/(roundness*2+2))*roundness+roundness+rectangle.Top); 
      if(shape.GetPointPosition(shape.GetPointsCount()-1) != pointPosition) 
         shape.AddPoint(pointPosition, color, outlineColor); 
   } 
   // Bottom-right corner 
   for(unsigned int i = 0; i <= roundness+1; ++i) 
   { 
      sf::Vector2f pointPosition(cos(-(i*M_PI/(roundness*2+2)))*roundness+rectangle.Left+rectangle.Width-roundness, -sin(-(i*M_PI/(roundness*2+2)))*roundness+rectangle.Top+rectangle.Height-roundness); 
      if(shape.GetPointPosition(shape.GetPointsCount()-1) != pointPosition) 
         shape.AddPoint(pointPosition, color, outlineColor); 
   } 
   // Bottom-left corner 
   for(unsigned int i = 0; i <= roundness+1; ++i) 
   { 
      sf::Vector2f pointPosition(cos(-0.5f*M_PI-(i*M_PI/(roundness*2+2)))*roundness+roundness+rectangle.Left, -sin(-0.5f*M_PI-(i*M_PI/(roundness*2+2)))*roundness+rectangle.Top+rectangle.Height-roundness); 
      if(shape.GetPointPosition(shape.GetPointsCount()-1) != pointPosition && (i != roundness+1 || shape.GetPointPosition(0) != pointPosition)) 
         shape.AddPoint(pointPosition, color, outlineColor); 
   } 
 
   shape.SetOutlineWidth(outline); 
 
   return shape; 
}
 
fr/sources/roundedrectangle.txt · Last modified: 2010/08/28 21:21 by bluelink
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki