Class: Sphere

Inherits:
Object
  • Object
show all
Defined in:
lib/zadt/AbstractDataTypes/Geometrics/sphere.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#centerObject (readonly)

Returns the value of attribute center.



5
6
7
# File 'lib/zadt/AbstractDataTypes/Geometrics/sphere.rb', line 5

def center
  @center
end

#pct_errorObject

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

#radiusObject (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

.helpObject



14
15
16
# File 'lib/zadt/AbstractDataTypes/Geometrics/sphere.rb', line 14

def self.help
  Sphere.show_help_message
end

Instance Method Details

#equationObject



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

#helpObject



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

Returns:

  • (Boolean)


26
27
28
# File 'lib/zadt/AbstractDataTypes/Geometrics/sphere.rb', line 26

def in_sphere?(point)
  Zadt::Universe.distance(@center, point) <= @radius
end

#inspectObject



60
61
62
# File 'lib/zadt/AbstractDataTypes/Geometrics/sphere.rb', line 60

def inspect
  "Sphere: #{equation}"
end

#on_sphere?(point) ⇒ Boolean

Returns:

  • (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_areaObject



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

#volumeObject



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