Almost working from 1st try
This commit is contained in:
commit
c6547c1121
8 changed files with 297 additions and 0 deletions
42
point.lua
Normal file
42
point.lua
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
-- Represents 2D point
|
||||
---@class Point
|
||||
Point = { x = 1, y = 1 }
|
||||
|
||||
-- Point factory
|
||||
---@param x number | nil
|
||||
---@param y number | nil
|
||||
---@return any
|
||||
function Point:new( x, y )
|
||||
local point = {
|
||||
x = x or x,
|
||||
y = y or y
|
||||
}
|
||||
|
||||
setmetatable( point, { __index = self } )
|
||||
|
||||
return point
|
||||
end
|
||||
|
||||
-- Comparing function
|
||||
---@param point self
|
||||
---@return boolean
|
||||
function Point:equals( point )
|
||||
return self.x == point.x and self.y == point.y
|
||||
end
|
||||
|
||||
-- Get self coordinates
|
||||
---@return number, number
|
||||
function Point:coords()
|
||||
return self.x, self.y
|
||||
end
|
||||
|
||||
-- Return vector length between points
|
||||
---@param point Point
|
||||
---@return number
|
||||
function Point:distanceTo( point )
|
||||
return math.sqrt(
|
||||
( self.x - point.x ) ^ 2 +
|
||||
( self.y - point.y ) ^ 2
|
||||
)
|
||||
end
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue