Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. import { Renderer2 } from '@angular/core';
  2. import { vectorPlus, vectorMultiply, isAtLine } from 'src/utils/math';
  3. import { AbsSVGShape } from './svg.abs-shape';
  4.  
  5. export class SVGPolygon extends AbsSVGShape {
  6.  
  7. private circularPoints: number[][];
  8. private actualPointsPosition: number[][] = [];
  9.  
  10. constructor(x: number, y: number, nSides: number, traceType: number, renderer: Renderer2) {
  11. super(x, y, traceType, renderer);
  12. this.hidePerimeter();
  13.  
  14. this.shapeElement = this.renderer.createElement('rect', 'svg');
  15. this.renderer.appendChild(this.element, this.shapeElement);
  16.  
  17. this.setOpacities();
  18. this.setCursor(x, y);
  19.  
  20. this.element = this.renderer.createElement('polygon', 'svg');
  21.  
  22. const angleBetweenCorners = 2 * Math.PI / nSides;
  23. for (let i = 0; i < nSides; i++) {
  24. this.circularPoints.push([Math.cos(i * angleBetweenCorners), -Math.sin(i * angleBetweenCorners)]);
  25. }
  26. }
  27.  
  28. protected isAtBorder(x: number, y: number) {
  29. const additionnalWidth = 10.0;
  30. const width = this.pointSize + additionnalWidth;
  31. const points = [
  32. [this.center[0] - this.size[0], this.center[1] - this.size[1]],
  33. [this.center[0] + this.size[0], this.center[1] - this.size[1]],
  34. [this.center[0] + this.size[0], this.center[1] + this.size[1]],
  35. [this.center[0] - this.size[0], this.center[1] + this.size[1]],
  36. [this.center[0] - this.size[0], this.center[1] - this.size[1]]];
  37.  
  38. for (let i = 0; i < 4; i++) {
  39. if (isAtLine([x, y], points[i], points[i + 1], width)) {
  40. return true;
  41. }
  42. }
  43.  
  44. return false;
  45. }
  46.  
  47. protected isInside(x: number, y: number) {
  48. return (
  49. this.center[0] - this.size[0] <= x &&
  50. this.center[0] + this.size[0] >= x &&
  51. this.center[1] - this.size[1] <= y &&
  52. this.center[1] + this.size[1] >= y);
  53. }
  54.  
  55. release() {
  56. this.hidePerimeter();
  57. }
  58.  
  59. onShift(isShift: boolean) {
  60. // nothing
  61. }
  62.  
  63. isIn(x: number, y: number, r: number): boolean {
  64. return false;
  65. }
  66.  
  67. protected setPositionAttributes(): void {
  68. this.renderer.setAttribute(this.element, 'points', this.pointsAttribute());
  69. }
  70.  
  71. setCursor(x: number, y: number) {
  72. // you habe your this.circularPoints :
  73. // a centered Polygon corner positions of radius 1;
  74. let radius: number;
  75. if (Math.abs(x - this.startingPoint[1]) < y - this.startingPoint[2]) {
  76. radius = Math.abs(x + this.startingPoint[1]) / 2;
  77. } else {
  78. radius = Math.abs(y + this.startingPoint[2]) / 2;
  79. }
  80. for (let i = 0; i < this.circularPoints.length; i++) {
  81. this.actualPointsPosition.push(vectorPlus(vectorMultiply(this.circularPoints[i], radius), this.center));
  82. }
  83. }
  84.  
  85. // [[1, 2], [3, 4]] -> 1,2 3,4
  86. private pointsAttribute(): string {
  87. return this.actualPointsPosition.map((e) => `${e[0]},${e[1]}`).join(' ');
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement