From 15aef61fd538aebb87383b21cac8b2521931a34f Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sun, 10 Dec 2023 10:31:45 -0800 Subject: [PATCH] greatly simplify overlap detection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Exploits the knowledge that w and h are non-negative. All it took was a question on the LÖVE Discord, which pointed me at the technical term of AABB (axis-aligned bounding box) collision detection. --- 0117-straddles_interval | 3 --- 0118-overlapping_intervals | 6 ------ 0119-overlapping_areas | 4 ++-- 0127-test_straddles_interval | 3 --- 0128-test_overlapping_intervals | 3 --- 5 files changed, 2 insertions(+), 17 deletions(-) delete mode 100644 0117-straddles_interval delete mode 100644 0118-overlapping_intervals delete mode 100644 0127-test_straddles_interval delete mode 100644 0128-test_overlapping_intervals diff --git a/0117-straddles_interval b/0117-straddles_interval deleted file mode 100644 index eb48924..0000000 --- a/0117-straddles_interval +++ /dev/null @@ -1,3 +0,0 @@ -straddles_interval = function(lo,hi, border) - return lo <= border and hi >= border -end \ No newline at end of file diff --git a/0118-overlapping_intervals b/0118-overlapping_intervals deleted file mode 100644 index 303068a..0000000 --- a/0118-overlapping_intervals +++ /dev/null @@ -1,6 +0,0 @@ -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 \ No newline at end of file diff --git a/0119-overlapping_areas b/0119-overlapping_areas index ca64d71..e02242f 100644 --- a/0119-overlapping_areas +++ b/0119-overlapping_areas @@ -1,5 +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 + return a.x < b.x + b.w+20 and a.x + a.w > b.x-20 + and a.y < b.y + b.h+60 and a.y + a.h > b.y-20 end \ No newline at end of file diff --git a/0127-test_straddles_interval b/0127-test_straddles_interval deleted file mode 100644 index e257c58..0000000 --- a/0127-test_straddles_interval +++ /dev/null @@ -1,3 +0,0 @@ -test_straddles_interval = function() - check(straddles_interval(-20, 21, 0), '1') -end \ No newline at end of file diff --git a/0128-test_overlapping_intervals b/0128-test_overlapping_intervals deleted file mode 100644 index 94a655e..0000000 --- a/0128-test_overlapping_intervals +++ /dev/null @@ -1,3 +0,0 @@ -test_overlapping_intervals = function() - check(overlapping_intervals(0, 1, -20, 21), 'within') -end \ No newline at end of file