mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
Add function to calculate area of triangle using Heron's formula (#4065)
* Update area.py Modified area of triangle function. Added a new algorithm to calculate area when 3 sides are known * Add files via upload * Update area.py * Update area.py * Update area.py * Update area.py * Remove unnecessary whitespace Co-authored-by: Dhruv Manilawala <dhruvmanila@gmail.com>
This commit is contained in:
parent
80f5213df5
commit
dd4b265680
|
@ -1,7 +1,7 @@
|
|||
"""
|
||||
Find the area of various geometric shapes
|
||||
"""
|
||||
from math import pi
|
||||
from math import pi, sqrt
|
||||
|
||||
|
||||
def surface_area_cube(side_length: float) -> float:
|
||||
|
@ -26,7 +26,7 @@ def surface_area_sphere(radius: float) -> float:
|
|||
"""
|
||||
Calculate the Surface Area of a Sphere.
|
||||
Wikipedia reference: https://en.wikipedia.org/wiki/Sphere
|
||||
:return 4 * pi * r^2
|
||||
Formula: 4 * pi * r^2
|
||||
|
||||
>>> surface_area_sphere(5)
|
||||
314.1592653589793
|
||||
|
@ -44,7 +44,7 @@ def surface_area_sphere(radius: float) -> float:
|
|||
|
||||
def area_rectangle(length: float, width: float) -> float:
|
||||
"""
|
||||
Calculate the area of a rectangle
|
||||
Calculate the area of a rectangle.
|
||||
|
||||
>>> area_rectangle(10, 20)
|
||||
200
|
||||
|
@ -68,7 +68,7 @@ def area_rectangle(length: float, width: float) -> float:
|
|||
|
||||
def area_square(side_length: float) -> float:
|
||||
"""
|
||||
Calculate the area of a square
|
||||
Calculate the area of a square.
|
||||
|
||||
>>> area_square(10)
|
||||
100
|
||||
|
@ -84,7 +84,7 @@ def area_square(side_length: float) -> float:
|
|||
|
||||
def area_triangle(base: float, height: float) -> float:
|
||||
"""
|
||||
Calculate the area of a triangle
|
||||
Calculate the area of a triangle given the base and height.
|
||||
|
||||
>>> area_triangle(10, 10)
|
||||
50.0
|
||||
|
@ -106,9 +106,42 @@ def area_triangle(base: float, height: float) -> float:
|
|||
return (base * height) / 2
|
||||
|
||||
|
||||
def area_triangle_three_sides(side1: float, side2: float, side3: float) -> float:
|
||||
"""
|
||||
Calculate area of triangle when the length of 3 sides are known.
|
||||
|
||||
This function uses Heron's formula: https://en.wikipedia.org/wiki/Heron%27s_formula
|
||||
|
||||
>>> area_triangle_three_sides(5, 12, 13)
|
||||
30.0
|
||||
>>> area_triangle_three_sides(10, 11, 12)
|
||||
51.521233486786784
|
||||
>>> area_triangle_three_sides(-1, -2, -1)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: area_triangle_three_sides() only accepts non-negative values
|
||||
>>> area_triangle_three_sides(1, -2, 1)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: area_triangle_three_sides() only accepts non-negative values
|
||||
"""
|
||||
if side1 < 0 or side2 < 0 or side3 < 0:
|
||||
raise ValueError("area_triangle_three_sides() only accepts non-negative values")
|
||||
elif side1 + side2 < side3 or side1 + side3 < side2 or side2 + side3 < side1:
|
||||
raise ValueError("Given three sides do not form a triangle")
|
||||
semi_perimeter = (side1 + side2 + side3) / 2
|
||||
area = sqrt(
|
||||
semi_perimeter
|
||||
* (semi_perimeter - side1)
|
||||
* (semi_perimeter - side2)
|
||||
* (semi_perimeter - side3)
|
||||
)
|
||||
return area
|
||||
|
||||
|
||||
def area_parallelogram(base: float, height: float) -> float:
|
||||
"""
|
||||
Calculate the area of a parallelogram
|
||||
Calculate the area of a parallelogram.
|
||||
|
||||
>>> area_parallelogram(10, 20)
|
||||
200
|
||||
|
@ -132,7 +165,7 @@ def area_parallelogram(base: float, height: float) -> float:
|
|||
|
||||
def area_trapezium(base1: float, base2: float, height: float) -> float:
|
||||
"""
|
||||
Calculate the area of a trapezium
|
||||
Calculate the area of a trapezium.
|
||||
|
||||
>>> area_trapezium(10, 20, 30)
|
||||
450.0
|
||||
|
@ -172,7 +205,7 @@ def area_trapezium(base1: float, base2: float, height: float) -> float:
|
|||
|
||||
def area_circle(radius: float) -> float:
|
||||
"""
|
||||
Calculate the area of a circle
|
||||
Calculate the area of a circle.
|
||||
|
||||
>>> area_circle(20)
|
||||
1256.6370614359173
|
||||
|
@ -188,7 +221,7 @@ def area_circle(radius: float) -> float:
|
|||
|
||||
def area_ellipse(radius_x: float, radius_y: float) -> float:
|
||||
"""
|
||||
Calculate the area of a ellipse
|
||||
Calculate the area of a ellipse.
|
||||
|
||||
>>> area_ellipse(10, 10)
|
||||
314.1592653589793
|
||||
|
@ -214,7 +247,7 @@ def area_ellipse(radius_x: float, radius_y: float) -> float:
|
|||
|
||||
def area_rhombus(diagonal_1: float, diagonal_2: float) -> float:
|
||||
"""
|
||||
Calculate the area of a rhombus
|
||||
Calculate the area of a rhombus.
|
||||
|
||||
>>> area_rhombus(10, 20)
|
||||
100.0
|
||||
|
@ -236,24 +269,20 @@ def area_rhombus(diagonal_1: float, diagonal_2: float) -> float:
|
|||
return 1 / 2 * diagonal_1 * diagonal_2
|
||||
|
||||
|
||||
def main():
|
||||
print("Areas of various geometric shapes: \n")
|
||||
print(f"Rectangle: {area_rectangle(10, 20)}")
|
||||
print(f"Square: {area_square(10)}")
|
||||
print(f"Triangle: {area_triangle(10, 10)}")
|
||||
print(f"Parallelogram: {area_parallelogram(10, 20)}")
|
||||
print(f"Trapezium: {area_trapezium(10, 20, 30)}")
|
||||
print(f"Circle: {area_circle(20)}")
|
||||
print("\nSurface Areas of various geometric shapes: \n")
|
||||
print(f"Cube: {surface_area_cube(20)}")
|
||||
print(f"Sphere: {surface_area_sphere(20)}")
|
||||
print(f"Rhombus: {area_rhombus(10, 20)}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
import doctest
|
||||
|
||||
doctest.testmod(verbose=True) # verbose so we can see methods missing tests
|
||||
|
||||
main()
|
||||
print("[DEMO] Areas of various geometric shapes: \n")
|
||||
print(f"Rectangle: {area_rectangle(10, 20) = }")
|
||||
print(f"Square: {area_square(10) = }")
|
||||
print(f"Triangle: {area_triangle(10, 10) = }")
|
||||
print(f"Triangle: {area_triangle_three_sides(5, 12, 13) = }")
|
||||
print(f"Parallelogram: {area_parallelogram(10, 20) = }")
|
||||
print(f"Trapezium: {area_trapezium(10, 20, 30) = }")
|
||||
print(f"Circle: {area_circle(20) = }")
|
||||
print("\nSurface Areas of various geometric shapes: \n")
|
||||
print(f"Cube: {surface_area_cube(20) = }")
|
||||
print(f"Sphere: {surface_area_sphere(20) = }")
|
||||
print(f"Rhombus: {area_rhombus(10, 20) = }")
|
||||
|
|
Loading…
Reference in New Issue
Block a user