Latest commit a024ab8 May 10, 2016 @bfoz bfoz Added min, max, and minmax to Path
It took a bit of yak-shaving to get it done


Geometry for Ruby

Build Status Gem Version

Classes and methods for the handling of all of the basic geometry that you learned in high school (and then forgot).

The classes in this libary are based on the Vector class provided by the Ruby standard library. Geometric primitives are generally assumed to lie in 2D space, but aren't necessarily restricted to it. Please let me know if you find cases that don't work in higher dimensions and I'll do my best to fix them.


Copyright 2012-2016 Brandon Fosdick and released under the BSD license.




point = Geometry::Point[3,4]    # 2D Point at coordinate 3, 4

# Copy constructors
point2 = Geometry::Point[point]
point2 = Geometry::Point[Vector[5,6]]

# Accessors
point[2]	# Same as point.z

# Zero   # A Point full of zeros of unspecified length      # Another way to do the same thing   # => Point[0,0,0]


# Two-point constructors
line = Geometry::Line[[0,0], [10,10]]
line = Geometry::Line[Geometry::Point[0,0], Geometry::Point[10,10]]
line = Geometry::Line[Vector[0,0], Vector[10,10]]

# Slope-intercept constructors
Geometry::Line[Rational(3,4), 5]	# Slope = 3/4, Intercept = 5
Geometry::Line[0.75, 5]

# Point-slope constructors
Geometry::Line(Geometry::Point[0,0], 0.75)
Geometry::Line(Vector[0,0], Rational(3,4))

# Special constructors (2D only)


# A Rectangle made from two corner points [1,2], [2,3] from:[1,2], to:[2,3] center:[1,2], size:[1,1]	# Using a center point and a size origin:[1,2], size:[1,1]	# Using an origin point and a size

# A Rectangle with its origin at [0, 0] and a size of [10, 20] size: [10, 20] size: Size[10, 20] width: 10, height: 20


# A circle at Point[1,2] with a radius of 3
circle = center:[1,2], radius:3


# A polygon that looks a lot like a square
polygon = [0,0], [1,0], [1,1], [0,1]

Regular Polygon

# Everyone loves a good hexagon
hexagon = 6, :diameter => 3

Zeros and Ones

# For when you know you need a zero, but you don't know how big it should be
zero =       # Returns a Point of indeterminate length that always compares equal to zero

# Oh, you wanted ones instead? No problem.
ones =        # => Point[1,1,1...1]

# Looking for something more exotic that a mere 1?
iso = Point.iso(5)      # => Point[5,5,5...5]