spawning tries 10 times to avoid overlap

This commit is contained in:
Kartik K. Agaram 2023-11-26 22:55:54 -08:00
parent fe8711224a
commit 6ae88ace54
15 changed files with 74 additions and 7 deletions

View File

@ -1,4 +1,5 @@
on.update = function(dt)
refresh_debug_animations()
if Animating then
animate_next(dt)
return
@ -24,4 +25,4 @@ on.update = function(dt)
A1(Move.node.key)
return
end
end
end

View File

@ -5,7 +5,9 @@ on.keychord_press = function(chord, key)
if Manifest_navigator.reload then
load_manifest()
end
if chord == 'C-=' then
if chord == 'f5' then
Show_debug = not Show_debug
elseif chord == 'C-=' then
-- zoom in
Viewport.zoom = Viewport.zoom+0.1
A()
@ -31,7 +33,7 @@ on.keychord_press = function(chord, key)
elseif Manifest_navigator.show then
keychord_press_on_manifest_navigator(chord, key)
elseif chord == 'C-n' then
new_definition()
animate(new_definition)
A()
elseif chord == 'C-l' then
Manifest_navigator.show = true

View File

@ -33,5 +33,6 @@ on.draw = function()
draw_run_time_error()
draw_test_failure_indicator()
draw_ticks()
if Show_debug then draw_debug() end
draw_menu_bar()
end

View File

@ -1,6 +1,16 @@
spawn_point = function()
return {
x = math.random(Viewport.x, Viewport.x+Viewport.w),
y = math.random(Viewport.y, Viewport.y+Viewport.h),
}
Spawn_candidates = {}
for _=1,10 do
local result = {w=600, h=100}
result.x = math.random(Viewport.x, Viewport.x+Viewport.w)
result.x = round(result.x/100)*100
result.y = math.random(Viewport.y, Viewport.y+Viewport.h)
if _ == 10 or not overlaps_any_definition(result) then
result.w, result.h = nil
result.x = result.x + 100
result.y = result.y + 20
return result
end
table.insert(Spawn_candidates, result)
end
end

3
0117-straddles_interval Normal file
View File

@ -0,0 +1,3 @@
straddles_interval = function(lo,hi, border)
return lo <= border and hi >= border
end

View File

@ -0,0 +1,6 @@
overlapping_intervals = function(alo,ahi, blo,bhi)
return straddles_interval(alo,ahi, blo)
or straddles_interval(alo,ahi, bhi)
or straddles_interval(blo, bhi, alo)
or straddles_interval(blo, bhi, ahi)
end

5
0119-overlapping_areas Normal file
View File

@ -0,0 +1,5 @@
-- return true if a is less than some distance from b
overlapping_areas = function(a, b)
return overlapping_intervals(a.x, a.x+a.w, b.x-20, b.x+b.w+20)
and overlapping_intervals(a.y, a.y+a.h, b.y-20, b.y+b.h+60) -- leave more space below existing definitions
end

View File

@ -0,0 +1,9 @@
overlaps_any_definition = function(box)
for k,def in pairs(Definitions) do
if overlapping_areas(box, def) then
print(def.key)
box.name = def.key
return true
end
end
end

3
0121-draw_debug Normal file
View File

@ -0,0 +1,3 @@
draw_debug = function()
draw_debug_definitions()
end

1
0122-Show_debug Normal file
View File

@ -0,0 +1 @@
Show_debug = false

View File

@ -0,0 +1,12 @@
draw_debug_definitions = function()
for k,def in pairs(Definitions) do
App.color{r=1, g=0, b=0}
-- love.graphics.rectangle('line', vx(def.x-20), vy(def.y-20), scale(620), scale(30))
love.graphics.rectangle('line', vx(def.x-20), vy(def.y-20), scale(def.w+20), scale(def.h+60))
end
for _,cand in ipairs(Spawn_candidates) do
App.color{r=0.4, g=0.8, b=0.4}
love.graphics.rectangle('line', vx(cand.x), vy(cand.y), scale(800), scale(140))
love.graphics.print(cand.name, vx(cand.x), vy(cand.y))
end
end

1
0125-Spawn_candidates Normal file
View File

@ -0,0 +1 @@
Spawn_candidates = {}

View File

@ -0,0 +1,7 @@
test_overlapping_areas = function()
check(
overlapping_areas(
{x=0, y=0, w=1, h=1},
{x=0, y=0, w=1, h=1}),
'identical')
end

View File

@ -0,0 +1,3 @@
test_straddles_interval = function()
check(straddles_interval(-20, 21, 0), '1')
end

View File

@ -0,0 +1,3 @@
test_overlapping_intervals = function()
check(overlapping_intervals(0, 1, -20, 21), 'within')
end