GithubHelp home page GithubHelp logo

bfoz / geometry Goto Github PK

View Code? Open in Web Editor NEW
47.0 5.0 19.0 322 KB

Geometric primitives for Ruby

Home Page: https://rubygems.org/gems/geometry

License: BSD 2-Clause "Simplified" License

Ruby 100.00%
ruby geometry geometry-library geometry-processing geometric-shapes

geometry's Introduction

Geometry for Ruby

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.

License

Copyright 2012-2024 Brandon Fosdick [email protected] and released under the BSD license.

Primitives

Examples

Point

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.x
point.y
point[2]	# Same as point.z

# Zero
PointZero.new   # A Point full of zeros of unspecified length
Point.zero      # Another way to do the same thing
Point.zero(3)   # => Point[0,0,0]

Line

# 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)
Geometry::Line.horizontal(y=0)
Geometry::Line.vertical(x=0)

Rectangle

# A Rectangle made from two corner points
Geometry::Rectangle.new [1,2], [2,3]
Geometry::Rectangle.new from:[1,2], to:[2,3]

Geometry::Rectangle.new center:[1,2], size:[1,1]	# Using a center point and a size
Geometry::Rectangle.new 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]
Geometry::Rectangle.new size: [10, 20]
Geometry::Rectangle.new size: Size[10, 20]
Geometry::Rectangle.new width: 10, height: 20

Circle

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

Polygon

# A polygon that looks a lot like a square
polygon = Geometry::Polygon.new [0,0], [1,0], [1,1], [0,1]

Regular Polygon

# Everyone loves a good hexagon
hexagon = Geometry::RegularPolygon.new 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 = Point.zero       # Returns a Point of indeterminate length that always compares equal to zero

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

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

geometry's People

Contributors

bfoz avatar krzysiekherod avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

geometry's Issues

Allow center==nil in equality checking

A Circle or RegularPolygon with a nil center should be equal to another class of the same type with the same radius or diameter (and number of sides, for RegularPolygon)

Fill out the Polygon class cluster

Methods

  • inside?
    • checks if a passed point is inside, or on the boundary
    • checks if a passed edge is entirely inside, or on the boundary
    • checks if a passed polygon is entirely inside, or matches the boundary entirely
  • outside? - opposite of inside?
  • intersect?
    • true if passed line intersects the polygon
    • true if any part of the passed polygon is inside?
  • equal? polygon - true if passed polygon has same vertices, and number of vertices
  • on_boundary?
    • true if passed point is a vertex or on an edge
    • true if passed line is an edge or entirely on an edge
    • true if passed polygon is equal?
  • vertex? point - true if passed point is a vertex of the polygon
  • edge? line - true if passed line is an edge of the vertex
  • edge? point - true if passed point in on any edge
  • concave?
  • convex?
  • decompose - cuts a concave polygon into 1 or more convex polygons
  • hull - creates a convex hull
  • square - smallest enclosing Square
  • rectangle - smallest enclosing Rectangle

Cluster

  • RegularPolygon

Add a PolyLine class

A sequence of Edges that may, or may not, be closed. It's like a Polygon in that it only contains straight lines, but also like a Path in that it isn't necessarily closed.

Add a Path class

Like a Polygon, but not necessarily closed and not limited to straight lines

Add min, max, and minmax methods to Rectangle

min returns a Point that's on the Rectangle and closest to the origin
max returns a Point that's on the Rectangle and farthest from the origin
minmax returns the Array of Points [min, max]

Fill out the Line class cluster

Needs accessors, etc

  • parallel? line - true if the passed line is parallel
  • intersect? line - true if the passed line intersects
  • angle - the angle between the two lines
  • direction - a vector pointing along the line
  • equal? line - true if collinear
  • distance
    • point - distance to the point
    • line - closest distance between the lines
    • circle - distance to the circle's center minus its radius

RegularPolygon

A Polygon that's constrained to be a regular polygon. Has accessors for radius and center.

Add RegularPolygon#circle

circle should return a Circle that encloses the RegularPolygon

incircle should return a Circle that incribes the RegularPolygon

Composition

Adding two elements should return a Path that's the union of the elements, or an exception if they don't intersect.

Line testing

Fix test framework for Line class

  • Rename file
  • Rename test case (not a Point test)
  • Add test cases for individual cluster classes

Add an inset method to Rectangle

Generates a new Rectangle that's inset by the specified amount. Overloads for single inset all around, horizontal/vertical insets, and top/left/bottom/right insets. Negative numbers produce an "outset".

Module convenience methods

  • pentagon - returns a regular pentagon
  • hexagon - returns a regular hexagon
  • regular_polygon - constructs a regular polygon with a specified number of edges

Add a Rotation class

Represents the concept of a rotation regardless of the underlying implementation. Has methods for getting the equivalent rotation matrix and the equivalent quaternion.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.