# ETOOBUSY đźš€ minimal blogging for the impatient

# Area of a triangle

**TL;DR**

I was thinking about calculating the area of a triangleâ€¦

â€¦ for reasons that go beyond this post, and I obviously started from
the basic formula we were all teached in school: *base times height
divided by two*. So with reference to the following picture:

we have that segment $\overline{AB}$ is the *base* $b$ and the height
with respect to it is *h*. In this case, then, we have that area $S$ is:

Easy, right? Now letâ€™s assume that you are given a triangle by its coordinates in the cartesian plane $XY$:

In this case, we are given the three points:

\[\mathbf{A} = (A_x, A_y) \\ \mathbf{B} = (B_x, B_y) \\ \mathbf{C} = (C_x, C_y)\]How to calculate the area in this case? Wellâ€¦

# First of all, letâ€™s switch to vectors

Itâ€™s easy to see that vectors $\vec{v}$ and $\vec{w}$
actually are an *arrowized* version of the two sides $\overline{AB}$ and
$\overline{AC}$ respectively. This means that wherever we place these
two vectors applied to a point in the plane, it will be the same
triangle moved around and the area will be the same.

Long story shortâ€¦ we will work with these two vectors:

\[\vec{v} = (v_x, v_y) = (B_x - A_x, B_y - A_y) \\ \vec{w} = (w_x, w_y) = (C_x - A_x, C_y - A_y)\]# How to calculate the area, then?

Letâ€™s just get back to the originsâ€¦

The *base* $b$ is just the length of vector $\vec{v}$, which we will
represent like this:

Now the corresponding height $h$ can be calculated like this:

\[h = |w| \cdot sin\theta = \sqrt{w_x^2 + w_y^2} \cdot sin\theta\]Soâ€¦ the area $S$ will be:

\[S = \frac{b \cdot h}{2} = \frac{|v|\cdot |w| \cdot sin\theta}{2}\]# So whatâ€™s with this angle?!?

It turns out that you can calculate the *scalar product* of the two
vectors in a pretty straightforward way, and it is indeed related to the
*cosine* of the angle $\theta$:

which means:

\[cos\theta = \frac{\vec{v}\cdot\vec{w}}{|v| \cdot |w|}\]Now we just have to remember Pythagoraâ€™s theorem:

\[sin^2\theta + cos^2\theta = 1 \\ sin^2\theta = 1 - cos^2\theta \\ sin^2\theta = 1 - \frac{(\vec{v}\cdot\vec{w})^2}{|v|^2 \cdot |w|^2} \\ sin^2\theta = \frac{|v|^2 \cdot |w|^2 - (\vec{v}\cdot\vec{w})^2}{|v|^2 \cdot |w|^2}\]We will concentrate on the *positive* value for the sine (so that we get
positive areas), so:

Letâ€™s multiply by the denominator on both sidesâ€¦ and divide by two as well:

\[\frac{|v| \cdot |w| \cdot sin\theta}{2} = \frac{\sqrt{|v|^2 \cdot |w|^2 - (\vec{v}\cdot\vec{w})^2}}{2}\]Now wait! The left hand side is our area $S$, soâ€¦

\[S = \frac{\sqrt{|v|^2 \cdot |w|^2 - (\vec{v}\cdot\vec{w})^2}}{2} \\ S = \frac{\sqrt{(v_x^2 + v_y^2) \cdot (w_x^2 + w_y^2) - (v_x \cdot w_x + v_y \cdot w_y)^2}}{2}\]# Did you notice?

The scalar(dot) product of a generic vector $\vec{v}$ with itself ends up being the square of its length:

\[\vec{v}\cdot\vec{v} = |v|^2\]so our area can also be expressed as:

\[S = \frac{ \sqrt{ (\vec{v}\cdot\vec{v})\cdot(\vec{w}\cdot\vec{w}) - (\vec{v}\cdot\vec{w})\cdot(\vec{v}\cdot\vec{w}) } }{2}\]Nifty, uh?!?

# Itâ€™s easier with some code

After this long ride, letâ€™s just take a look at some code. We assume
that our points are represented by arrays (references) with $X$
coordinate at index `0`

and $Y$ coordinate at index `1`

;

```
sub triangle_area {
my ($A, $B, $C) = @_;
my ($v_x, $v_y) = map {$B->[$_] - $A->[$_]} 0 .. 1;
my ($w_x, $w_y) = map {$C->[$_] - $A->[$_]} 0 .. 1;
my $vv = $v_x * $v_x + $v_y * $v_y;
my $ww = $w_x * $w_x + $w_y * $w_y;
my $vw = $v_x * $w_x + $v_y * $w_y;
return sqrt($vv * $ww - $vw * $vw) / 2;
}
```

And I guess weâ€™re done for today!