Class: Sphere
- Inherits:
-
Object
- Object
- Sphere
- Defined in:
- lib/zadt/AbstractDataTypes/Geometrics/sphere.rb
Instance Attribute Summary collapse
-
#center ⇒ Object
readonly
Returns the value of attribute center.
-
#pct_error ⇒ Object
Returns the value of attribute pct_error.
-
#radius ⇒ Object
readonly
Returns the value of attribute radius.
Class Method Summary collapse
Instance Method Summary collapse
- #equation ⇒ Object
- #help ⇒ Object
- #how_far_from_sphere(point) ⇒ Object
- #in_sphere?(point) ⇒ Boolean
-
#initialize(radius = 1, center = [0,0,0], pct_error = 1) ⇒ Sphere
constructor
A new instance of Sphere.
- #inspect ⇒ Object
- #on_sphere?(point) ⇒ Boolean
- #surface_area ⇒ Object
- #volume ⇒ Object
Constructor Details
#initialize(radius = 1, center = [0,0,0], pct_error = 1) ⇒ Sphere
Returns a new instance of Sphere.
8 9 10 11 12 |
# File 'lib/zadt/AbstractDataTypes/Geometrics/sphere.rb', line 8 def initialize(radius = 1, center = [0,0,0], pct_error = 1) @radius = radius @center = Point.new(center) @pct_error = pct_error end |
Instance Attribute Details
#center ⇒ Object (readonly)
Returns the value of attribute center.
5 6 7 |
# File 'lib/zadt/AbstractDataTypes/Geometrics/sphere.rb', line 5 def center @center end |
#pct_error ⇒ Object
Returns the value of attribute pct_error.
6 7 8 |
# File 'lib/zadt/AbstractDataTypes/Geometrics/sphere.rb', line 6 def pct_error @pct_error end |
#radius ⇒ Object (readonly)
Returns the value of attribute radius.
5 6 7 |
# File 'lib/zadt/AbstractDataTypes/Geometrics/sphere.rb', line 5 def radius @radius end |
Class Method Details
.help ⇒ Object
14 15 16 |
# File 'lib/zadt/AbstractDataTypes/Geometrics/sphere.rb', line 14 def self.help Sphere. end |
Instance Method Details
#equation ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/zadt/AbstractDataTypes/Geometrics/sphere.rb', line 44 def equation dim_check(3) center_point = @center.dup coord_names = ["x", "y", "z"] center_point.coords.each_with_index do |center_coord, index| if center_coord == 0 # coord_name is fine elsif center_coord < 0 coord_names[index] = "(#{coord_names[index]} + #{-center_coord})" else coord_names[index] = "(#{coord_names[index]} - #{center_coord})" end end "#{coord_names[0]}^2 + #{coord_names[1]}^2 + #{coord_names[2]}^2 = #{@radius ** 2}" end |
#help ⇒ Object
18 19 20 |
# File 'lib/zadt/AbstractDataTypes/Geometrics/sphere.rb', line 18 def help Sphere.help end |
#how_far_from_sphere(point) ⇒ Object
30 31 32 |
# File 'lib/zadt/AbstractDataTypes/Geometrics/sphere.rb', line 30 def how_far_from_sphere(point) (@radius - Zadt::Universe.distance(@center, point)).abs end |
#in_sphere?(point) ⇒ Boolean
26 27 28 |
# File 'lib/zadt/AbstractDataTypes/Geometrics/sphere.rb', line 26 def in_sphere?(point) Zadt::Universe.distance(@center, point) <= @radius end |
#inspect ⇒ Object
60 61 62 |
# File 'lib/zadt/AbstractDataTypes/Geometrics/sphere.rb', line 60 def inspect "Sphere: #{equation}" end |
#on_sphere?(point) ⇒ Boolean
22 23 24 |
# File 'lib/zadt/AbstractDataTypes/Geometrics/sphere.rb', line 22 def on_sphere?(point) Zadt::Universe.distance(@center, point).round(2) == radius.round(2) end |
#surface_area ⇒ Object
39 40 41 42 |
# File 'lib/zadt/AbstractDataTypes/Geometrics/sphere.rb', line 39 def surface_area dim_check(3) 4.0 * Math::PI * (@radius ** 2) end |
#volume ⇒ Object
34 35 36 37 |
# File 'lib/zadt/AbstractDataTypes/Geometrics/sphere.rb', line 34 def volume dim_check(3) Math::PI * (@radius ** 3) * 4.0 / 3.0 end |