Question
build me an algorithm in python to find the surface area of any shape
Answer
You can’t reliably find the surface area of literally any shape unless the shape is described somehow — for example:
- a known formula shape like a cube, sphere, cylinder, cone
- a 2D polygon with coordinates
- a 3D mesh made of triangles
- a mathematical function/surface
A good general Python design is to build an algorithm that chooses the right formula based on the shape type.
Here’s a clean starter version for common 3D shapes:
import math
def surface_area(shape, **kwargs):
shape = shape.lower()
if shape == "cube":
side = kwargs["side"]
return 6 * side ** 2
elif shape == "rectangular_prism":
length = kwargs["length"]
width = kwargs["width"]
height = kwargs["height"]
return 2 * (length * width + length * height + width * height)
elif shape == "sphere":
radius = kwargs["radius"]
return 4 * math.pi * radius ** 2
elif shape == "cylinder":
radius = kwargs["radius"]
height = kwargs["height"]
return 2 * math.pi * radius * (radius + height)
elif shape == "cone":
radius = kwargs["radius"]
height = kwargs["height"]
slant_height = math.sqrt(radius ** 2 + height ** 2)
return math.pi * radius * (radius + slant_height)
elif shape == "triangular_prism":
base = kwargs["base"]
triangle_height = kwargs["triangle_height"]
prism_length = kwargs["length"]
side_a = kwargs["side_a"]
side_b = kwargs["side_b"]
side_c = kwargs["side_c"]
triangle_area = 0.5 * base * triangle_height
perimeter = side_a + side_b + side_c
return 2 * triangle_area + perimeter * prism_length
else:
raise ValueError("Shape not supported yet.")
Example usage:
print(surface_area("cube", side=5))
print(surface_area("sphere", radius=3))
print(surface_area("cylinder", radius=4, height=10))
print(surface_area(
"rectangular_prism",
length=8,
width=4,
height=2
))
Output would be:
150
113.09733552923255
351.85837720205683
112
If you want something closer to “any shape,” the most flexible method is to represent the shape as a 3D mesh made of triangles. Then the total surface area is just the sum of all triangle areas.
Here’s a generic triangle-mesh surface area algorithm:
import math
def distance(p1, p2):
return math.sqrt(
(p2[0] - p1[0]) ** 2 +
(p2[1] - p1[1]) ** 2 +
(p2[2] - p1[2]) ** 2
)
def triangle_area(p1, p2, p3):
a = distance(p1, p2)
b = distance(p2, p3)
c = distance(p3, p1)
s = (a + b + c) / 2
return math.sqrt(s * (s - a) * (s - b) * (s - c))
def mesh_surface_area(triangles):
total_area = 0
for triangle in triangles:
p1, p2, p3 = triangle
total_area += triangle_area(p1, p2, p3)
return total_area
Example:
triangles = [
((0, 0, 0), (1, 0, 0), (0, 1, 0)),
((1, 0, 0), (1, 1, 0), (0, 1, 0))
]
print(mesh_surface_area(triangles))
This calculates the area of two triangles making a square, so the result is:
1.0
So the best answer is:
surface_area(shape, **dimensions)
for regular shapes, and:
mesh_surface_area(triangles)
for irregular 3D shapes.
👉 Are you trying to calculate surface area for basic geometry homework shapes, or for an irregular 3D model/object?
Shared from PensacolaCoPilot
