Last active
September 19, 2016 14:34
-
-
Save oprypin/b09025121f3e4e255d1f866c70db9170 to your computer and use it in GitHub Desktop.
CrSFML: Rounded polygon shape
This file contains hidden or 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 RoundedPolygon < SF::Shape | |
def initialize | |
super() | |
@result = [] of SF::Vector2f | |
end | |
def assign(points, radius : Number) | |
@result.clear | |
unless points.size < 2 | |
p1 = points[points.size - 1] | |
a1 = p1 - points[points.size - 2] | |
a1 = Math.atan2(-a1.x, a1.y) | |
points.each do |p2| | |
a2 = p2 - p1 | |
a2 = Math.atan2(-a2.x, a2.y) # normal angle | |
a2 += Math::PI * 2 if a2 < a1 | |
steps = (radius == 0 ? 1 : ((a2 - a1) * 3).round.to_i) | |
(0..steps).each do |i| | |
a = a2 * i / steps + a1 * (steps - i) / steps | |
@result << SF.vector2f(p1.x + Math.cos(a)*radius, p1.y + Math.sin(a)*radius) | |
end | |
a1 = a2 | |
p1 = p2 | |
end | |
end | |
update() | |
end | |
def point_count | |
@result.size | |
end | |
def get_point(index) | |
@result[index] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment