polygon.ts 798 B

1234567891011121314151617181920212223242526
  1. /**
  2. * Copyright (c) 2018-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. /**
  7. * Create 3d 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, radius = -1) {
  11. const points = new Float32Array(sideCount * 3);
  12. const r = radius === -1
  13. ? (sideCount <= 4 ? Math.sqrt(2) / 2 : 0.6)
  14. : radius;
  15. const offset = shift ? 1 : 0;
  16. for (let i = 0, il = sideCount; i < il; ++i) {
  17. const c = (i * 2 + offset) / sideCount * Math.PI;
  18. points[i * 3] = Math.cos(c) * r;
  19. points[i * 3 + 1] = Math.sin(c) * r;
  20. points[i * 3 + 2] = 0;
  21. }
  22. return points;
  23. }