|
3 | 3 | Wikipedia reference: https://en.wikipedia.org/wiki/Area |
4 | 4 | """ |
5 | 5 |
|
6 | | -from math import pi, sqrt, tan |
| 6 | +from math import pi, sin, sqrt, tan |
7 | 7 |
|
8 | 8 |
|
9 | 9 | def surface_area_cube(side_length: float) -> float: |
@@ -312,6 +312,50 @@ def area_triangle(base: float, height: float) -> float: |
312 | 312 | return (base * height) / 2 |
313 | 313 |
|
314 | 314 |
|
| 315 | +def area_triangle_two_sides_included_angle( |
| 316 | + side1: float, side2: float, included_angle_degrees: float |
| 317 | +) -> float: |
| 318 | + """ |
| 319 | + Calculate the area of a triangle given the length of any two sides and |
| 320 | + their included angle. |
| 321 | +
|
| 322 | + >>> round(area_triangle_two_sides_included_angle(10, 10, 30), 1) |
| 323 | + 25.0 |
| 324 | +
|
| 325 | + >>> round(area_triangle_two_sides_included_angle(10, 10, -30), 1) |
| 326 | + 25.0 |
| 327 | +
|
| 328 | + >>> round(area_triangle_two_sides_included_angle(10, 10, 330), 1) |
| 329 | + 25.0 |
| 330 | +
|
| 331 | + >>> round(area_triangle_two_sides_included_angle(10, 10, 180), 1) |
| 332 | + 0.0 |
| 333 | +
|
| 334 | + >>> round(area_triangle_two_sides_included_angle(10, 10, 0), 1) |
| 335 | + 0.0 |
| 336 | +
|
| 337 | + >>> round(area_triangle_two_sides_included_angle(0, 10, 120), 1) |
| 338 | + 0.0 |
| 339 | +
|
| 340 | + >>> area_triangle_two_sides_included_angle(1, -1, 1) |
| 341 | + Traceback (most recent call last): |
| 342 | + ... |
| 343 | + ValueError: area_triangle_two_sides_included_angle() only accepts \ |
| 344 | + non-negative lengths |
| 345 | +
|
| 346 | + >>> area_triangle_two_sides_included_angle(-1, 1, 1) |
| 347 | + Traceback (most recent call last): |
| 348 | + ... |
| 349 | + ValueError: area_triangle_two_sides_included_angle() only accepts \ |
| 350 | + non-negative lengths |
| 351 | + """ |
| 352 | + if side1 < 0 or side2 < 0: |
| 353 | + raise ValueError( |
| 354 | + "area_triangle_two_sides_included_angle() only accepts non-negative lengths" |
| 355 | + ) |
| 356 | + return 0.5 * side1 * side2 * abs(sin(included_angle_degrees * pi / 180)) |
| 357 | + |
| 358 | + |
315 | 359 | def area_triangle_three_sides(side1: float, side2: float, side3: float) -> float: |
316 | 360 | """ |
317 | 361 | Calculate area of triangle when the length of 3 sides are known. |
|
0 commit comments