How to center triangle in rectangle?

315 Views Asked by At

I have the green rectangle (size/shape can vary) centered in the chart (black master rectangle).

I want know how to draw two lines (or a triangle, shown in my picture in red) through the green rectangle in such way that the lines are passing exactly through the corners of the green rectangle.

I know x1,y1 x2,y2. I need to know how to compute x?,y?

enter image description here

2

There are 2 best solutions below

1
On BEST ANSWER

As pointed out by other answer, for the upper point, what you need to do is

  • construct a line $\ell_1$ passing through $p_0 = (x_0,y_0)$ and $p_1 = (x_1,y_1)$,
  • construct a line $\ell_2$ passing through $p_m = (x_{max},y_0)$ and $p_2 = (x_2,y_2)$.
  • find the intersection $p = (x,y)$ of lines $\ell_1$ and $\ell_2$.

The answer is a horrible mess: $$\begin{align} x &= \frac{(x_1-x_0)(x_{max} y_2-x_2y_0)-(x_2-x_{max})(x_0y_1-x_1y_0)}{(x_2-x_{max})(y_0-y_1)-(x_1-x_0)(y_0-y_2)}\\ y &= \frac{(x_0y_1-x_1y_0)(y_0-y_2)-(y_0-y_1)(x_{max} y_2-x_2y_0)}{(x_2-x_{max})(y_0-y_1)-(x_1-x_0)(y_0-y_2)} \end{align} $$

If you know cross product and has access to a library which knows how to compute cross product, there is a simpler way to describe and calculate $p$.

First, some background mathematics.

For any point $p = (u,v)$, we can represent it by triplets of the from $(ut, vt, t)$ where $t \ne 0$. For any line $\ell : ax + by + c = 0$, we can represent it by triplets of the form $(as, bs, cs)\in \mathbb{R}^3$ where $s \ne 0$.
These are called homogeneous coordinates for point $p$ and line $\ell$. In particular, we will use $\vec{p} = (u,v,1)$ and $\vec{\ell} = (a,b,c)$ to denote one particular set of homogeneous coordinates for $p$ and $\ell$.

In terms of homogeneous coordinates, some geometric relations among points and lines have nice algebraic correspondences. For example,

  • point $p$ lies on line $\ell$ is equivalent to $\;\vec{p}\cdot\vec{\ell} = 0$.
  • line $\ell$ passes through point $p_1$ and $p_2$ is equivalent to $\;\vec{\ell} \propto \vec{p}_1 \times \vec{p}_2$.
  • lines $\ell_1$ and $\ell_2$ intersect at point $p$ is equivalent to $\;\vec{p} \propto \vec{\ell}_1 \times \vec{\ell}_2$.

Apply this to the problem of determining lines $\ell_1, \ell_2$ and hence point $p$ from points $p_0 = (x_0,y_0), p_m = (x_{max},y_0), p_1 = (x_1,y_1)$, and $p_2 = (x_2,y_2)$, we obtain

  • $\vec{\ell}_1 \propto \vec{p}_0 \times \vec{p}_1$,
  • $\vec{\ell}_2 \propto \vec{p}_m \times \vec{p}_2$,
  • $\vec{p} \propto \vec{\ell}_1 \times \vec{\ell}_2 \propto (\vec{p}_0 \times \vec{p}_1 ) \times ( \vec{p}_m \times \vec{p}_2)$.

This means once we have computed the components of following cross product, $$(u_x, u_y, u_z) \stackrel{def}{=} (\vec{p}_0 \times \vec{p}_1 ) \times ( \vec{p}_m \times \vec{p}_2)$$ the coordinates for the desired intersection $p$ is given by $\displaystyle\;(x,y) = \left(\frac{u_x}{u_z},\frac{u_y}{u_z}\right)$.

5
On

Derive a formula for the two lines that connect the same corner of both boxes, then calculate the intersection of the two lines.