# Get intersection Ray with square

I have a class Ray and Square

public class Rayo {

public Point origen;
public Vector direction;

Square{
X,Y,Widht, height;
}


I need intersection between this two, how? my code crossProduct in java is

public static Vector productoCruz(Vector a, Vector b) {
return new Vector(a.Y * b.Z - a.Z * b.Y, a.Z * b.X - a.X * b.Z, a.X * b.Y - a.Y * b.X);
}


To apply the product point you need two vectors, and I only have 1 (the ray vector), where do I get the other vector?

The best algorithm depends on the condition like whether or not the square is axis aligned for example. I'm gonna discuss the more general case which can find find intersection for any arbitrary oriented square. The algorithm works by first checking the intersection of ray with the plane containing the square or rectangle etc. Then check if the ray is within bounds of the square.

For this you require the following information

Ray's in the form $$R =e+td$$ where $$e$$ is the origin or eye point and $$d$$ is the direction vector.

Secondly you need to define your square by using the following information

1. Poisition - This might be the vector to the center or any corner of the square. The algorithm I'm describing assumes it is one of the corners.
2. Normal - Self explanatory.
3. Width and Height not as scalars but as vectors. These can be called the two edge vectors. I will denote them as $$e1$$ and $$e2$$. Note that both edge vectors need to grow outward from the corner of the square chosen in point 1.

After you have the following information you first need to compute Ray - plane intersection. The plane being where your square lies. This is done as follows. We know the point normal form of a plane as,

$$(p-p1).n= 0$$

If we suppose $$p$$ is our ray here and $$p1 = position$$, then we will have,

$$(e+td - p1).n=0$$
$$(e+td - p1).n=0$$
$$e.n + td.n - p1.n = 0$$
$$t = p1.n - e.n/d.n$$
$$t = (p1-e).n/d.n$$

If $$t \geq 0$$ then we have an intersection. Now all we need to do is check whether the ray lies inside the square or anywhere else on the plane. This is easy to do.

$$intersect\_point = e + td$$
$$v = intersect\_point - p1$$

$$width = ||e1||$$
$$height = ||e2||$$

$$proj1 = v.e1 /width$$
$$proj2 = v.e2 / height$$

$$if((proj1 < width \;\&\&\; proj1 > 0) \;\&\&\; (proj2 < height \;\&\&\; proj2 > 0) )$$
$$return \, true$$

Note that the above assumes that the edge vectors grow outward from the corner, i.e. A square with the corners A,B,C,D in clockwise fashion starting from the top left. If we choose corner $$A$$ then the edge vectors are $$\vec{AB}$$ and $$\vec{AD}$$.

• the p1 is position to ray?, also this should resolve with crossProduct ? – x-rw Dec 23 '18 at 19:16
• p is the ray direction and p1 is the vector describing the position to the square. No need for cross product in this method – gallickgunner Dec 23 '18 at 19:19
• The normal is for the square. you need to define it when initializing the square. It's a necessary information required in the Square structure. Normal is a vector that is perpendicular to the plane containing the square. If for example your square is located at 1,0,0 looking left, the normal is gonna be -1,0,0. The normal can be caluclated by taking the cross product of the two edge vectors e1 and e2. – gallickgunner Dec 24 '18 at 8:41
• Well for sphere the intersection procedure is different altogether. These are basic intersection procedures given in almost any good book for graphics and games. I'd recommend you to read "Fundamentals of Computer Graphics by Peter Shirley" or "Essential maths for games by Van Verth". – gallickgunner Dec 24 '18 at 14:38
• That's a great step to be honest. Most of peter shirley books include raytracing stuff, I have personally read the above one so i can tell it's a good book. These two are more than enough for now. For more advanced stuff there is "Physically Based Rendering" – gallickgunner Dec 25 '18 at 16:50