You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Now this function to process dominant direction from a vector finds the most directionally similar vector between Vector.Left, Vector.Right, Vector.Up and Vector.Down computing the dot product twice(!) for each pair and finding the maximum result to get corresponding array index later. For some reason it also treats -Number.MAX_VALUE as the lowest number ever when in reality it is -Infinity.
Proposal
I propose to determine the dominant direction by comparing the coordinates of the vector with each other to determine a quarter of plane this vector belongs to and along with this the dominant direction. The code below will have one shared ray assigned to the bottom instead of right. But it can be changed adding extra comparison. Among other things, such code is less prone to rounding errors.
Plane partitioning diagram
Javascript code demonstrating the idea:
My original solution
if(y>=x){//left or bottomif(y<=-x)//LEFT or topreturnSide.Left;//if (y === x) return Side.Right;//right or BOTTOMreturnSide.Bottom;}//right or topif(y>=-x)//RIGHT or bottomreturnSide.Right;//left or TOPreturnSide.Up;
More readable solution from `BoundingBox.getSideFromIntersection(intersection: Vector): Side`
Context
Now this function to process dominant direction from a vector finds the most directionally similar vector between
Vector.Left
,Vector.Right
,Vector.Up
andVector.Down
computing the dot product twice(!) for each pair and finding the maximum result to get corresponding array index later. For some reason it also treats-Number.MAX_VALUE
as the lowest number ever when in reality it is-Infinity
.Proposal
I propose to determine the dominant direction by comparing the coordinates of the vector with each other to determine a quarter of plane this vector belongs to and along with this the dominant direction. The code below will have one shared ray assigned to the bottom instead of right. But it can be changed adding extra comparison. Among other things, such code is less prone to rounding errors.
Plane partitioning diagram
Javascript code demonstrating the idea:
My original solution
More readable solution from `BoundingBox.getSideFromIntersection(intersection: Vector): Side`
The text was updated successfully, but these errors were encountered: