point lying inside a square There is a square lying on a plane in 3d space. I have:


*

*N: normal of the square

*S: vector normal to N determining the orientation of a pair of the square sides

*C: coordinates of the center of the square

*s: length of the side of the square

*P: a point


How can I determine if the point is inside the square? Let's suppose that we already know that the point lies on the same plane of the square.
 A: Given the $S$ you mentioned in the comments (without which the problem is ill-defined), you can take $S\times N$ to get a vector in the direction of the other pair of sides. Normalize both $S$ and $S\times N$ and express $P-C$ in the basis of those two vectors. If the components are both between $-s/2$ and $s/2$ then the point $P$ is inside the square.
A: Here is one way (not guaranteed to be the simplest, but easy enough for computers) to do this task in the plane.
Let $A$, $B$, $C$ and $D$ be the position vectors of the vertices of the square, written clockwise. Imagine for a moment that $P$ is a point near the center of the square. Connect $P$ to the vertices with line segments.
By taking the cross product of the vectors $(B-P)\times(A-B)$ you get an "upward" normal, reflecting the fact that the triangle $PBA$ (around the edges in that order) is oriented counterclockwise. The same thing can be done with $PCB$, $PDC$ and $PAD$. If you set up all four of these counterclockwise-oriented (upward) triangles, then no matter where your point $P$ appears in the interior, all of the cross products will come out pointing upward.
If someone accidentally gives you the vertices in counterclockwise order, nothing changes except that all the triangles are oriented clockwise (downward).
Now imagine you take $P$ and pull it over one of the sides: what happens to the triangles? If you pull it over a side, then one of the orientations reverses. If you pull it directly over a corner, two of the orientations reverse.
So: there is your test. After you have set up your equations to give you four upward oriented triangles for an interior point, plugging in any exterior point results in a mixture of orientations for the four triangles. If all four are up or all down, then the point is in the interior.
A: Compute $\alpha = \langle N, P-C \rangle$. If $\alpha \neq 0$ then $P$ is not on the plane.
If $\alpha = 0$, compute $l_1 = \langle \frac{S}{\|S\|}, P-C \rangle$. If $|l_1| > \frac{s}{2}$, then $P$ is not in the square.
If $|l_1| \leq \frac{s}{2}$, compute $l_2 = \|P-C-l_1 \frac{S}{\|S\|}\|$. If $|l_2| > \frac{s}{2}$, then $P$ is not in the square, otherwise it is inside the square.
Aside: $l_1 \frac{S}{\|S\|}$ is the component of $P-C$ along a unit vector in the $S$ direction, and $P-C-l_1 \frac{S}{\|S\|}$ is the vector in a direction perpendicular to $S$ that lies in the plane (since $P-C$ and $S$ are perpendicular to $N$).
