lines-lua/point.lua
2025-06-25 14:01:04 +03:00

42 lines
766 B
Lua

-- Represents 2D point
---@class Point
Point = { x = 1, y = 1 }
-- Point factory
---@param x number
---@param y number
---@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