<p>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.</p>

<p>For this you require the following information</p>

<p>Ray's in the form <span class="math-container">$R =e+td $</span> where <span class="math-container">$e$</span> is the origin or eye point and <span class="math-container">$d$</span> is the direction vector.</p>

<p>Secondly you need to define your square by using the following information  </p>

<ol>
<li><strong>Poisition</strong> - 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.</li>
<li><strong>Normal</strong> - Self explanatory.</li>
<li><strong>Width</strong> and <strong>Height</strong> not as scalars but as vectors. These can be called the two edge vectors. I will denote them as <span class="math-container">$e1$</span> and <span class="math-container">$e2$</span>. <em>Note that both edge vectors need to grow outward from the corner of the square chosen in point 1.</em></li>
</ol>

<p>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>

<p><span class="math-container">$(p-p1).n= 0$</span></p>

<p>If we suppose <span class="math-container">$p$</span> is our ray here and <span class="math-container">$p1 = position$</span>, then we will have,</p>

<p><span class="math-container">$(e+td - p1).n=0$</span><br>
<span class="math-container">$(e+td - p1).n=0$</span><br>
<span class="math-container">$e.n + td.n - p1.n = 0$</span><br>
<span class="math-container">$t = p1.n - e.n/d.n$</span><br>
<span class="math-container">$t = (p1-e).n/d.n$</span></p>

<p>If <span class="math-container">$t \geq 0$</span> 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.</p>

<p><span class="math-container">$intersect\_point = e + td$</span><br>
<span class="math-container">$v = intersect\_point - p1$</span></p>

<p><span class="math-container">$width = ||e1||$</span><br>
<span class="math-container">$height = ||e2||$</span></p>

<p><span class="math-container">$proj1 = v.e1 /width $</span><br>
<span class="math-container">$proj2 = v.e2 / height$</span>  </p>

<p><span class="math-container">$if((proj1 &lt; width \;\&amp;\&amp;\; proj1 &gt; 0) \;\&amp;\&amp;\; (proj2  &lt; height \;\&amp;\&amp;\; proj2 &gt; 0) )$</span><br>
<span class="math-container">$return \, true$</span></p>

<p>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 <span class="math-container">$A$</span> then the edge vectors are <span class="math-container">$\vec{AB}$</span> and <span class="math-container">$\vec{AD}$</span>. </p>

CC BY-SA 4.0
2018-12-23T19:16:39.013Z
the p1 is position to ray?, also this should resolve with crossProduct ?
information
CC BY-SA 4.0
2018-12-23T19:19:01.120Z
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
CC BY-SA 4.0
2018-12-23T20:13:29.407Z
how i get the normal (n)  to this t=(p1−e).n/d.n
information
CC BY-SA 4.0
2018-12-24T01:22:13.257Z
I'm new, I do not know much about graphics, can you explain how I get the normal please?
information
CC BY-SA 4.0
2018-12-24T08:41:41.000Z
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
CC BY-SA 4.0
2018-12-24T14:30:55.497Z
i edited my question, 
I can not graph the square, I can only graph spheres
information
CC BY-SA 4.0
2018-12-24T14:38:03.047Z
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
CC BY-SA 4.0
2018-12-25T16:30:01.040Z
thanks for the recommendation, I will leave my project to understand more about graficacion, 
could you recommend more books please, I am very interested in this topic.
information
CC BY-SA 4.0
2018-12-25T16:50:18.870Z
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
CC BY-SA 4.0
2018-12-23T18:05:51.967Z
2019-02-14T06:52:00.253Z
2019-02-14T06:52:00.253Z
gallickgunner
<p>I have a class Ray and Square</p>

<p>public class Rayo {</p>

<pre><code>public Point origen;
public Vector direction;

Square{
X,Y,Widht, height;
}
</code></pre>

<p>I need intersection between this two, how? my code crossProduct in java is </p>

<pre><code>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);
    }
</code></pre>

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

<raytracing>
Get intersection Ray with square
information
# Get intersection Ray with square

(Asked by: username_0 on 2018-12-23)

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?

**Tags:** raytracing


---

## 1 answers

### Answer by username_1 on 2018-12-23. Score: 2 | Accepted answer

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 username_0

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 username_0

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 username_0 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}$.

> **Comments:**
> **username_0**: the p1 is position to ray?, also this should resolve with crossProduct ?  
> **username_1**: p is the ray direction and p1 is the vector describing the position to the square. No need for cross product in this method  
> **username_0**: how i get the normal (n)  to this t=(p1−e).n/d.n  
> **username_0**: I'm new, I do not know much about graphics, can you explain how I get the normal please?  
> **username_1**: The normal is for the square. you need to define it when initializing the square. It's a necessary username_0 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.  
> **username_0**: i edited my question,  I can not graph the square, I can only graph spheres  
> **username_1**: 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".  
> **username_0**: thanks for the recommendation, I will leave my project to understand more about graficacion,  could you recommend more books please, I am very interested in this topic.  
> **username_1**: 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"  

---

