Skip to content

Commit 4a30f38

Browse files
authored
Merge pull request AllAlgorithms#43 from polettix/patch-triangle-area
Add content for "Area of triangle"
2 parents 992df0c + 76466c3 commit 4a30f38

File tree

1 file changed

+120
-1
lines changed

1 file changed

+120
-1
lines changed

docs/area-of-triangle.md

+120-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,123 @@ title: Area of triangle
44
sidebar_label: Area of triangle
55
---
66

7-
[Open a pull request](https://github.com/AllAlgorithms/algorithms/tree/master/docs/area-of-triangle.md) to add the content for this algorithm.
7+
A *triangle* is a polygon with three sides and three vertices.
8+
Calculating its area efficiently depends on what is known about it.
9+
10+
## From base and height
11+
12+
The basic formula assumes knowledge of the length of one of the sides
13+
(called *base*) and the length of the *height* of the triangle with
14+
respect to that side. This *height* is the segment that originates from
15+
the vertex that is *not* on the side we know about, and intersects the
16+
side we know about at a right angle.
17+
18+
Assuming the base is put horizontally and the third vertex above it, the
19+
following picture results. Quantity *b* is the length of the bottom
20+
side, and *h* the height with respect to it.
21+
22+
<img
23+
src="https://upload.wikimedia.org/wikipedia/commons/thumb/6/63/Triangle.TrigArea.svg/535px-Triangle.TrigArea.svg.png"
24+
alt="Triangle">
25+
26+
Height can sometimes be referred to as *altitude*.
27+
28+
### Formula
29+
30+
In this case, the area is calculated as follows:
31+
32+
<img
33+
src="https://latex.codecogs.com/png.latex?T=\frac{b&space;\cdot&space;h}{2}"
34+
alt="T=\frac{b \cdot h}{2}" title="S=\frac{b \cdot h}{2}" />
35+
36+
37+
### Algorithm
38+
39+
The algorithm can be derived directly from the defining formula above:
40+
41+
```
42+
triangle_area_basic (b, h) {
43+
return b * h / 2;
44+
}
45+
```
46+
47+
## In the euclidean plane
48+
49+
In this section, it is assumed that the triangle is known by the
50+
coordinates of its three vertices A, B, and C on the plane:
51+
52+
<img src="http://latex.codecogs.com/png.latex?\dpi{110}&space;\\\mathbf{A}&space;=&space;(x_A,&space;y_A)&space;\\\mathbf{B}&space;=&space;(x_B,&space;y_B)&space;\\\mathbf{C}&space;=&space;(x_C,&space;y_C)&space;" alt="A, B, and C in the plane" />
53+
54+
### Formula
55+
56+
In this case, the area of the triangle can be calculated as follows:
57+
58+
<img src="http://latex.codecogs.com/png.latex?\dpi{110}&space;T&space;=&space;\frac{|(x_B&space;-&space;x_A)(y_C&space;-&space;y_A)&space;-&space;(x_C&space;-&space;x_A)(y_B&space;-&space;y_A)|}{2}&space;" alt="T = \frac{|(x_B - x_A)(y_C - y_A) - (x_C - x_A)(y_B - y_A)|}{2} " />
59+
60+
### Algorithm
61+
62+
The following algorithm is a direct translation of the formula:
63+
64+
```
65+
triangle_area_plane (x_A, y_A, x_B, y_B, x_C, y_C) {
66+
return abs((x_B - x_A) * (y_C - y_A) - (x_C - x_A) * (y_B - y_A)) / 2;
67+
}
68+
```
69+
70+
## In the euclidean space
71+
72+
In this section, it is assumed that the triangle is known by the
73+
coordinates of its three vertices A, B, and C in the three-dimensional
74+
space:
75+
76+
<img src="http://latex.codecogs.com/png.latex?\dpi{110}&space;\\\mathbf{A}&space;=&space;(x_A,&space;y_A,&space;z_A)&space;\\\mathbf{B}&space;=&space;(x_B,&space;y_B,&space;z_B)&space;\\\mathbf{C}&space;=&space;(x_C,&space;y_C,&space;z_C)&space;" title="http://latex.codecogs.com/png.latex?\dpi{110} \\\mathbf{A} = (x_A, y_A, z_A) \\\mathbf{B} = (x_B, y_B, z_B) \\\mathbf{C} = (x_C, y_C, z_C) " />
77+
78+
### Formula
79+
80+
The formula to calculate the area involves calculating the determinant
81+
of three matrices:
82+
83+
<img src="https://wikimedia.org/api/rest_v1/media/math/render/svg/67c599953dad11fdc117c9ecf5ad8c56de3563e0" alt="triangle area in the plane">
84+
85+
The absolute value of each determinant is the double of the area of the
86+
triangle obtained by projecting the target triangle onto one of the
87+
coordinate planes, which allows reusing the formula for the triangle in
88+
the plane in a previous section.
89+
90+
### Algorithm
91+
92+
The following algorithm leverages the triangle area calculation in the
93+
plane, described in a previous section and not repeated here:
94+
95+
```
96+
/* Assume that A, B, and C are 3-dimensional arrays
97+
X at index 0, Y at index 1, Z at index 2 */
98+
triangle_area_space (A, B, C) {
99+
x = triangle_area_plane(A[1], A[2], B[1], B[2], C[1], C[2]);
100+
y = triangle_area_plane(A[2], A[0], B[2], B[0], C[2], C[0]);
101+
z = triangle_area_plane(A[0], A[1], B[0], B[1], C[0], C[1]);
102+
return sqrt(x * x + y * y + z * z);
103+
}
104+
```
105+
106+
*Note*: the division by two in the formula is already included in the
107+
result from the calculation of the area in the plane.
108+
109+
## Performance
110+
111+
All algorithms for calculating the area of a triangle in the sections
112+
above execute in constant time (O(0)).
113+
114+
## Implementations
115+
116+
| | Language | Link |
117+
|:-: | :-: | :-: |
118+
| | | |
119+
120+
## Helpful Links
121+
122+
- [Triangle][]
123+
- [Area of Triangles and Polygons][]
124+
125+
[Triangle]: https://en.wikipedia.org/wiki/Triangle
126+
[Area of Triangles and Polygons]: http://geomalgorithms.com/a01-_area.html

0 commit comments

Comments
 (0)