GuiGeo

Gem Version Downloads GitHub Build Ruby Style Guide MIT License

There are a few good geometry gems already available for Ruby (ruby-geometry and geometry), but I need one which is focused on the needs of creating 2D graphical user interfaces.

GuiGeo2

GuiGeo2 vs GuiGeo

  • Ruby 3+: minor changes
  • Added workflow: Github testing with multiple versions of Ruby
  • Added benchmarks
  • Removed method disjoint
  • Removed Guardfile
  • Linter: using gem standard
  • Restructuring
  • Rspec replaced by Minitest
  • Breaking change: rect & rect means intersection
  • Breaking change: rect | rect means union

Installation

As usual:

# Gemfile
...
gem "gui_geometry2"
...

and run "bundle install".

Usage (copied from GuiGeo)

Points are represented by X and Y coordinates:

Rectangles consist of two point objects. One specifies the location of the rectangle - it's top-left corner. The other specifies its size in terms of width then height.

require "gui_geometry"
include GuiGeo2

# create point x == y == 0
point
# => point(0, 0)

# create a point x=3, y=4
my_point = point(3, 4)
# => point(3, 4)

[my_point.x, my_point.y]
# => [3, 4]

# create a rectangle - x, y, w, h form
rect(4, 5, 100, 80)
# => rect(4, 5, 100, 80)

# create rectangle - location, size form
my_rect = rect point(4, 5), point(100, 80)
# => rect(4, 5, 100, 80)

# create rectangle - size form (location defaults to 0,0
rect point(30, 50)
# => rect(0, 0, 30, 50)

# get loc and size:
[my_rect.loc, my_rect.size]
# => [point(4, 5), point(100, 80)]

# get x, y, width and height
[my_rect.x, my_rect.y, my_rect.w, my_rect.h]
# => [4, 5, 100, 80]

# get the 4 corners:
[my_rect.tl, my_rect.tr, my_rect.bl, my_rect.br]
# => [point(4, 5), point(104, 5), point(4, 85), point(104, 85)]

To get access to min, max, bound, and minmax:

include GuiGeo2::Tools

min(4, 5)
# => 4

max(4, 5)
# => 5

minmax(4, 5)
# => [4, 5]

bound(5, 4, 10)
# => 5