# Calculate intersection points of 2 intersecting shapes

I'm not a mathematician, so please be patient!

I need formulas to calculate the two intersection points of a triangle and a square which are configured as shown in my image below.

Assumptions:

The right-isosceles triangle and the square have fixed sizes. The triangle has both side lengths = $t$. The square has side lengths = $s$.

The triangle is always un-rotated and fixed at the origin $(0,0)$.

The square is rotated around it's center point $(x_3,y_3)$ by $r$ degrees.

The square will only move and rotate very, very slightly from the position below.

The intersection will always involve the triangle's hypotenuse and the same two sides of the square shown below.

I hope to use your solution to calculate $(x_1,y_1)$ and $(x_2,y_2)$ for various positions and rotations of the square.

-
As you already know which lines of the shapes will intersect, this boils down to writing down the equations of these lines, preferrably in normal form $ax+by=c$, and then solving two systems of two linear equations each. At least in the most direct aproach. –  MvG Jan 9 '13 at 8:20

The hypothenuse of the triangle has the equation

$$x + y = t$$

The direction of the square which is “right” in case of $r=0$ has the following normal (i.e. direction orthogonal to the line):

$$\begin{pmatrix} \cos r \\ \sin r \end{pmatrix}$$

As the coefficients in the equation of a line are proportional to the normal, the left side of the square will be an equation of the form

$$\cos r \cdot x + \sin r \cdot y = c$$

for some parameter $c$. You also know that when you start at the center and move a length $s$ in the opposite direction of that normal vector, you end up at the center of the left side. You can plug that into the equation to obtain $c$:

$$c = \cos r \cdot (x_3 - s\cos r) + \sin r \cdot (y_3 -s\sin r) = x_3\cos r + y_3\sin r - s$$

So the equation of the left side of the square is

$$\cos r \cdot x + \sin r \cdot y = x_3\cos r + y_3\sin r - s$$

Take these two equations together to compute $(x_1, y_1)$:

\begin{align*} x_1 + y_1 &= t \\ \cos r \cdot x_1 + \sin r \cdot y_1 &= x_3\cos r + y_3\sin r - s \end{align*}

Repeat the above computations with the “up” direction instead of the “right” direction in order to obtain $(x_2,y_2)$. The “up” vector is

$$\begin{pmatrix} -\sin r \\ \cos r \end{pmatrix}$$

-
Thank you, MvG! This was the help that I needed. –  markE Jan 9 '13 at 14:06