Class: Coords::Cartesian3d

Inherits:
Cartesian2d show all
Defined in:
lib/coords/cartesian3d.rb

Instance Method Summary collapse

Methods inherited from Cartesian2d

#distance, #reflect, #reflect!, #to_polar, #x, #y

Constructor Details

#initialize(x, y, z) ⇒ Cartesian3d

Returns a new instance of Cartesian3d.



4
5
6
7
# File 'lib/coords/cartesian3d.rb', line 4

def initialize(x, y, z)
  super(x, y)
  @z = z
end

Instance Method Details

#==(point) ⇒ Object



29
30
31
# File 'lib/coords/cartesian3d.rb', line 29

def ==(point)
  x.round(12) == point.x.round(12) && y.round(12) == point.y.round(12) && z.round(12) == point.z.round(12)
end

#distance_squared(point) ⇒ Object



13
14
15
# File 'lib/coords/cartesian3d.rb', line 13

def distance_squared(point)
  ((x - point.x).abs ** 2) + ((y - point.y).abs ** 2) + ((z - point.z).abs ** 2)
end

#rotate(rZ = 0, rX = 0, rY = 0) ⇒ Object



44
45
46
47
48
# File 'lib/coords/cartesian3d.rb', line 44

def rotate(rZ = 0, rX = 0, rY = 0)
  rotated_point = Cartesian3d.new(x, y, z)
  rotated_point.rotate!(rZ, rX, rY)
  rotated_point
end

#rotate!(rZ = 0, rX = 0, rY = 0) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/coords/cartesian3d.rb', line 50

def rotate!(rZ = 0, rX = 0, rY = 0)
  # rotate clockwise around z axis
  d = Math.hypot(y, x)
  theta = Math.atan2(y, x) + rZ
  @x = d * Math.cos(theta)
  @y = d * Math.sin(theta)

  # rotate clockwise around x axis
  d = Math.hypot(y, z)
  theta = Math.atan2(z, y) + rX
  @y = d * Math.cos(theta)
  @z = d * Math.sin(theta)

  # rotate clockwise around y axis
  d = Math.hypot(x, z)
  theta = Math.atan2(x, z) + rY
  @z = d * Math.cos(theta)
  @x = d * Math.sin(theta)

  @x = x.round(12)
  @y = y.round(12)
  @z = z.round(12)
end

#to_sObject



17
18
19
# File 'lib/coords/cartesian3d.rb', line 17

def to_s
  x.to_s + ',' + y.to_s + ',' + z.to_s
end

#to_sphericalObject



21
22
23
24
25
26
27
# File 'lib/coords/cartesian3d.rb', line 21

def to_spherical
  radius = Math.sqrt((x ** 2) + (y ** 2) + (z ** 2));
  theta = Math.acos(z / radius)
  phi = Math.atan2(y, x)

  Spherical.new(radius.round(12), theta.round(12), phi.round(12))
end

#translate(x2, y2, z2) ⇒ Object



33
34
35
36
37
# File 'lib/coords/cartesian3d.rb', line 33

def translate(x2, y2, z2)
  translated_point = Cartesian3d.new(x, y, z)
  translated_point.translate!(x2, y2, z2)
  translated_point
end

#translate!(x2, y2, z2) ⇒ Object



39
40
41
42
# File 'lib/coords/cartesian3d.rb', line 39

def translate!(x2, y2, z2)
  super(x2, y2)
  @z += z2
end

#zObject



9
10
11
# File 'lib/coords/cartesian3d.rb', line 9

def z
  @z
end