I'm working on writing some utility functions for the Desmos Art Contest.

So far, I have

Rotation:

$

R_{otate}(x,\space y,\space\theta\,\space p_x,\space p_y)=((x-p_x)\cos\theta-(y-p_y)\sin\theta+p_x,\space(y-p_y)\sin\theta+(x-p_y)\cos\theta+p_y)

$

Transformation:

$

T_{ransform}(x,y,i_x,i_y)=({x+i_x},\space{y+i_y})

$

For my reflection, I want it to have a signature similar to

$$

\Large f\normalsize\Bigg(\Big[\begin{smallmatrix}\Large x\\\Large y\end{smallmatrix}\Big]\huge_,\normalsize\bigg[\begin{smallmatrix}\Large l_{x1}&&\Large l_{x2}\\\Large l_{y1}&&\Large l_{y2}\end{smallmatrix}\bigg]\Bigg)\rightarrow\bigg[\begin{smallmatrix}\Large x\vphantom{2_x}\prime\\\Large y\prime\end{smallmatrix}\bigg]

$$

But I cannot use matrices in the Desmos Graphing Calculator. Thus, I plan on just having each element of the matrices just be their own parameter (no, I'm not writing it in $\small\LaTeX$. I am innable). But, all ways to calculate it as far as I've seen use matrix math. How can I implement a reflection without them?

Variables $l_{x1}$, $l_{x2}$, $l_{y1}$, and $l_{y2}$ correspond to points on the line of reflection. As in, they correspond to a line formed by the equation

$y-l_{x1}=\underbrace{\frac{l_{x2}-l_{x1}}{l_{y2}-l_{y1}}}_{\text{slope}\space m}\large(x-l_{y1})$

## Best Answer

I'll use vector algebra, which is not too different from standard algebra. This is as simple and matrix-free as it can get (using the definition $y=mx + a$ is just ugly in my opinion). The derivation also requires basic calculus. Let $L$ be a line, and $\mathbf{q} = (q_1, q_2)$ be a point. The line can be described using the parametric equation $$ L(t) = \mathbf{v}t + \mathbf{a} = (v_1t + a_1, v_2t + a_2), $$ where $\mathbf{v}=(v_1, v_2)$ is a direction vector, and $\mathbf{a}=(a_1, a_2)$ is a point on $L$.

Then the reflection $\mathbf{q}^*$ of $\mathbf{q}$ over $L$ is simply $$ \mathbf{q}^*= 2(\mathbf{v}t^* + \mathbf{a}) - \mathbf{q} = (2(v_1t^* + a_1) - q_1, 2(v_2t^* + a_2) - q_2),\label{eq:1}\tag{1} $$ where $$t^* = \frac{q_1v_1 + q_2v_2 - a_1v_1 - a_2v_2}{v_1^2 + v_2^2}.$$

Now plug $a_1=l_{x1},a_2 = l_{x2},v_1 = l_{x2}-l_{x1}$, $v_2 = l_{y2}-l_{y1}$, and the rest is just algebra. The parametric equation of the line is particularly convenient, because it allows us to define a line from two points on the line.

Derivation: Let $\mathbf{u}=(u_1, u_2)$ be a point on $L$ that minimizes the distance between $\mathbf{q}$ and $L$, i.e. we want to find a $\mathbf{u} = L(t^*)$ that minimizes $$ f(t) = \frac{1}{2}\lVert L(t) - \mathbf{q}\rVert^2 = \frac{1}{2}\left\{(v_1t + a_1 - q_1)^2 + (v_2t + a_2 - q_2)^2\right\}. $$ Then the reflection $\mathbf{q}^*$ is simply $\mathbf{q}^* = \mathbf{q} + 2(\mathbf{u} - \mathbf{q}) = 2\mathbf{u} - \mathbf{q} = (2u_1 - q_1, 2u_2 - q_2)$, see figure below (blue arrow is $2(\mathbf{u} - \mathbf{q})$)$\mathbf{q} across a line $L$" />Now, using calculus, we know that $f'(t^*) = 0$, so $$f'(t^*) = (v_1t^* + a_1 - q_1)v_1 + (v_2t^* + a_2 - q_2)v_2 = 0,$$ or $$ t^* = \frac{(a_1 - q_1)v_1 + (a_2 - q_2)v_2}{v_1^2 + v_2^2}. $$ Finally, $\mathbf{u} = \mathbf{v}t^* + \mathbf{a} = (v_1t^* + a_1, v_2t^* + a_2)$, leading to \ref{eq:1} after putting everything together.

Note that $\mathbf{u} - \mathbf{q}$ is orthogonal to $\mathbf{v}$, i.e. $$ (\mathbf{u} - \mathbf{q})\cdot \mathbf{v} = (u_1 - q_1)v_1 + (u_2 - q_2)v_2 = (v_1t^* + a_1 - q_1)v_1 + (v_2t^* + a_2 - q_2)v_2 = f'(t^*) = 0. $$