Area of a triangle from the determinant

In this section we look at a method of calculating the area of any triangle in the x-y plane from its three vertex coordinates, using the matrix determinant. You might need to review that section before going on. At the end of the page, after some practice problems (with solutions), you'll find a simple Python program to do the calculation. Have fun.


The area of the pink triangle is found by calculating the area of the rectangle, then subtracting the areas of the three right triangles, all using the coordinates of the three vertices. The area of the rectangle is

$$ \begin{align} &(x_2 - x_1)(y_2 - y_3) \\ &\phantom{00} = x_2 y_2 - x_2 y_3 - x_1 y_2 + x_1 y_3 \end{align}$$

Now the area of a right triangle is $A = \frac{1}{2}bh,$ where b and h are the lengths of the two non-hypotenuse sides. So the areas of our three triangles are:

$$ \begin{align} A_A &= \frac{1}{2}(x_2 - x_1)(y_2 - y_1) \\[5pt] &= \frac{1}{2}(x_2 y_2 - x_2 y_1 - x_1 y_2 + x_1 y_1) \\[10pt] A_B &= \frac{1}{2}(x_2 - x_3)(y_2 - y_3) \\[5pt] &= \frac{1}{2}(x_2 y_2 - x_2 y_3 - x_3 y_2 + x_3 y_3) \\[10pt] A_C &= \frac{1}{2}(x_3 - x_1)(y_1 - y_3) \\[5pt] &= \frac{1}{2}(x_3 y_1 - x_3 y_3 - x_1 y_1 + x_1 y_3) \end{align}$$

Now let's add up all of the areas of those triangles in preparation for subtracting them from the rectangle area:

$$ \begin{align} A_{\Delta} &= \frac{1}{2}(x_2 y_2 - x_2 y_1 - x_1 y_2 + x_1 y_1 \\ & + x_2 y_2 - x_2 y_3 - x_3 y_2 + x_3 y_3 \\ & + x_3 y_1 - x_3 y_3 - x_1 y_1 + x_1 y_3) \end{align}$$

$$ \begin{align} = \frac{1}{2} &(2x_2 y_2 - x_2 y_1 - x_1 y_2 - x_2 y_3 \\ & - x_3 y_2 + x_3 y_1 - x_1 y_1 + x_1 y_3 ) \end{align}$$

Now subtract that sum from the area of the rectangle. Let's add and swap signs in the parentheses above to simplify the process just a bit.

$$ \begin{align} A &= x_2 y_2 - x_2 y_3 - x_1 y_2 + x_1 y_3 \\ &+ \frac{1}{2}(-2x_2 y_2 + x_2 y_1 + x_1 y_2 \\ &+ x_2 y_3 + x_3 y_2 - x_3 y_1 + x_1 y_1 \\ &- x_1 y_3 ) \\[10pt] &= \frac{1}{2}(-x_2 y_3 - x_1 y_2 + x_2 y_1 \\ &\phantom{000} - x_3 y_1 + x_1 y_3 + x_3 y_2) \end{align}$$

Now compare this to the determinant we calculate from the matrix contrived by combining the column vectors of x's and y's with the unit column vector (1, 1, 1):

$$ \frac{1}{2} det \; \left( \begin{array}{ccc} x_1 & y_1 & 1 \\ x_2 & y_2 & 1 \\ x_3 & y_3 & 1 \end{array} \right)$$

This determinant gives us

$$ \begin{align} \frac{1}{2}&(x_1 y_2 + x_3 y_1 + x_2 y_3 \\ &- x_3 y_2 - x_1 y_3 - x_2 y_1) \end{align}$$

Now notice that all terms between these two expressions are accounted for, except for the sign. We know that the sign of the determinant changes upon each single swap of a row or column, so we can conclude that the sign depends only on the order in which the vertex points are considered. We can just take the absolute value of the expression to get the area of the triangle. So the area of a triangle, given the coordinates of the three vertex points, is

$$ A_{\Delta} = \frac{1}{2} det \; \left( \begin{array}{ccc} x_1 & y_1 & 1 \\ x_2 & y_2 & 1 \\ x_3 & y_3 & 1 \end{array} \right)$$

This is a quick and handy way to find the area of a triangle when what we have are the vertex coordinates. We do so without having to calculate any side lengths or altitudes. It's also a nice way to code area calculation in a computer program.


Practice problems

Use the determinant method to calculate the area of triangles with these vertex coordinates:

1.

$(0, 0), \; (1, 1), \; (3, 5)$

Solution

$$ A_{\Delta} = \frac{1}{2} det \; \left( \begin{array}{ccc} 0 & 0 & 1 \\ 1 & 1 & 1 \\ 3 & 5 & 1 \end{array} \right)$$

$$ \begin{align} &= \frac{1}{2}|0 + 0 + 5 - (3 + 0 + 0)| \\ &= \frac{1}{2}|5 - 3| = 1 \; unit^2 \end{align}$$

2.

$(2, -1), \; (2, 7), \; (9, 6)$

Solution

$$ A_{\Delta} = \frac{1}{2} det \; \left( \begin{array}{ccc} 2 & -1 & 1 \\ 2 & 7 & 1 \\ 9 & 6 & 1 \end{array} \right)$$

$$ \begin{align} &= \frac{1}{2}|14 - 9 + 12 - (63 + 12 - 2)| \\ &= \frac{1}{2}|17 - 83| = 33 \; units^2 \end{align}$$

3.

$(-2, -3), \; (-4, -5), \; (1, 3)$

Solution

$$ A_{\Delta} = \frac{1}{2} det \; \left( \begin{array}{ccc} -2 & -3 & 1 \\ -4 & -5 & 1 \\ 1 & 3 & 1 \end{array} \right)$$

$$ \begin{align} &= \frac{1}{2}|10 - 3 - 12 - (-5 - 6 + 12)| \\ &= \frac{1}{2}|-5 - 1| = 3 \; units^2 \end{align}$$

4.

$(-2, 3), \; (1, 4), \; (6, 9)$

Solution

$$ A_{\Delta} = \frac{1}{2} det \; \left( \begin{array}{ccc} -2 & 3 & 1 \\ 1 & 4 & 1 \\ 6 & 9 & 1 \end{array} \right)$$

$$ \begin{align} &= \frac{1}{2}|-8 + 16 + 9 - (24 - 18 + 3)| \\ &= \frac{1}{2}|17 - 9| = 4 \; units^2 \end{align}$$


Python code for this algorithm

Here is some sample Python code for this little algorithm. It prompts the user for the three sets of vertex coordinates [as a comma-separated (csv) list: x1, y1, x2, y2, x3, y3], then calculates the area and prints it.


          
    # Get the input as a string of six digits (csv);
    # parse it into individual strings.

    print('\nEnter (x,y) coordinates of the three vertices')
    str = str(input('as x1, y1, x2, y2, x3, y3:  '))
    list = str.split(",")

    #convert each element to an integer

    v = []  # The coordinates
    for i in list:
        v.append(int(i))

    if (len(v) == 6):
        # Calculate the area using the determinant formula
        A = 0.0
        A = abs(0.5*(v[0]*v[3] + v[1]*v[4] + v[2]*v[5] - 
                     v[3]*v[4] - v[0]*v[5] - v[1]*v[2]))
        print('Area = ', A)
    else:
        # If length != 6, quit with a message
        print('\nYou must enter six coordinates.')
          
      

Creative Commons License   optimized for firefox
xaktly.com by Dr. Jeff Cruzan is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. © 2016, Jeff Cruzan. All text and images on this website not specifically attributed to another source were created by me and I reserve all rights as to their use. Any opinions expressed on this website are entirely mine, and do not necessarily reflect the views of any of my employers. Please feel free to send any questions or comments to jeff.cruzan@verizon.net.