Algorithmically finding the pair of LookVectors in an array of LookVectors that compose the largest angle

I’m not sure what you mean by that. The original problem laid out that the angle will never be more than 180 degrees. This solution assumes that, so we don’t have to deal with finding the furthest apart pairs, but can just take the min and max.

The trick with using the magnitude instead of the angle also still works, but the magnitude is not
v1.X * v2.X + v1.Z * v2.Z
but

local v = v1-v2
local magnitude = math.sqrt(v.X^2 + v.Z^2)

Provided there is no Y component (though it would be easy to account for).

Since we just care about ordering highest to lowest, the heaviest operation in there (the square root) is not needed.

The unit vector of the cross product indeed should be [0,1,0] or [0,-1,0] on a horizontal plane, unless the input vectors point in exactly the same or opposite directions, which would give [0,0,0]. The cross product is defined as

cx = v1.Y*v2.Z − v1.Z*v2.Y
cy = v1.Z*v2.X − v1.X*v2.Z
cz = v1.X*v2.Y - v1.Y*v2.X
local cross = Vector3.new(cx, cy, cz)

But since we only need the Y component, we don’t need to call the whole :Cross() function.

But as mentioned, I probably wouldn’t do that, since the original solution is more general. If later you want to compare vectors in vertical planes, you would have to undo the optimizations and rethink the whole problem. While even with a lot of vectors, the gain would probably be minimal: percentually yes, but in absolute numbers, no one will ever notice the difference. A more important difference that is noticed is the hours of dev time needed to work with the code later when maintenance or updates are required. Though I mean this more as a general rule than specific to this problem.

so if we look at this image


your method will select V2 and V3 as being the highest angle

then if we look at this image


the angles are the same but now it will select V1 and V3

but if all the vectors are within 180 degrees then it should work correctly i must of miss understood cody i did not realise that all vectors where all going to be on one side and not get greater then 180

1 Like

Ah hmm, I see what you mean. The angle between 2 vectors is by definition always equal to or less than 180 degrees. But I thought what the author meant, is that when rotating the plane correctly, the signed angles never exceed the range 0-180. That is, I assumed that the drawing you made should never occur, because the angle labelled 170 degrees would have been considered -190 degrees.

I’m not explaining it very well, but if my assumption was incorrect, we do need some extra steps to account for the circle being round. We could for example for each vector, find the vector closest in angle in our list to it’s inverse. Which amounts to adding 180 degrees to the angle and seeing where it fits in the ordered list of angles, taking the closest vector (either the one before or after it, looping at the edges). Anyway, I don’t think the problem statement is meant to be read that way. I read it as that corners are never at a reflex angle. The approach wouldn’t really make sense to me otherwise for the use case. Let’s ask the author.

i think your correct because if we look at this image

all the vectors are on 1 side and there are none on the bottom side so your method will work perfect for this

but if 1 vector was to go on the bottom side then it would not work but i think cody is not doing that now that i read his post again :slight_smile:

thanks for shearing your method help me think of this problem at a new angle

1 Like

I am supporting doing that but I’m using two different handlers for angles that are above or below 90 degrees. There are three possible scenarios:

1- the angle is 0, 180 or NaN, do nothing, the wall does not receive an extension.
2- the angle is smaller than or equal to 90 so extend the two outer pairs of walls up into each other
3- the angle is greater than 90 so extend the two outer pairs up into the corresponding innermost face of that part and create an end part that connects the two so there isn’t any overlap.

Scenario 1:

Scenario 2:
image

Scenario 3:

So, in a case like this, the largest angle is either or 180 degrees, or it’s NaN (since NaN is neither greater than, equal to, or less than NaN or any number), so it ends up evaluating to the largest angle being NaN since the check attempts to compare two numbers.
image