polygon.ts 706 B

1234567891011121314151617181920212223
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. /**
  7. * Create points for a polygon:
  8. * 3 for a triangle, 4 for a rectangle, 5 for a pentagon, 6 for a hexagon...
  9. */
  10. export function polygon(sideCount: number, shift: boolean) {
  11. const points = new Float32Array(sideCount * 2)
  12. const radius = sideCount <= 4 ? Math.sqrt(2) / 2 : 0.6
  13. const offset = shift ? 1 : 0
  14. for (let i = 0, il = 2 * sideCount; i < il; i += 2) {
  15. const c = (i + offset) / sideCount * Math.PI
  16. points[i] = Math.cos(c) * radius
  17. points[i + 1] = Math.sin(c) * radius
  18. }
  19. return points
  20. }