Class: EpiMath::Point
- Inherits:
-
Object
- Object
- EpiMath::Point
- Defined in:
- lib/epimath100/point.class.rb
Class Method Summary collapse
Instance Method Summary collapse
- #*(p) ⇒ Object
-
#+(p) ⇒ Object
TODO : do not modify @.
- #-(p) ⇒ Object
- #==(p) ⇒ Object
-
#initialize(x, y, z = nil) ⇒ Point
constructor
A new instance of Point.
- #to_s ⇒ Object
- #x ⇒ Object
- #x=(v) ⇒ Object
- #y ⇒ Object
- #y=(v) ⇒ Object
- #z ⇒ Object
- #z=(v) ⇒ Object
Constructor Details
#initialize(x, y, z = nil) ⇒ Point
Returns a new instance of Point.
9 10 11 12 13 |
# File 'lib/epimath100/point.class.rb', line 9 def initialize x, y, z=nil MyError::Error.call "Point::new : a passed argument is not a valid number" if (!x.is_a?Numeric or !y.is_a?Numeric or (z != nil and !z.is_a?Numeric)) @coord = {:x => x.to_f, :y => y.to_f} @coord[:z] = z.to_f if z != nil end |
Class Method Details
.get_in(a, b, p) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/epimath100/point.class.rb', line 92 def self.get_in(a, b, p) MyError::Error.call "Point::get_in : an argument is not a Point" if !a.is_a?Point or !b.is_a?Point if a.x != b.x and p.is_a?Float and p >= 0 and p <= 1 coef = ((b.y-a.y) / (b.x-a.x)) ordonnee = a.y - coef * a.x min = [b.x, a.x].min max = [b.x, a.x].max mid = (max - min) * p return Point.new(min + mid, coef * (mid + min) + ordonnee) end return nil end |
.get_middle(a, b) ⇒ Object
88 89 90 |
# File 'lib/epimath100/point.class.rb', line 88 def self.get_middle(a, b) return Point.get_in(a, b, 0.5) end |
.new_a(p) ⇒ Object
15 16 17 18 |
# File 'lib/epimath100/point.class.rb', line 15 def self.new_a p MyError::Error.call "Point::new_a : not a valid array of coord" if !p.is_a?Array or p.size < 2 return Point.new(*p) end |
Instance Method Details
#*(p) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/epimath100/point.class.rb', line 43 def *(p) MyError::Error.call "Point::* : passed argument is invalid" if !p.is_a?Numeric @coord[:x] *= p @coord[:y] *= p @coord[:z] *= p if @coord[:z] return self end |
#+(p) ⇒ Object
TODO : do not modify @
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/epimath100/point.class.rb', line 21 def +(p) if p.is_a?Point @coord[:x] += p.x @coord[:y] += p.y @coord[:z] += p.z if p.z or @coord[:z] elsif p.is_a?Numeric @coord[:x] += p @coord[:y] += p @coord[:z] += p if @coord[:z] else MyError::Error.call "Point::+ : passed argument is invalid (#{p.class})" end return self end |
#-(p) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/epimath100/point.class.rb', line 36 def -(p) p_ = Point.new(-self.x, -self.y) p_ = Point.new(-self.x, -self.y, -self.z) if self.z p_ = p_ + p return p_ end |
#==(p) ⇒ Object
52 53 54 55 56 |
# File 'lib/epimath100/point.class.rb', line 52 def ==(p) MyError::Error.call "Point::== : passed argument is invalid" if !p.is_a?Point return true if p.x == self.x and p.y == self.y and p.z == self.z return false end |
#to_s ⇒ Object
58 59 60 61 62 |
# File 'lib/epimath100/point.class.rb', line 58 def to_s str = "(#{self.x}; #{self.y}" str += "; #{self.z}" if self.z str += ")" end |
#x ⇒ Object
64 65 66 |
# File 'lib/epimath100/point.class.rb', line 64 def x @coord[:x] end |
#x=(v) ⇒ Object
76 77 78 |
# File 'lib/epimath100/point.class.rb', line 76 def x= v @coord[:x] = v end |
#y ⇒ Object
68 69 70 |
# File 'lib/epimath100/point.class.rb', line 68 def y @coord[:y] end |
#y=(v) ⇒ Object
80 81 82 |
# File 'lib/epimath100/point.class.rb', line 80 def y= v @coord[:y] = v end |
#z ⇒ Object
72 73 74 |
# File 'lib/epimath100/point.class.rb', line 72 def z @coord[:z] end |
#z=(v) ⇒ Object
84 85 86 |
# File 'lib/epimath100/point.class.rb', line 84 def z= v @coord[:z] = v end |