Class: Eymiha::Point3s

Inherits:
Object show all
Includes:
ThreeDimensions
Defined in:
lib/eymiha/math3/point3s.rb

Overview

Point3s represents a 3D point in spherical coordinates:

  • s_radius is the distance from the origin.

  • theta is the angular distance around the cylindrical axis.

  • phi is angle that rotates the cylindrical axis to be directed at the point.

The spherical theta coordinate is equal to the cylindrical theta.

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 phi is an angle measured from the positive z axis.

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ThreeDimensions

#half_pi, #origin, #pi, #quaternion, #sqrt2, #sqrt3, #two_pi

Constructor Details

#initialize(s_radius = 0, theta = 0, phi = 0) ⇒ Point3s

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



44
45
46
# File 'lib/eymiha/math3/point3s.rb', line 44

def initialize(s_radius=0, theta=0, phi=0)
  set s_radius, theta, phi
end

Instance Attribute Details

#phiObject

spherical phi coordinate reader.



28
29
30
# File 'lib/eymiha/math3/point3s.rb', line 28

def phi
  @phi
end

#s_radiusObject

spherical radius coordinate reader and writer.



24
25
26
# File 'lib/eymiha/math3/point3s.rb', line 24

def s_radius
  @s_radius
end

#thetaObject

spherical theta coordinate reader.



26
27
28
# File 'lib/eymiha/math3/point3s.rb', line 26

def theta
  @theta
end

Instance Method Details

#==(point3s) ⇒ Object

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



73
74
75
76
# File 'lib/eymiha/math3/point3s.rb', line 73

def ==(point3s)
  (point3s.s_radius == s_radius) &&
    ((s_radius == 0) || ((point3s.theta == theta) && (point3s.phi == phi)))
end

#approximately_equals?(point3s, 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)


81
82
83
84
85
86
# File 'lib/eymiha/math3/point3s.rb', line 81

def approximately_equals?(point3s,epsilon=Numeric.epsilon)
  (@s_radius.approximately_equals?(point3s.s_radius,epsilon)&&
   ((@s_radius.approximately_equals?(0,epsilon) ||
     (@theta.approximately_equals?(point3s.theta,epsilon)&&
      @phi.approximately_equals?(point3s.phi,epsilon)))))
end

#c_radiusObject

Returns the cylindrical radius coordinate of the instance.



126
127
128
# File 'lib/eymiha/math3/point3s.rb', line 126

def c_radius
  s_radius*Math.sin(phi)
end

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

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



92
93
94
# File 'lib/eymiha/math3/point3s.rb', line 92

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

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

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



106
107
108
# File 'lib/eymiha/math3/point3s.rb', line 106

def point3c(point3c=nil,point3s=self)
  (point3c ||= Point3c.new).set(point3s.c_radius,point3s.theta,point3s.z)
end

#point3s(s_radius = self.s_radius, theta = self.theta, phi = self.phi) ⇒ Object

Returns a copy of point3s with the given spherical coordinates:

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

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

  • otherwise a TypeError is raised.



100
101
102
# File 'lib/eymiha/math3/point3s.rb', line 100

def point3s(s_radius=self.s_radius,theta=self.theta,phi=self.phi)
  Point3s.new(s_radius,theta,phi)
end

#set(s_radius = 0, theta = 0, phi = 0) ⇒ Object

Returns a string representation of the instance. Sets the coordinate values of the instance. When

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

  • s_radius responds like a Point3c, its spherical coordinates are assigned.

  • otherwise a TypeError is raised.

The modified instance is returned.



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/eymiha/math3/point3s.rb', line 59

def set(s_radius=0, theta=0, phi=0)
  if s_radius.kind_of? Numeric
    @s_radius, @theta, @phi =
      1.0*s_radius, (1.0*theta).rectify_theta, (1.0*phi).rectify_phi
  elsif s_radius.point3s_like?
    set s_radius.s_radius, s_radius.theta, s_radius.phi
  else
    raise_no_conversion s_radius
  end
  self
end

#to_sObject

Returns a string representation of the instance.



49
50
51
# File 'lib/eymiha/math3/point3s.rb', line 49

def to_s
  "Point3s: s_radius #{s_radius}  theta #{theta}  phi #{phi}"
end

#xObject

Returns the cartesian x coordinate of the instance.



111
112
113
# File 'lib/eymiha/math3/point3s.rb', line 111

def x
  s_radius*Math.cos(theta)*Math.sin(phi)
end

#yObject

Returns the cartesian y coordinate of the instance.



116
117
118
# File 'lib/eymiha/math3/point3s.rb', line 116

def y
  s_radius*Math.sin(theta)*Math.sin(phi)
end

#zObject

Returns the cartesian z coordinate of the instance.



121
122
123
# File 'lib/eymiha/math3/point3s.rb', line 121

def z
  s_radius*Math.cos(phi);
end