Last active
December 25, 2015 17:38
-
-
Save dchapman1988/7014081 to your computer and use it in GitHub Desktop.
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
require 'matrix' | |
# A polygon that's most likely to be encountured in nature. | |
# These are normally convex and concave polygons. | |
# (all sides and all angles are not congruent) | |
class IdealPolygon | |
attr_accessor :coordinates, :number_of_sides | |
def initialize(coordinates=[]) | |
@coordinates = coordinates | |
end | |
# The coordinates (x1,y1), (x2,y2), ..., (xN,yN) of a convex polygon | |
# need to me arranged in the "determinant". The coordinates must be taken | |
# in clockwise order around the polygon, beginning and ending at the same point. | |
def area | |
Matrix[*self.coordinates].determinate | |
end | |
def number_of_sides | |
@number_of_sides ||= (self.coordinates.count / 2) | |
end | |
end |
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
# A polygon class for which all sides and all angles are congruent | |
class RegularPolygon | |
attr_accessor :number_of_sides, :length_of_a_side, :inscribed_radius, :circumcircle_radius | |
def initialize(number_of_sides=0.0, length_of_a_side=0.0, inscribed_radius=0.0, circumcircle_radius=0.0) | |
@number_of_sides = number_of_sides | |
@length_of_a_side = length_of_a_side | |
@inscribed_radius = inscribed_radius.nonzero? || self.apothem | |
@circumcircle_radius = circumcircle_radius | |
end | |
# This is just the algorithm to find the inscribed_radius | |
def apothem | |
0.5 * self.length_of_a_side * Math.cot(108/self.number_of_sides) | |
end | |
def area | |
0.5 * self.number_of_sides * self.length_of_a_side * self.inscribed_radius | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment