Class: HyperSphere

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

Direct Known Subclasses

Circle, Sphere

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(radius = 1, center = [0,0,0,0], pct_error = 1) ⇒ HyperSphere

Returns a new instance of HyperSphere.



7
8
9
10
11
# File 'lib/zadt/AbstractDataTypes/Geometrics/hypersphere.rb', line 7

def initialize(radius = 1, center = [0,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.



4
5
6
# File 'lib/zadt/AbstractDataTypes/Geometrics/hypersphere.rb', line 4

def center
  @center
end

#pct_errorObject

Returns the value of attribute pct_error.



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

def pct_error
  @pct_error
end

#radiusObject (readonly)

Returns the value of attribute radius.



4
5
6
# File 'lib/zadt/AbstractDataTypes/Geometrics/hypersphere.rb', line 4

def radius
  @radius
end

Class Method Details

.helpObject



13
14
15
# File 'lib/zadt/AbstractDataTypes/Geometrics/hypersphere.rb', line 13

def self.help
  Sphere.show_help_message
end

Instance Method Details

#equationObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/zadt/AbstractDataTypes/Geometrics/hypersphere.rb', line 37

def equation
  dims = @center.dims
  return circle_equation if dims == 2
  center_point = @center.dup
  # Only get the variables used in that dimension
  coord_names = ("a".."z").to_a.slice(-dims, dims)
  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
  final_string = ""
  (dims - 1).times do |index|
    final_string += "#{coord_names[index]}^2 + "
  end
  final_string += "#{coord_names[dims - 1]}^2"
  final_string += " = "
  final_string += "#{@radius ** 2}"
  final_string
end

#helpObject



17
18
19
# File 'lib/zadt/AbstractDataTypes/Geometrics/hypersphere.rb', line 17

def help
  Sphere.help
end

#how_far(point) ⇒ Object



33
34
35
# File 'lib/zadt/AbstractDataTypes/Geometrics/hypersphere.rb', line 33

def how_far(point)
  (@radius - Zadt::Universe.distance(@center, point)).abs
end

#inside?(point) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/zadt/AbstractDataTypes/Geometrics/hypersphere.rb', line 25

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

#on?(point) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/zadt/AbstractDataTypes/Geometrics/hypersphere.rb', line 21

def on?(point)
  Zadt::Universe.distance(@center, point).round(2) == radius.round(2)
end

#outside?(point) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/zadt/AbstractDataTypes/Geometrics/hypersphere.rb', line 29

def outside?(point)
  !inside?(point)
end