Skip to content

Instantly share code, notes, and snippets.

@gmassanek
Created February 17, 2025 03:35
Show Gist options
  • Save gmassanek/1bbd829e393135dabd2466dc5a60c717 to your computer and use it in GitHub Desktop.
Save gmassanek/1bbd829e393135dabd2466dc5a60c717 to your computer and use it in GitHub Desktop.
class Polygon
RAD_TO_DEG = (180 / Math::PI)
DEG_TO_RAD = (Math::PI / 180)
def self.all(h)
a = {}
(3..100).each do |n|
a[n] = call(h, n)
end
a
end
def self.call(h, n)
puts "Height: #{h}"
odd = n % 2 == 1
i_angle = 360 / n.to_f
puts "Interior angle: #{i_angle}"
i2_angle = i_angle / 2.0
puts "Half that angle: #{i2_angle}"
if odd
cos = Math.cos(i2_angle * DEG_TO_RAD)
r = h / (cos + 1)
puts "Radius: #{r}"
x = n.times.map do |i|
a = i_angle * (i + 1)
r * Math.sin(a * DEG_TO_RAD)
end.max
w = 2 * x
puts "w: #{w}"
else
r = (h / 2.0) / Math.cos(i2_angle * DEG_TO_RAD)
puts "Radius: #{r}"
x = n.times.map do |i|
a = i_angle * i + i2_angle
r * Math.sin(a * DEG_TO_RAD)
end.max
w = 2 * x
puts "w: #{w}"
end
w
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment