Skip to content

Heron's Formula

  • Project
  • 1 hint
  • 2 of 6 in this chapter

Back to geometry

Heron’s formula is a formula that is used to find the area of any triangle. All you need is the lengths of all 3 sides.

Text
area = √s * (s-a) * (s-b) * (s-c)

s = (a+b+c)/2
a r e a = s ∗ ( s − a ) ∗ ( s − b ) ∗ ( s − c ) area = \sqrt s * (s-a) * (s-b) * (s-c) s = a + b + c 2 s = \frac{a+b+c}{2}

Challenge

Using this formula, write a function that solves for the area of a triangle, given 3 side lengths.

  • Triangle 1 with sides 10, 12, and 15.
  • Triangle 2 with sides 12, 14, and 7.

You can set the sides to the Double data type.

Hints

Hint 1T

t

Solution

Swift
func TrigArea(a: Double, b: Double, c: Double) -> Double {
 let s: Double = (a+b+c)/2
 let x: Double = s*(s-a)*(s-b)*(s-c)
 return x.squareRoot()
}
print("Area of Triangle 1: \(TrigArea(a: 10, b: 12, c: 15))")
print("Area of Triangle 2: \(TrigArea(a: 12, b: 14, c: 7))")

Give the project a try first.