EasyGeometry gem allows one to create two-dimensional geometrical entities, such as lines and polygons, and query for information about these entities. This could include asking the area of an polygon, checking for collinearity of a set of points, or finding the intersection between two lines.
- Point, Vector
- Line, Segment, Ray
- Polygon, Triangle
gem install easy_geometry
x = EasyGeometry::D2::Point.new(0, 0)
y = EasyGeometry::D2::Point.new(1, 1)
z = EasyGeometry::D2::Point.new(2, 2)
w = EasyGeometry::D2::Point.new(1, 0)
EasyGeometry::D2::Point.is_collinear?(x, y, z) # true
EasyGeometry::D2::Point.is_collinear?(x, y, w) # false
t = EasyGeometry::D2::Triangle.new(x, y, w)
t.area # 0.5
t.perimeter # 3.414213562373095
s1 = EasyGeometry::D2::Segment.new([0, 0], [1, 1])
s2 = EasyGeometry::D2::Segment.new([0, 0], [-1, 1])
s3 = EasyGeometry::D2::Segment.new([0, 0], [1, 0])
s1.midpoint # Point(1/2, 1/2)
s3.length # 1
s1.intersection(s2) # [Point(0, 0)]
You can find more examples in documentetion and in specs
directory.
2019 Henry Metlov, released under the MIT license