Class: Point3c

Inherits:
Object show all
Defined in:
lib/point3c.rb

Overview

Point3c represents a 3D point in cylindrical coordinates:

  • c_radius is the distance from the cylindrical axis.

  • theta is the angular distance around the cylindrical axis.

  • z is the distance from the cylindrical base-plane.

The cylindrical theta coordinate is equal to the spherical theta. The cylindrical z coordinate is equal to the cartesian z coordinate.

From a cartesian reference, the cylindrical axis is the z axis, theta is measured using the right hand rule with the positive x axis representing 0, and the cylindrical plane is the plane z=0.

Point3c instances may be converted to Point3 and Point3s instances, but information at the “boundaries” may be lost. Besides responding as a Point3c, an instance will also respond like a Point3 and Point3s as it has a full complement of readers for the different coordinate systems.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(c_radius = 0, theta = 0, z = 0) ⇒ Point3c

Creates and returns a Point3c instance. Sets the coordinates values using the set method.



37
38
39
# File 'lib/point3c.rb', line 37

def initialize(c_radius=0, theta=0, z=0)
  set c_radius, theta, z
end

Instance Attribute Details

#c_radiusObject

cylindrical radius coordinate reader and writer.



23
24
25
# File 'lib/point3c.rb', line 23

def c_radius
  @c_radius
end

#thetaObject

cylindrical theta reader.



27
28
29
# File 'lib/point3c.rb', line 27

def theta
  @theta
end

#zObject

cylindrical z coordinate reader and writer.



25
26
27
# File 'lib/point3c.rb', line 25

def z
  @z
end

Instance Method Details

#==(point3c) ⇒ Object

Returns true if the coordinates of the instance are effectively equal to the coordinates of the given point.



64
65
66
67
# File 'lib/point3c.rb', line 64

def ==(point3c)
  ((point3c.c_radius == c_radius) && (point3c.z == z)) &&
    (((c_radius == 0) && (z == 0)) || (point3c.theta == theta))
end

#approximately_equals?(point3c, epsilon = Numeric.epsilon) ⇒ Boolean Also known as: =~

Returns true if the coordinates of the instance are approximately effectively equal to the coordinates of the given point, each coordinate less than a distance epsilon from the target.

Returns:

  • (Boolean)


72
73
74
75
76
77
78
# File 'lib/point3c.rb', line 72

def approximately_equals?(point3c,epsilon=Numeric.epsilon)
  (@c_radius.approximately_equals?(point3c.c_radius,epsilon) &&
    @z.approximately_equals?(point3c.z,epsilon)) &&
   ((@c_radius.approximately_equals?(0,epsilon) &&
     @z.approximately_equals?(0,epsilon)) ||
    @theta.approximately_equals?(point3c.theta,epsilon))
end

#phiObject

Returns the spherical phi coordinate of the instance.



118
119
120
121
# File 'lib/point3c.rb', line 118

def phi
  m = s_radius
  (m == 0)? 0 : Math.acos(z/m)
end

#point3(point3 = nil, point3c = self) ⇒ Object

Returns a 3D point in cartesian coordinates, filling point3 if given, and copied from point3c.



84
85
86
# File 'lib/point3c.rb', line 84

def point3(point3=nil,point3c=self)
  (point3 ||= Point3.new).set(point3c.x,point3c.y,point3c.z)
end

#point3c(c_radius = self.c_radius, theta = self.theta, z = self.z) ⇒ Object

Returns a copy of point3c with the given cylindrical coordinates:

  • c_radius is Numeric, the arguments are copied as the coordinates.

  • c_radius responds like a Point3c, its coordinates are copied.

  • otherwise a TypeError is raised.



98
99
100
# File 'lib/point3c.rb', line 98

def point3c(c_radius=self.c_radius,theta=self.theta,z=self.z)
  Point3c.new(c_radius,theta,z)
end

#point3s(point3s = nil, point3c = self) ⇒ Object

Returns a 3D point in spherical coordinates, filling point3s if given, and copied from point3c.



90
91
92
# File 'lib/point3c.rb', line 90

def point3s(point3s=nil,point3c=self)
  (point3s ||= Point3s.new).set(point3c.s_radius,point3c.theta,point3c.phi)
end

#s_radiusObject

Returns the spherical radius coordinate of the instance.



113
114
115
# File 'lib/point3c.rb', line 113

def s_radius
  Math.sqrt((x**2)+(y**2)+(z**2))
end

#set(c_radius = 0, theta = 0, z = 0) ⇒ Object

Sets the coordinate values of the instance. When

  • c_radius is Numeric, the arguments are interpretted as coordinates.

  • c_radius responds like a Point3c, its cylindrical coordinates are assigned.

  • otherwise a TypeError is raised.

The modified instance is returned.



51
52
53
54
55
56
57
58
59
60
# File 'lib/point3c.rb', line 51

def set(c_radius=0, theta=0, z=0)
  if c_radius.kind_of? Numeric
    @c_radius, @theta, @z = 1.0*c_radius, (1.0*theta).rectify_theta, 1.0*z
  elsif c_radius.point3c_like?
    set c_radius.c_radius, c_radius.theta, c_radius.z
  else
    raise_no_conversion c_radius
  end
  self
end

#to_sObject

Returns a string representation of the instance.



42
43
44
# File 'lib/point3c.rb', line 42

def to_s
  "Point3c: c_radius #{c_radius}  theta #{theta}  z #{z}"
end

#xObject

Returns the cartesian x coordinate of the instance.



103
104
105
# File 'lib/point3c.rb', line 103

def x
  c_radius*Math.cos(theta)
end

#yObject

Returns the cartesian y coordinate of the instance.



108
109
110
# File 'lib/point3c.rb', line 108

def y
  c_radius*Math.sin(theta)
end