class Coords::Polar
Public Class Methods
new(radius, theta)
click to toggle source
# File lib/coords/polar.rb, line 4 def initialize(radius, theta) @radius = radius @theta = theta end
Public Instance Methods
==(point)
click to toggle source
# File lib/coords/polar.rb, line 41 def ==(point) radius.round(12) == point.radius.round(12) && theta.round(12) == point.theta.round(12) end
distance(point)
click to toggle source
# File lib/coords/polar.rb, line 26 def distance(point) Math.sqrt(distance_squared(point)) end
distance_squared(point)
click to toggle source
# File lib/coords/polar.rb, line 17 def distance_squared(point) x1 = radius * Math.cos(theta) x2 = point.radius * Math.cos(point.theta) y1 = radius * Math.sin(theta) y2 = point.radius * Math.sin(point.theta) ((x2 - x1).abs ** 2) + ((y2 - y1).abs ** 2) end
radius()
click to toggle source
# File lib/coords/polar.rb, line 9 def radius @radius end
rotate(radians)
click to toggle source
# File lib/coords/polar.rb, line 61 def rotate(radians) rotated_point = Polar.new(radius, theta) rotated_point.rotate!(radians) rotated_point end
rotate!(radians)
click to toggle source
# File lib/coords/polar.rb, line 67 def rotate!(radians) @theta += radians @theta -= (2 * Math::PI) while theta > (2 * Math::PI) @theta += (2 * Math::PI) while theta < 0 end
theta()
click to toggle source
# File lib/coords/polar.rb, line 13 def theta @theta end
to_cartesian()
click to toggle source
# File lib/coords/polar.rb, line 34 def to_cartesian x = radius * Math.cos(theta) y = radius * Math.sin(theta) Cartesian2d.new(x.round(12), y.round(12)) end
to_s()
click to toggle source
# File lib/coords/polar.rb, line 30 def to_s radius.to_s + ',' + theta.to_s end
translate(radius2, theta2)
click to toggle source
# File lib/coords/polar.rb, line 45 def translate(radius2, theta2) translated_point = Polar.new(radius, theta) translated_point.translate!(radius2, theta2) translated_point end
translate!(radius2, theta2)
click to toggle source
# File lib/coords/polar.rb, line 51 def translate!(radius2, theta2) c1 = self.to_cartesian c2 = Polar.new(radius2, theta2).to_cartesian c3 = c1.translate(c2.x, c2.y) p = c3.to_polar @radius = p.radius @theta = p.theta end