MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / area_reg_polygon

Function area_reg_polygon

maths/area.py:513–554  ·  view source on GitHub ↗

Calculate the area of a regular polygon. Wikipedia reference: https://en.wikipedia.org/wiki/Polygon#Regular_polygons Formula: (n*s^2*cot(pi/n))/4 >>> area_reg_polygon(3, 10) 43.301270189221945 >>> area_reg_polygon(4, 10) 100.00000000000001 >>> area_reg_poly

(sides: int, length: float)

Source from the content-addressed store, hash-verified

511
512
513def area_reg_polygon(sides: int, length: float) -> float:
514 """
515 Calculate the area of a regular polygon.
516 Wikipedia reference: https://en.wikipedia.org/wiki/Polygon#Regular_polygons
517 Formula: (n*s^2*cot(pi/n))/4
518
519 >>> area_reg_polygon(3, 10)
520 43.301270189221945
521 >>> area_reg_polygon(4, 10)
522 100.00000000000001
523 >>> area_reg_polygon(0, 0)
524 Traceback (most recent call last):
525 ...
526 ValueError: area_reg_polygon() only accepts integers greater than or equal to \
527three as number of sides
528 >>> area_reg_polygon(-1, -2)
529 Traceback (most recent call last):
530 ...
531 ValueError: area_reg_polygon() only accepts integers greater than or equal to \
532three as number of sides
533 >>> area_reg_polygon(5, -2)
534 Traceback (most recent call last):
535 ...
536 ValueError: area_reg_polygon() only accepts non-negative values as \
537length of a side
538 >>> area_reg_polygon(-1, 2)
539 Traceback (most recent call last):
540 ...
541 ValueError: area_reg_polygon() only accepts integers greater than or equal to \
542three as number of sides
543 """
544 if not isinstance(sides, int) or sides < 3:
545 raise ValueError(
546 "area_reg_polygon() only accepts integers greater than or \
547equal to three as number of sides"
548 )
549 elif length < 0:
550 raise ValueError(
551 "area_reg_polygon() only accepts non-negative values as \
552length of a side"
553 )
554 return (sides * length**2) / (4 * tan(pi / sides))
555
556
557if __name__ == "__main__":

Callers 1

area.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected