Class: Fron::Point
Overview
Simple class for point with x and y coordinates.
Instance Attribute Summary collapse
-
#x ⇒ Numeric
Sets / gets the x portion of the point.
-
#y ⇒ Numeric
Sets / gets the y portion of the point.
Instance Method Summary collapse
-
#*(other) ⇒ Core::Point
Multiplies the point by given scalar value.
-
#+(other) ⇒ Core::Point
Adds two points together.
-
#-(other) ⇒ Core::Point
Returns the difference from an other point.
-
#/(other) ⇒ Core::Point
Divides the point by given scalar value.
- #clamp(width, height) ⇒ Object
-
#distance ⇒ Numeric
Returns the distance between this point and 0, 0.
-
#initialize(x = 0, y = 0) ⇒ Point
constructor
Creates a new instance.
- #to_h ⇒ Object
-
#to_s ⇒ String
Returns the string representation of the point.
Constructor Details
#initialize(x = 0, y = 0) ⇒ Point
Creates a new instance
20 21 22 23 |
# File 'opal/fron/utils/point.rb', line 20 def initialize(x = 0, y = 0) @x = x @y = y end |
Instance Attribute Details
#x ⇒ Numeric
Sets / gets the x portion of the point
8 9 10 |
# File 'opal/fron/utils/point.rb', line 8 def x @x end |
#y ⇒ Numeric
Sets / gets the y portion of the point
14 15 16 |
# File 'opal/fron/utils/point.rb', line 14 def y @y end |
Instance Method Details
#*(other) ⇒ Core::Point
Multiplies the point by given scalar value
48 49 50 |
# File 'opal/fron/utils/point.rb', line 48 def *(other) self.class.new x * other, y * other end |
#+(other) ⇒ Core::Point
Adds two points together
39 40 41 |
# File 'opal/fron/utils/point.rb', line 39 def +(other) self.class.new x + other.x, y + other.y end |
#-(other) ⇒ Core::Point
Returns the difference from an other point.
30 31 32 |
# File 'opal/fron/utils/point.rb', line 30 def -(other) self.class.new x - other.x, y - other.y end |
#/(other) ⇒ Core::Point
Divides the point by given scalar value
57 58 59 |
# File 'opal/fron/utils/point.rb', line 57 def /(other) self.class.new x / other, y / other end |
#clamp(width, height) ⇒ Object
76 77 78 79 80 |
# File 'opal/fron/utils/point.rb', line 76 def clamp(width, height) @x = @x.clamp(0, width) @y = @y.clamp(0, height) self end |
#distance ⇒ Numeric
Returns the distance between this point and 0, 0
65 66 67 |
# File 'opal/fron/utils/point.rb', line 65 def distance Math.sqrt(@x**2 + @y**2) end |
#to_h ⇒ Object
82 83 84 |
# File 'opal/fron/utils/point.rb', line 82 def to_h { x: x, y: y } end |
#to_s ⇒ String
Returns the string representation of the point
72 73 74 |
# File 'opal/fron/utils/point.rb', line 72 def to_s "[#{x}, #{y}]" end |