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)
| 511 | |
| 512 | |
| 513 | def 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 \ |
| 527 | three 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 \ |
| 532 | three 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 \ |
| 537 | length 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 \ |
| 542 | three 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 \ |
| 547 | equal to three as number of sides" |
| 548 | ) |
| 549 | elif length < 0: |
| 550 | raise ValueError( |
| 551 | "area_reg_polygon() only accepts non-negative values as \ |
| 552 | length of a side" |
| 553 | ) |
| 554 | return (sides * length**2) / (4 * tan(pi / sides)) |
| 555 | |
| 556 | |
| 557 | if __name__ == "__main__": |