Skip to content

class SF::ConvexShape
inherits SF::Shape #

Specialized shape representing a convex polygon

This class inherits all the functions of SF::Transformable (position, rotation, scale, bounds, ...) as well as the functions of SF::Shape (outline, color, texture, ...).

It is important to keep in mind that a convex shape must always be... convex, otherwise it may not be drawn correctly. Moreover, the points must be defined in order; using a random order would result in an incorrect shape.

Usage example:

polygon = SF::ConvexShape.new
polygon.point_count = 3
polygon[0] = SF.vector2f(0, 0)
polygon[1] = SF.vector2f(0, 10)
polygon[2] = SF.vector2f(25, 5)
polygon.outline_color = SF::Color::Red
polygon.outline_thickness = 5
polygon.position = {10, 20}
...
window.draw polygon

See also: SF::Shape, SF::RectangleShape, SF::CircleShape

Class methods#

.new(point_count : Int = 0) #

Default constructor

  • point_count - Number of points of the polygon
View source

Methods#

#[](index) #

Shorthand for get_point

View source

#[]=(index, point) #

Shorthand for set_point

View source

#dup : ConvexShape #

Returns a shallow copy of this object.

This allocates a new object and copies the contents of self into it.

View source

#finalize #

Virtual destructor

View source

#get_point(index : Int) : Vector2f #

Get the position of a point

The returned point is in local coordinates, that is, the shape's transforms (position, rotation, scale) are not taken into account. The result is undefined if index is out of the valid range.

  • index - Index of the point to get, in range 0 ... point_count

Returns: Position of the index-th point of the polygon

See also: point=

View source

#point_count : Int32 #

Get the number of points of the polygon

Returns: Number of points of the polygon

See also: point_count=

View source

#point_count=(count : Int) #

Set the number of points of the polygon

count must be greater than 2 to define a valid shape.

  • count - New number of points of the polygon

See also: point_count

View source

#set_point(index : Int, point : Vector2 | Tuple) #

Set the position of a point

Don't forget that the polygon must remain convex, and the points need to stay ordered! point_count= must be called first in order to set the total number of points. The result is undefined if index is out of the valid range.

  • index - Index of the point to change, in range 0 ... point_count
  • point - New position of the point

See also: point

View source