user14663792
user14663792

Reputation:

corners of angled rect in 3d

Ive got 2 points in 3d space (with the same y coordinate). Ill call them c and m. I want to find the corner points (marked in the pic as p1-p4) of a square with the width w. The important thing is, that the square is not parallel to the x-axis. If it were, (for p1 as an example) I could just do:

p1.x = m.x + w / 2
p1.y = m.y + w / 2
p1.z = m.z

How would I do the same with a angled square? These are all the given points:

m; c

and lenghts:

w; d

pic1

Upvotes: 0

Views: 242

Answers (1)

scg
scg

Reputation: 491

There's multiple ways to do it, but here's one way.

If the two points are guaranteed to have the same y value, you should be able to do it as follows.

Take 'm - c' and call that u. Normalize u. Then take the cross product of u and the y axis to get v, a vector parallel to the xz plane that's perpendicular to u. (This can be optimized, but that's unlikely to be important.) Then take the cross product of u and v to get a third vector, w. Note that you can use 'm - c' or 'c - m', or use different orders for the cross-product arguments, and it'll still work, but the resulting vectors may point in different directions (but only opposite directions). You can also normalize at different points in the process and get the same results at the end.

Once you have m, v, and w, you can use some basic vector math to compute the corners.

[Edit: I see you have a variable named 'w', so I should clarify that the 'w' in my example is a different 'w' than yours. As for your 'w' and 'd', those would factor in in the vector math I mentioned at the end.]

Upvotes: 1

Related Questions