1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-15 05:04:50 +00:00
opus/sys/apis/point.lua

208 lines
4.5 KiB
Lua
Raw Normal View History

local Util = require('util')
2016-12-11 19:24:52 +00:00
local Point = { }
function Point.copy(pt)
return { x = pt.x, y = pt.y, z = pt.z }
end
2017-07-24 02:37:07 +00:00
function Point.same(pta, ptb)
return pta.x == ptb.x and
pta.y == ptb.y and
pta.z == ptb.z
end
function Point.above(pt)
return { x = pt.x, y = pt.y + 1, z = pt.z, heading = pt.heading }
end
function Point.below(pt)
return { x = pt.x, y = pt.y - 1, z = pt.z, heading = pt.heading }
end
2016-12-11 19:24:52 +00:00
function Point.subtract(a, b)
a.x = a.x - b.x
a.y = a.y - b.y
a.z = a.z - b.z
end
2017-07-24 02:37:07 +00:00
-- Euclidian distance
2016-12-11 19:24:52 +00:00
function Point.pythagoreanDistance(a, b)
return math.sqrt(
math.pow(a.x - b.x, 2) +
math.pow(a.y - b.y, 2) +
math.pow(a.z - b.z, 2))
end
2017-07-24 02:37:07 +00:00
-- turtle distance (manhattan)
2016-12-11 19:24:52 +00:00
function Point.turtleDistance(a, b)
if a.y and b.y then
2017-10-08 21:45:01 +00:00
return math.abs(a.x - b.x) +
2016-12-11 19:24:52 +00:00
math.abs(a.y - b.y) +
math.abs(a.z - b.z)
else
return math.abs(a.x - b.x) +
math.abs(a.z - b.z)
end
end
function Point.calculateTurns(ih, oh)
if ih == oh then
return 0
end
if (ih % 2) == (oh % 2) then
return 2
end
return 1
end
2017-07-24 02:37:07 +00:00
function Point.calculateHeading(pta, ptb)
local heading
if (pta.heading % 2) == 0 and pta.z ~= ptb.z then
if ptb.z > pta.z then
heading = 1
else
heading = 3
end
elseif (pta.heading % 2) == 1 and pta.x ~= ptb.x then
if ptb.x > pta.x then
heading = 0
else
heading = 2
end
elseif pta.heading == 0 and pta.x > ptb.x then
heading = 2
elseif pta.heading == 2 and pta.x < ptb.x then
heading = 0
elseif pta.heading == 1 and pta.z > ptb.z then
heading = 3
elseif pta.heading == 3 and pta.z < ptb.z then
heading = 1
end
return heading or pta.heading
end
2016-12-11 19:24:52 +00:00
-- Calculate distance to location including turns
-- also returns the resulting heading
function Point.calculateMoves(pta, ptb, distance)
local heading = pta.heading
local moves = distance or Point.turtleDistance(pta, ptb)
if (pta.heading % 2) == 0 and pta.z ~= ptb.z then
moves = moves + 1
if ptb.heading and (ptb.heading % 2 == 1) then
heading = ptb.heading
elseif ptb.z > pta.z then
heading = 1
else
heading = 3
end
elseif (pta.heading % 2) == 1 and pta.x ~= ptb.x then
moves = moves + 1
if ptb.heading and (ptb.heading % 2 == 0) then
heading = ptb.heading
elseif ptb.x > pta.x then
heading = 0
else
heading = 2
end
end
if ptb.heading then
if heading ~= ptb.heading then
moves = moves + Point.calculateTurns(heading, ptb.heading)
heading = ptb.heading
end
end
2017-10-08 21:45:01 +00:00
2016-12-11 19:24:52 +00:00
return moves, heading
end
-- given a set of points, find the one taking the least moves
function Point.closest(reference, pts)
local lpt, lm -- lowest
for _,pt in pairs(pts) do
local m = Point.calculateMoves(reference, pt)
if not lm or m < lm then
lpt = pt
lm = m
end
end
return lpt
end
function Point.eachClosest(spt, ipts, fn)
local pts = Util.shallowCopy(ipts)
while #pts > 0 do
local pt = Point.closest(spt, pts)
local r = fn(pt)
if r then
return r
2017-07-24 02:37:07 +00:00
end
Util.removeByValue(pts, pt)
2017-07-24 02:37:07 +00:00
end
end
2016-12-11 19:24:52 +00:00
function Point.adjacentPoints(pt)
local pts = { }
2017-10-08 21:45:01 +00:00
for _, hi in pairs(_G.turtle.getHeadings()) do
2016-12-11 19:24:52 +00:00
table.insert(pts, { x = pt.x + hi.xd, y = pt.y + hi.yd, z = pt.z + hi.zd })
end
return pts
end
function Point.normalizeBox(box)
return {
x = math.min(box.x, box.ex),
y = math.min(box.y, box.ey),
z = math.min(box.z, box.ez),
ex = math.max(box.x, box.ex),
ey = math.max(box.y, box.ey),
ez = math.max(box.z, box.ez),
}
2016-12-11 19:24:52 +00:00
end
function Point.inBox(pt, box)
return pt.x >= box.x and
pt.y >= box.y and
pt.z >= box.z and
pt.x <= box.ex and
2017-09-15 05:08:04 +00:00
pt.y <= box.ey and
pt.z <= box.ez
2016-12-11 19:24:52 +00:00
end
return Point
--[[
2016-12-11 19:24:52 +00:00
Box = { }
function Box.contain(boundingBox, containedBox)
local shiftX = boundingBox.ax - containedBox.ax
if shiftX > 0 then
containedBox.ax = containedBox.ax + shiftX
containedBox.bx = containedBox.bx + shiftX
end
local shiftZ = boundingBox.az - containedBox.az
if shiftZ > 0 then
containedBox.az = containedBox.az + shiftZ
containedBox.bz = containedBox.bz + shiftZ
end
shiftX = boundingBox.bx - containedBox.bx
if shiftX < 0 then
containedBox.ax = containedBox.ax + shiftX
containedBox.bx = containedBox.bx + shiftX
end
shiftZ = boundingBox.bz - containedBox.bz
if shiftZ < 0 then
containedBox.az = containedBox.az + shiftZ
containedBox.bz = containedBox.bz + shiftZ
end
end
--]]