Class: Fron::Point

Inherits:
Object show all
Defined in:
opal/fron/utils/point.rb

Overview

Simple class for point with x and y coordinates.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x = 0, y = 0) ⇒ Point

Creates a new instance

Parameters:

  • x (Float) (defaults to: 0)

    The x coordiante

  • y (Float) (defaults to: 0)

    The y coordiante



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

#xNumeric

Sets / gets the x portion of the point

Parameters:

Returns:



8
9
10
# File 'opal/fron/utils/point.rb', line 8

def x
  @x
end

#yNumeric

Sets / gets the y portion of the point

Parameters:

Returns:



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

Parameters:

  • other (Numeric)

    The scalar value

Returns:

  • (Core::Point)

    The result



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

Parameters:

  • other (Core::Point)

    The other point

Returns:

  • (Core::Point)

    The result



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.

Parameters:

  • other (Core::Point)

    The point to caluclate the difference from

Returns:

  • (Core::Point)

    The difference



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

Parameters:

  • other (Numeric)

    The scalar value

Returns:

  • (Core::Point)

    The result



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

#distanceNumeric

Returns the distance between this point and 0, 0

Returns:



65
66
67
# File 'opal/fron/utils/point.rb', line 65

def distance
  Math.sqrt(@x**2 + @y**2)
end

#to_hObject



82
83
84
# File 'opal/fron/utils/point.rb', line 82

def to_h
  { x: x, y: y }
end

#to_sString

Returns the string representation of the point

Returns:

  • (String)

    The representation



72
73
74
# File 'opal/fron/utils/point.rb', line 72

def to_s
  "[#{x}, #{y}]"
end