Easy Geometry
Geometry for Ruby
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.
Available geometry entities
Point, Vector
Line, Segment, Ray
Polygon, Triangle
Installation
gem install easy_geometry
Examples
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 # <EasyGeometry::D2::Point:0x00007fabe69124d8 @x=(1/2), @y=(1/2)>
s3.length # 1
s1.intersection(s2) # [#<EasyGeometry::D2::Point:0x00007fc2728713d8 @x=(0/1), @y=(0/1)>]
You can find more examples in specs
directory and here.
License
2019 Henry Metlov, released under the MIT license
Last updated
Was this helpful?