Class: Coords::Polar
- Inherits:
-
Object
- Object
- Coords::Polar
- Defined in:
- lib/coords/polar.rb
Direct Known Subclasses
Instance Method Summary collapse
- #==(point) ⇒ Object
- #distance(point) ⇒ Object
- #distance_squared(point) ⇒ Object
-
#initialize(radius, theta) ⇒ Polar
constructor
A new instance of Polar.
- #radius ⇒ Object
- #rotate(radians) ⇒ Object
- #rotate!(radians) ⇒ Object
- #theta ⇒ Object
- #to_cartesian ⇒ Object
- #to_s ⇒ Object
- #translate(radius2, theta2) ⇒ Object
- #translate!(radius2, theta2) ⇒ Object
Constructor Details
#initialize(radius, theta) ⇒ Polar
Returns a new instance of Polar.
4 5 6 7 |
# File 'lib/coords/polar.rb', line 4 def initialize(radius, theta) @radius = radius @theta = theta end |
Instance Method Details
#==(point) ⇒ Object
41 42 43 |
# 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) ⇒ Object
26 27 28 |
# File 'lib/coords/polar.rb', line 26 def distance(point) Math.sqrt(distance_squared(point)) end |
#distance_squared(point) ⇒ Object
17 18 19 20 21 22 23 24 |
# 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 ⇒ Object
9 10 11 |
# File 'lib/coords/polar.rb', line 9 def radius @radius end |
#rotate(radians) ⇒ Object
61 62 63 64 65 |
# 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) ⇒ Object
67 68 69 70 71 |
# 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 ⇒ Object
13 14 15 |
# File 'lib/coords/polar.rb', line 13 def theta @theta end |
#to_cartesian ⇒ Object
34 35 36 37 38 39 |
# 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 ⇒ Object
30 31 32 |
# File 'lib/coords/polar.rb', line 30 def to_s radius.to_s + ',' + theta.to_s end |
#translate(radius2, theta2) ⇒ Object
45 46 47 48 49 |
# 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) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# 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 |