Module: Geo2D

Defined in:
lib/geo2d.rb

Overview

Planar geometry of points and line-strings

Defined Under Namespace

Classes: LineSegment, LineString, Vector

Class Method Summary collapse

Class Method Details

.Line(*args) ⇒ Object

Line-string constructor



496
497
498
499
500
501
502
# File 'lib/geo2d.rb', line 496

def Line(*args)
  #if args.size<3
  #  LineSegment.new(*args.map{|arg| Vector(arg)})
  #else
    LineString.new(*args.map{|arg| Vector(arg)})
  #end
end

.LineSegment(start_point, end_point) ⇒ Object

Segment constructor



491
492
493
# File 'lib/geo2d.rb', line 491

def LineSegment(start_point, end_point)
  LineSegment.new(Vector(start_point), Vector(end_point))
end

.Point(*args) ⇒ Object



486
487
488
# File 'lib/geo2d.rb', line 486

def Point(*args)
  Vector(*args)
end

.rotation(center, angle) ⇒ Object

Rotation transformation; given the center of rotation (a point, i.e. a Vector) and the angle this returns a procedure that can be used to apply the rotation to points.



512
513
514
515
# File 'lib/geo2d.rb', line 512

def rotation(center, angle)
  center = Vector(center)
  lambda{|p| center + (p-center).rotate(angle)}
end

.rotation_transform(angle) ⇒ Object



504
505
506
507
508
# File 'lib/geo2d.rb', line 504

def rotation_transform(angle)
  sn = Math.sin(angle)
  cs = Math.cos(angle)
  [cs, -sn, sn, cs]
end

.Vector(*args) ⇒ Object

Vector constructor



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/geo2d.rb', line 140

def Vector(*args)
  case args.size
  when 2
    x, y = args
  when 1
    arg = args.first
    if arg.is_a?(Vector)
      return arg
    elsif arg.kind_of?(Array) && arg.size==2
      x, y = arg
    elsif arg.kind_of?(Hash)
      if arg.has_key?(:x) && arg.has_key?(:y)
        x, y = arg[:x], arg[:y]
      end
    else
      if arg.respond_to?(:x) && arg.respond_to?(:y)
        x, y = arg.x, arg.y
      else
        raise ArgumentError,"Invalid point definition"
      end
    end
  else
    raise ArgumentError,"Invalid number of parameters for a point"
  end
  Vector.new(x,y)
end