61

I have coordinates of 3d triangle and I need to calculate its area. I know how to do it in 2D, but don't know how to calculate area in 3d. I have developed data as follows.

(119.91227722167969, 122.7717056274414, 39.3568115234375), 
(119.8951187133789, 122.7717056274414, 39.38057327270508), 
(121.11941528320312, 123.2818832397461, 38.41301345825195)
iamgopal
  • 643

11 Answers11

71

If your 3 points are A, B, C then you may use directly the (half) cross product formula : $$S=\dfrac{|\mathbf{AB}\times\mathbf{AC}|}2=\dfrac{|\mathbf{AB}||\mathbf{AC}||\sin(\theta)|}2 $$ that is (see the Wikipedia link to get the cross-product in $\mathbb{R}^3$) : $$S=\frac 12 \sqrt{(y_{AB}\cdot z_{AC}-z_{AB}\cdot y_{AC})^2+(z_{AB}\cdot x_{AC}-x_{AB}\cdot z_{AC})^2+(x_{AB}\cdot y_{AC}-y_{AB}\cdot x_{AC})^2}$$ if $\mathbf{AB}=(x_{AB},y_{AB},z_{AB})$ and $\mathbf{AC}=(x_{AC},y_{AC},z_{AC})$

Raymond Manzoni
  • 43,021
  • 5
  • 86
  • 140
49

Say you have 3 points $\mathbf{A, B, C}$. Find the angle $\theta$ between $\mathbf{AB}$ and $\mathbf{AC}$ using dot product (i.e. $\mathbf{AB}\cdot\mathbf{AC}=|\mathbf{AB}||\mathbf{AC}|\cos\theta$) and then you can find the area of the triangle using $$ A=\frac{1}{2}|\mathbf{AB}||\mathbf{AC}|\sin\theta $$

Picasso
  • 441
  • 8
    Note that $\sin \theta = \sqrt{1 - (\cos\theta)^2}$ allows you to compute $\sin\theta$ quicker than using $\cos^{-1}$ and $\sin$. – Alice Ryhl Aug 09 '17 at 12:18
  • 8
    Wouldn't it just be easier to make use the matrices/determinant method to find the cross product rather than finding $\theta$ first and then using the cross product formula? – pinklemonade Apr 30 '18 at 14:08
40

Probably one of the best ways (and possibly least computationally intensive ways) to approach this problem is with vectors. In this case we have three points, I will keep them as arbitrary variables for better reproducibility.

$$P_1(a_1,a_2,a_3); \ \ \ P_2(b_1,b_2,b_3); \ \ \ P_3(c_1,c_2,c_3)$$

These three points can be used to create two vectors which share the same initial point. It does not matter in what combination we choose the points, so long as we create two vectors with the same initial point to then calculate their normal (orthogonal) vector using the cross product. Once we have the orthogonal, we can get its magnitude which will equate to 2 times the area of the said triangle. (The magnitude of the orthogonal will give us the area of a parallelogram sharing the same adjacent sides, therefore we will half this area in the end to get the area of the triangle).

To create the vectors from a pair of points, you do the following:

$$ \vec{P_1P_2} = <b_1-a_1,b_2-a_2,b_3-a_3> = <x_1, y_1, z_1>$$ $$ \vec{P_1P_3} = <c_1-a_1,c_2-a_2,c_3-a_3> = <x_2,y_2,z_2>$$

Here is a basic picture of what we will be doing:

We will get the area of this parallelogram and then half it

Once you have both vectors you can calculate their orthogonal vector by taking their cross product, you do it as follows:

$$\vec{u} = \vec{P_1P_2} \times \vec{P_1P_3}$$ $$\vec{u} = \begin{vmatrix} \mathbf i & \mathbf j & \mathbf k \\ x_1 & y_1 & z_1 \\ x_2 & y_2 & z_2 \\ \end{vmatrix}\\ $$

$$\vec{u} = \begin{vmatrix} y_1 & z_1\\ y_2 & z_2 \end{vmatrix}\mathbf i - \begin{vmatrix} x_1 & z_1\\ x_2 & z_2 \end{vmatrix}\mathbf j + \begin{vmatrix} x_1 & y_1\\ x_2 & y_2 \end{vmatrix}\mathbf k $$

This boils down to:

$$\vec{u} = (y_1z_2-y_2z_1)\mathbf i-(x_1z_2-x_2z_1)\mathbf j+(x_1y_2-x_2y_1)\mathbf k\\= \ <y_1z_2-y_2z_1,\ x_1z_2-x_2z_1,\ x_1y_2-x_2y_1> \\= \ <x_3,y_3,z_3>$$

Once you have the orthogonal vector from the cross product, we calculate its magnitude:

$$|\vec{u}| = \sqrt{(x_3)^2+(y_3)^2+(z_3)^2} = \mathbf{Area\ of\ Parallelogram}$$

Finally we are left with the area of a parallelogram composed of two of our triangles. At this point we half the magnitude, and we will have the desired result.

$$\frac{|\vec{u}|}{2} = \mathbf{Area\ of\ Triangle}$$

Greg
  • 561
  • I'm not sure its worth bumping a four year old question with a duplicate answer. – Mark McClure Oct 03 '16 at 12:11
  • 15
    Yes, but none of the other explanations go as in depth as the one I provided. They only demonstrate the method and do not explain the underlying thought process. – Greg Oct 03 '16 at 19:35
  • Hey Greg. FYI: I used YOUR answer because that's the one I was able to follow. So many thanks for your answer! (now on to testing my code, see if it provides believable results). – rew Jan 07 '24 at 20:06
22

Heron's formula is easiest as it "requires no arbitrary choice of side as base or vertex as origin, contrary to other formulas for the area of a triangle:" $$A = \sqrt{s(s-a)(s-b)(s-c)}$$ where $s=p/2$ is half of the perimeter $p=a+b+c$ (called the semiperimeter of the triangle). The triangle side lengths can be obtained via vector norm of relative positions: $$a=\|\vec{r}_1-\vec{r}_2\|$$ $$b=\|\vec{r}_2-\vec{r}_3\|$$ $$c=\|\vec{r}_3-\vec{r}_1\|$$

10

If you have 3 coordinates in 3D $(x_1,y_1,z_1);(x_2,y_2,z_2);(x_3,y_3,z_3)$ make two coterminous vectors like $\vec a=(x_2-x_1)I+(y_2-y_1)j+(z_2-z_1)k$ $\vec b=(x_3-x_1)I+(y_3-y_1)j+(z_3-z_1)k$ now find vector product of $\vec a$ and $\vec b$ and area of triangle formed will be $\frac{1}{2} \| \vec a \times \vec b \|$. You are with your desired answer.

  • awesome it has cleared up my douts. – lalit arora Nov 04 '13 at 08:49
  • NOTE: This is the least-expensive formula to compute, as it does not require square root, nor trigonometric calculation. The main computation work is the cross product, which only uses multiplies and add/subtract. EDIT No I'm wrong, in 3D must take MAGNITUDE of cross-product, which requires a SQUARE-ROOT. So Raymond Manzoni's answer is about the same cost. – ToolmakerSteve Mar 28 '14 at 03:33
  • 3
    $\frac12\vec a\times \vec b$ is a vector, but area isn't. You must write $\frac12|\vec a\times \vec b|$ –  Jan 16 '15 at 16:44
6

If you have the vertices would be $\dfrac{1}{2}$ of the cross product $$A=\dfrac{1}{2}\left|\overrightarrow{AB}\times \overrightarrow{AC}\right|$$

5

You have three points $\mathbf {A,B,C}$ then you can use the dot product so the area of the triangle is: $$ S = \frac{1}{2} \sqrt {|AB|²|AC|²-(\mathbf {AB}\cdot\mathbf {AC})^{2}}$$

Bach
  • 5,730
  • 2
  • 20
  • 41
3

Alternatively, if you want to compute the area in an arbitrary dimension, and have computed dot products between all the points, $a$, $b$ and $c$, you can calculate the area in a way which requires no arbitrary choice of point and only requires one square root, unlike the other formulas:

$$\frac{1}{2}\sqrt{2\left(ab\cdot ac+ab\cdot bc+ac\cdot bc-ac\cdot bb-aa\cdot bc-ab\cdot cc\right)+aa\cdot cc+bb\cdot cc+aa\cdot bb-ab^2-ac^2-bc^2}$$

Where $ab\rightarrow dot\left(a,b\right)$

Compute half the distance from a to b, and multiply it by the distance from c to the closest point to c on the ray from a to b.

3

Use Python:

def heron(a,b,c):  
    s = (a + b + c) / 2   
    area = (s*(s-a) * (s-b)*(s-c)) ** 0.5        
    return area

def distance3d(x1,y1,z1,x2,y2,z2):    
    a=(x1-x2)**2+(y1-y2)**2 + (z1-z2)**2
    d= a ** 0.5  
    return d  

def areatriangle3d(x1,y1,z1,x2,y2,z2,x3,y3,z3):  
    a=distance3d(x1,y1,z1,x2,y2,z2)  
    b=distance3d(x2,y2,z2,x3,y3,z3)  
    c=distance3d(x3,y3,z3,x1,y1,z1)  
    A = heron(a,b,c)  
    print("area of triangle is %r " %A)

Now call the function areatriangle3d()with your inputs.
Alternatively you can add a prompt to enter the values.

For the original problem above, I get an approximate area of $0.0097413991$ which is about $0.01$ square units.

To solve it using the cross product approach you can use this video here.

john
  • 860
  • I don't think specifying a programming language is what the OP was looking for as an answer, though of course any three points will form a triangle (unless they are all in a line, a degenerate case), and the invocation of Heron's rule is in line with other answers. – hardmath Dec 10 '15 at 00:05
  • 4
    If the OP wishes I can delete this. I was just trying to show the OP a way to implement it. – john Dec 10 '15 at 06:36
  • 1
    Next time be sure to format the code as code (highlight text, use the {} button), or it's very hard to read. – Zaz Aug 17 '16 at 16:28
  • @Zaz it's a valid python code. it will work "as it is". I think it's code enough to be code. – Yash Kumar Verma Dec 21 '18 at 19:54
  • @YashKumarVerma: I was talking about a past revision that didn't have indentation or monospace. – Zaz Dec 21 '18 at 20:25
  • Sorry about that, I will add format code . – john Mar 23 '19 at 20:34
2

This is computationally efficient with only one square root calculation required.

The three vertices are: $(x_1,y_1,z_1)$, $(x_2,y_2,z_2)$, $(x_3,y_3,z_3)$

 $Area = \frac 12\sqrt{ ((x_2 \cdot y_1) -(x_3 \cdot y_1) - (x_1 \cdot y_2) + (x_3 \cdot y_2) +( x_1 \cdot y_3) -( x_2 \cdot y_3) ) ^2 + ((x_2 \cdot z_1 )- (x_3 \cdot z_1 )-( x_1 \cdot z_2 ) +( x_3 \cdot z_2 ) +( x_1 \cdot z_3 )-( x_2 \cdot z_3)) ^2 +( (y_2 \cdot z_1 )-( y_3 \cdot z_1 )-( y_1 \cdot z_2 ) +( y_3 \cdot z_2 ) +( y_1 \cdot z_3 )-( y_2 \cdot z_3))^2 }$

0

If a,b,c are the position vectors, use 1/2abs(a×b+b×c+c×a). Take the magnitude of the area vector to get the solution.