class Coords::Cartesian2d

Public Class Methods

new(x, y) click to toggle source
# File lib/coords/cartesian2d.rb, line 4
def initialize(x, y)
  @x = x
  @y = y
end

Public Instance Methods

==(point) click to toggle source
# File lib/coords/cartesian2d.rb, line 36
def ==(point)
  x.round(12) == point.x.round(12) && y.round(12) == point.y.round(12)
end
distance(point) click to toggle source
# File lib/coords/cartesian2d.rb, line 21
def distance(point)
  Math.sqrt(distance_squared(point))
end
distance_squared(point) click to toggle source
# File lib/coords/cartesian2d.rb, line 17
def distance_squared(point)
  ((x - point.x).abs ** 2) + ((y - point.y).abs ** 2)
end
reflect(type = 'origin') click to toggle source
# File lib/coords/cartesian2d.rb, line 63
def reflect(type = 'origin')
  reflected_point = Cartesian2d.new(x, y)
  reflected_point.reflect!(type)
  reflected_point
end
reflect!(type = 'origin') click to toggle source
# File lib/coords/cartesian2d.rb, line 69
def reflect!(type = 'origin')
  if type == 'line'
    tmp_x = x
    @x = y
    @y = tmp_x
  else
    @x = x * -1 if ['origin', 'y'].include?(type)
    @y = y * -1 if ['origin', 'x'].include?(type)
  end
end
rotate(theta) click to toggle source
# File lib/coords/cartesian2d.rb, line 51
def rotate(theta)
  rotated_point = Cartesian2d.new(x, y)
  rotated_point.rotate!(theta)
  rotated_point
end
rotate!(theta) click to toggle source
# File lib/coords/cartesian2d.rb, line 57
def rotate!(theta)
  tmp_x = x
  @x = ((x * Math.cos(theta)) - (y * Math.sin(theta))).round(12)
  @y = ((tmp_x * Math.sin(theta)) + (y * Math.cos(theta))).round(12)
end
to_polar() click to toggle source
# File lib/coords/cartesian2d.rb, line 29
def to_polar
  radius = Math.sqrt((x ** 2) + (y ** 2));
  theta = Math.atan2(y, x);

  Polar.new(radius.round(12), theta.round(12))
end
to_s() click to toggle source
# File lib/coords/cartesian2d.rb, line 25
def to_s
  x.to_s + ',' + y.to_s
end
translate(x2, y2) click to toggle source
# File lib/coords/cartesian2d.rb, line 40
def translate(x2, y2)
  translated_point = Cartesian2d.new(x, y)
  translated_point.translate!(x2, y2)
  translated_point
end
translate!(x2, y2) click to toggle source
# File lib/coords/cartesian2d.rb, line 46
def translate!(x2, y2)
  @x += x2
  @y += y2
end
x() click to toggle source
# File lib/coords/cartesian2d.rb, line 9
def x
  @x
end
y() click to toggle source
# File lib/coords/cartesian2d.rb, line 13
def y
  @y
end