Skip to content

Add surface area of cuboid, conical frustum #6442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 12, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 55 additions & 15 deletions maths/area.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
def surface_area_cube(side_length: float) -> float:
"""
Calculate the Surface Area of a Cube.

>>> surface_area_cube(1)
6
>>> surface_area_cube(3)
Expand All @@ -22,12 +21,34 @@ def surface_area_cube(side_length: float) -> float:
return 6 * side_length**2


def surface_area_cuboid(length: float, breadth: float, height: float) -> float:
"""
Calculate the Surface Area of a Cuboid.
>>> surface_area_cuboid(1, 2, 3)
22
>>> surface_area_cuboid(-1, 2, 3)
Traceback (most recent call last):
...
ValueError: surface_area_cuboid() only accepts non-negative values
>>> surface_area_cuboid(1, -2, 3)
Traceback (most recent call last):
...
ValueError: surface_area_cuboid() only accepts non-negative values
>>> surface_area_cuboid(1, 2, -3)
Traceback (most recent call last):
...
ValueError: surface_area_cuboid() only accepts non-negative values
"""
if length < 0 or breadth < 0 or height < 0:
raise ValueError("surface_area_cuboid() only accepts non-negative values")
return 2 * ((length * breadth) + (breadth * height) + (length * height))


def surface_area_sphere(radius: float) -> float:
"""
Calculate the Surface Area of a Sphere.
Wikipedia reference: https://en.wikipedia.org/wiki/Sphere
Formula: 4 * pi * r^2

>>> surface_area_sphere(5)
314.1592653589793
>>> surface_area_sphere(1)
Expand All @@ -46,7 +67,6 @@ def surface_area_hemisphere(radius: float) -> float:
"""
Calculate the Surface Area of a Hemisphere.
Formula: 3 * pi * r^2

>>> surface_area_hemisphere(5)
235.61944901923448
>>> surface_area_hemisphere(1)
Expand All @@ -70,7 +90,6 @@ def surface_area_cone(radius: float, height: float) -> float:
Calculate the Surface Area of a Cone.
Wikipedia reference: https://en.wikipedia.org/wiki/Cone
Formula: pi * r * (r + (h ** 2 + r ** 2) ** 0.5)

>>> surface_area_cone(10, 24)
1130.9733552923256
>>> surface_area_cone(6, 8)
Expand All @@ -93,12 +112,41 @@ def surface_area_cone(radius: float, height: float) -> float:
return pi * radius * (radius + (height**2 + radius**2) ** 0.5)


def surface_area_conical_frustum(
radius_1: float, radius_2: float, height: float
) -> float:
"""
Calculate the Surface Area of a Conical Frustum.
>>> surface_area_conical_frustum(1, 2, 3)
45.511728065337266
>>> surface_area_conical_frustum(4, 5, 6)
300.7913575056268
>>> surface_area_conical_frustum(-1, 2, 3)
Traceback (most recent call last):
...
ValueError: surface_area_conical_frustum() only accepts non-negative values
>>> surface_area_conical_frustum(1, -2, 3)
Traceback (most recent call last):
...
ValueError: surface_area_conical_frustum() only accepts non-negative values
>>> surface_area_conical_frustum(1, 2, -3)
Traceback (most recent call last):
...
ValueError: surface_area_conical_frustum() only accepts non-negative values
"""
if radius_1 < 0 or radius_2 < 0 or height < 0:
raise ValueError(
"surface_area_conical_frustum() only accepts non-negative values"
)
slant_height = (height**2 + (radius_1 - radius_2) ** 2) ** 0.5
return pi * ((slant_height * (radius_1 + radius_2)) + radius_1**2 + radius_2**2)


def surface_area_cylinder(radius: float, height: float) -> float:
"""
Calculate the Surface Area of a Cylinder.
Wikipedia reference: https://en.wikipedia.org/wiki/Cylinder
Formula: 2 * pi * r * (h + r)

>>> surface_area_cylinder(7, 10)
747.6990515543707
>>> surface_area_cylinder(6, 8)
Expand All @@ -124,7 +172,6 @@ def surface_area_cylinder(radius: float, height: float) -> float:
def area_rectangle(length: float, width: float) -> float:
"""
Calculate the area of a rectangle.

>>> area_rectangle(10, 20)
200
>>> area_rectangle(-1, -2)
Expand All @@ -148,7 +195,6 @@ def area_rectangle(length: float, width: float) -> float:
def area_square(side_length: float) -> float:
"""
Calculate the area of a square.

>>> area_square(10)
100
>>> area_square(-1)
Expand All @@ -164,7 +210,6 @@ def area_square(side_length: float) -> float:
def area_triangle(base: float, height: float) -> float:
"""
Calculate the area of a triangle given the base and height.

>>> area_triangle(10, 10)
50.0
>>> area_triangle(-1, -2)
Expand All @@ -188,9 +233,7 @@ def area_triangle(base: float, height: float) -> float:
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)
Expand Down Expand Up @@ -233,7 +276,6 @@ def area_triangle_three_sides(side1: float, side2: float, side3: float) -> float
def area_parallelogram(base: float, height: float) -> float:
"""
Calculate the area of a parallelogram.

>>> area_parallelogram(10, 20)
200
>>> area_parallelogram(-1, -2)
Expand All @@ -257,7 +299,6 @@ def area_parallelogram(base: float, height: float) -> float:
def area_trapezium(base1: float, base2: float, height: float) -> float:
"""
Calculate the area of a trapezium.

>>> area_trapezium(10, 20, 30)
450.0
>>> area_trapezium(-1, -2, -3)
Expand Down Expand Up @@ -297,7 +338,6 @@ def area_trapezium(base1: float, base2: float, height: float) -> float:
def area_circle(radius: float) -> float:
"""
Calculate the area of a circle.

>>> area_circle(20)
1256.6370614359173
>>> area_circle(-1)
Expand All @@ -313,7 +353,6 @@ def area_circle(radius: float) -> float:
def area_ellipse(radius_x: float, radius_y: float) -> float:
"""
Calculate the area of a ellipse.

>>> area_ellipse(10, 10)
314.1592653589793
>>> area_ellipse(10, 20)
Expand All @@ -339,7 +378,6 @@ 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.

>>> area_rhombus(10, 20)
100.0
>>> area_rhombus(-1, -2)
Expand Down Expand Up @@ -376,7 +414,9 @@ def area_rhombus(diagonal_1: float, diagonal_2: float) -> float:
print(f"Circle: {area_circle(20) = }")
print("\nSurface Areas of various geometric shapes: \n")
print(f"Cube: {surface_area_cube(20) = }")
print(f"Cuboid: {surface_area_cuboid(10, 20, 30) = }")
print(f"Sphere: {surface_area_sphere(20) = }")
print(f"Hemisphere: {surface_area_hemisphere(20) = }")
print(f"Cone: {surface_area_cone(10, 20) = }")
print(f"Conical Frustum: {surface_area_conical_frustum(10, 20, 30) = }")
print(f"Cylinder: {surface_area_cylinder(10, 20) = }")