Would this be a solution?

I want to know if a slot in a rack is occupied.
I have a 40 slot rack.

This rack has a few slots being used.

If I try and place a device on a slot being used, I want the form NOT t be submitted and sent to a div called collision, but if no collision is found., allow the form to be submitted…

<form...  onSubmit="detectCollision(frontAssets)">
...
</form>
...
// just have the function return true or false
function detectCollision(assets = []) {    
  const selectValue	= document.getElementById('Beginning_ru').value;

  return assets.some((asset) => selectValue > asset['beginning_ru'])
}z

if (detectCollision(frontAssets)) {
  document.getElementById('Collision').scrollIntoView()
}

Well, no.

The starting point of a unit is insufficient information to determine if a collision has occurred. You’d need to know the entire span of the units (IE: You need the endpoints, too).

Consider:

If I have a unit in slots 1-3, and tell it i’m putting a unit at slot 20, is the selectedValue greater than the beginning_ru of the unit? But did I collide?

You could use an array and check for key in the array
Or use a Set and check if the slot index is included in the set
Or an Enum
All have methods to add. delete and check for inclusion without having to fully iterate them.

For a given pair of well defined forward ranges A and B;
if (max(startA,startB) < min(endA,endB)) { There was a collision.

Stated alternatively: if((startA <= endB) && (endA >= startB))

2 Likes

Hres the array

Array
(
    [0] => Array
        (
            [beginning_ru] => 28.0
            [ending_ru] => 31.0
            [beginning_x] => 0.5
            [external_width] => 17.500
            [external_height] => 7.000
            [rack_mounting_direction] => Front
            [operational_status] => 1
            [asset_type] => Periphial_Storage
        )

)

the function would be like

<script>
function detectCollision() {
//convert the PHP array to a javascript one
var assets = <?php echo json_encode($frontAssetsArray); ?>;
//gel both the Beginning_ru & Ending_ru of yellow box
var beginningRU = document.getElementByID('Beginning_ru').value;
var endingRU = beginningRU + ((document.getElementByID('Asset_Height').value / 1.75) - 1);

for(asset in assets) 
//check for collision, this is confusing...
  if((asset['beginning_ru'] <= endB) && (endA >= startB)) {
//show div warning
document.getElementById('collisioi').style.display="block";
return false;
 }
}
}

Thenb id like to call the funtion when the form submnits

onsubmit="return detectCollision()">
``

so there’s your “A”.

compare A to each B in the set. If none of them collide, you can accept A.

1 Like

s this right then…,

for(asset in assets) 
//check for collision, this is confusing...
  if((beginningRU <= asset['beginning_ru']) && (beginningRU >= asset['beginning_ru'])) || 
  if((beginningRU >= asset['ending_ru']) && (beginningRU <= asset['ending_ru'])) ||
  if((endingRU <= asset['beginning_ru']) && (endingRU >= asset['beginning_ru'])) ||  
  if((endingRU >= asset['ending_ru']) && (endingRU <= asset['ending_ru'])) {
//show div warning
document.getElementById('collisiondiv').style.display="block";
return false;
 }
}

I’m… not sure how 1 line became 4…

That doesnt make a lot of sense…

What I said was:

That’s it. The full check.

What you have written is:

if((startA <= startB) && (startA >= startB)) OR
if((startA >= endB) && (startA <= endB)) OR
if((endA <= startB) && (endA >= startB)) OR
if((endA >= endB) && (endA <= endB))

For the moment I will ignore the idea of putting multiple IF statements back to back tacked together with OR’s…

Consider one of your clauses.
(startA <= startB) && (startA >= startB)
Does that make sense? Can startA be both less than and greater than startB at the same time? Can ANY of those clauses be simultaneously true? Only if they were exactly the same.

You’ve invented a collision detector - but it only detects things that meet each other at an edge.

Practical Example

Consider a set of conditions:
A is a span from 1-3.
B is a span from 2-4.

Your logical brain determines they collide. We can draw that out:

1 2 3 4
A A A
B B B

But your code doesnt -
StartA is 1, EndA is 3;
StartB is 2, EndB is 4.

if((startA <= startB) && (startA >= startB)) OR
if((startA >= endB) && (startA <= endB)) OR
if((endA <= startB) && (endA >= startB)) OR
if((endA >= endB) && (endA <= endB))

=>

if((1 <= 2) && (1 >= 2))  //true && false = false.
if((1 >= 4) && (1 <= 4)) //false && true = false.
if((3 <= 2) && (3 >= 2)) //false && true = false.
if((3 >= 4) && (3 <= 4)) //false && true = false.

false OR false OR false OR false is… false. Your code says there is no collision.

Now lets consider what was said in post #5; both versions:

if (max(startA,startB) < min(endA,endB)) //substitute..
if (max(1,2) < min(3,4)) //simplify the functions...
if (2 < 3) //true. There was a collision.

or, the other version:

if((startA <= endB) && (endA >= startB)) //substitute.
if((1 <= 4) && (3 >= 2)) // Resolve inequalities.
if(true && true) // Resolve join.
true //there was a collision.

Proof by exhaustion

So there’s other possible conditions: A is wholly before B, A is wholly after B, or that they border one another.

A before B.

StartA = 1
EndA = 3
StartB = 6
EndB = 8

1 2 3 4 5 6 7 8
A A A
B B B
if((startA <= endB) && (endA >= startB)) //substitute.
if((1 <= 8) && (3 >= 6)) // Resolve inequalities.
if(true && false) // Resolve join.
false //there was not a collision.

A after B.

StartA = 6
EndA = 8
StartB = 1
EndB = 3

1 2 3 4 5 6 7 8
A A A
B B B
if((startA <= endB) && (endA >= startB)) //substitute.
if((6 <= 3) && (8 >= 1)) // Resolve inequalities.
if(false && true) // Resolve join.
false //there was not a collision.

A collides with B at the boundary

StartA = 3
EndA = 5
StartB = 1
EndB = 3

1 2 3 4 5 6 7 8
A A A
B B B
if((startA <= endB) && (endA >= startB)) //substitute.
if((3 <= 3) && (5 >= 1)) // Resolve inequalities.
if(true && true) // Resolve join.
true //there was a collision.

(You can prove the opposite end yourself if you want; suffice it to say, this is where the “=” part becomes important)

3 Likes

Oh sorry, I realized there was one other case, and I have not satisfied my claim of “proof by exhaustion”.

A Wholly Within B

StartA = 3
EndA = 5
StartB = 1
EndB = 7

1 2 3 4 5 6 7
A A A
B B B B B B B
if((startA <= endB) && (endA >= startB)) //substitute.
if((3 <= 7) && (5 >= 1)) // Resolve inequalities.
if(true && true) // Resolve join.
true //there was a collision.

(The same will be true if you swap B and A as to whom is inside whom)

1 Like

Great explanation Hurley, but I looks like you have definitely too much free time :slight_smile:

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.