distances
This commit is contained in:
26
src/main.lua
26
src/main.lua
@@ -1,11 +1,7 @@
|
||||
local screen_width, screen_height = love.graphics.getDimensions()
|
||||
-- print(screen_width, screen_height)
|
||||
local node_radius = 5
|
||||
local safe_zone = 15
|
||||
|
||||
-- programmatically determined later
|
||||
local translation = {0, 0} -- x, z
|
||||
|
||||
|
||||
|
||||
-- TODO load from JSON instead of defining here!
|
||||
@@ -17,8 +13,8 @@ local nodes = {
|
||||
TestB = {150, 0, 200},
|
||||
TestC = {300, 0, -200, true},
|
||||
TestE = {1200, 0, 200, "Displayed Name"},
|
||||
TestFarLeft = {-200, 0, 50},
|
||||
TestFarBottomRight = {1200, 0, 2000, true},
|
||||
TestFarLeft = {-200, 5000, 50},
|
||||
-- TestFarBottomRight = {1200, 0, 2000, true},
|
||||
|
||||
-- for testing automatic scaling/positioning
|
||||
TopLeft = {0, 0, 0},
|
||||
@@ -34,6 +30,7 @@ local edges = {
|
||||
{"TestA", "TestC"},
|
||||
{"TestB", "TestC"},
|
||||
{"TestC", "TestE"},
|
||||
{"TestE", "TestFarLeft"},
|
||||
}
|
||||
|
||||
|
||||
@@ -55,12 +52,10 @@ for _, node in pairs(nodes) do
|
||||
minimums[2] = node[3]
|
||||
end
|
||||
end
|
||||
-- print(minimums[1], minimums[2], maximums[1], maximums[2])
|
||||
|
||||
-- find scale factor
|
||||
-- find scale factor, translate
|
||||
local distance_x = maximums[1] - minimums[1]
|
||||
local distance_z = maximums[2] - minimums[2]
|
||||
-- print(distance_x, distance_z)
|
||||
local scale = math.min((screen_width - safe_zone * 2) / distance_x, (screen_height - safe_zone * 2) / distance_z)
|
||||
local translation = {-minimums[1] + safe_zone / scale, -minimums[2] + safe_zone / scale}
|
||||
|
||||
@@ -72,25 +67,34 @@ function love.draw()
|
||||
love.graphics.translate(unpack(translation))
|
||||
|
||||
-- draw edges below nodes
|
||||
love.graphics.setColor(0.33, 0.33, 0.33, 1)
|
||||
for index, edge in ipairs(edges) do
|
||||
local node_a = nodes[edge[1]]
|
||||
local node_b = nodes[edge[2]]
|
||||
if (not node_a) or (not node_b) then
|
||||
error("Edge " .. index .. " names nonexistent node(s): " .. edge[1] .. "->" .. node_a .. ", " .. edge[2] .. "->" .. node_b)
|
||||
end
|
||||
|
||||
love.graphics.setColor(0.33, 0.33, 0.33, 1)
|
||||
love.graphics.line(node_a[1], node_a[3], node_b[1], node_b[3])
|
||||
|
||||
local center_x = node_a[1] + (node_b[1] - node_a[1]) / 2
|
||||
local center_z = node_a[3] + (node_b[3] - node_a[3]) / 2
|
||||
local distance = math.floor(math.sqrt((node_b[3] - node_a[3])^2 + (node_b[2] - node_a[2])^2 + (node_b[1] - node_a[1])^2))
|
||||
|
||||
love.graphics.setColor(0.67, 0.67, 0.67, 1)
|
||||
love.graphics.print(distance .. "m", center_x, center_z - font:getHeight() / 2, 0, 1 / scale, 1 / scale)
|
||||
end
|
||||
|
||||
-- draw nodes and label them
|
||||
love.graphics.setColor(1, 1, 1, 1)
|
||||
for name, node in pairs(nodes) do
|
||||
love.graphics.circle("fill", node[1], node[3], node_radius / scale)
|
||||
|
||||
if node[4] then
|
||||
if type(node[4]) == "string" then
|
||||
name = node[4]
|
||||
end
|
||||
-- love.graphics.print(name, node[1] + node_radius / scale, node[3] + node_radius / scale, 0, 1 / scale, 1 / scale)
|
||||
|
||||
local text_x, text_y = node[1] + node_radius / scale, node[3] + node_radius / scale
|
||||
|
||||
local difference = node[1] + font:getWidth(name) - maximums[1]
|
||||
|
||||
Reference in New Issue
Block a user