Trigonometry.php 793 B

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. namespace MathPHP;
  3. /**
  4. * Trigonometric Functions
  5. */
  6. class Trigonometry
  7. {
  8. /**
  9. * Produce a given number of points on a unit circle
  10. *
  11. * The first point is repeated at the end as well to provide overlap.
  12. * For example: unitCircle(5) would return the array:
  13. * [[1, 0], [0, 1], [-1, 0], [0, -1], [1, 0]]
  14. *
  15. * @param int $points number of points
  16. *
  17. * @return array<array{float, float}>
  18. */
  19. public static function unitCircle(int $points = 11): array
  20. {
  21. $n = $points - 1;
  22. $unit_circle = [];
  23. for ($i = 0; $i <= $n; $i++) {
  24. $x = \cos(2 * pi() * $i / ($n));
  25. $y = \sin(2 * pi() * $i / ($n));
  26. $unit_circle[] = [$x, $y];
  27. }
  28. return $unit_circle;
  29. }
  30. }