circumference-drawer.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * iGeom by LInE
  3. * Geometric Object: Circumference
  4. * Drawer to Segment
  5. * www.matematica.br/igeom
  6. *
  7. * ./app/components/circumference-component/drawers/circumference-drawer.js
  8. *
  9. * @version 2023/08/20: fixed debug message that was avoiding to create Circumference(B,A) whanever exists Circumference(A,B)
  10. * @version 2023/07/30: indentation and 'getRadius()' changed to better name 'getRadiusValue()'
  11. *
  12. */
  13. import { CircumferenceModel } from "../models/circumference-model";
  14. import { PointDrawer } from "../../point-component/drawers/point-drawer";
  15. import { DrawerAggregator } from "../../../core/drawers/drawer-aggregator";
  16. import { ELEMENTS_CLASS } from "../../../core/enums/elements-class-enum";
  17. import { selector as Selector } from "../../../core/application/selector";
  18. import { SelectableDrawer } from "../../../core/drawers/selectable-drawer";
  19. import { objects as Objects } from "../../../core/application/objects";
  20. import { label as Label } from "../../../component-registry/label";
  21. export class CircumferenceDrawer extends SelectableDrawer {
  22. constructor () {
  23. super();
  24. this.states = ["center", "radius"];
  25. this.state = undefined;
  26. this.circumference = undefined;
  27. this.pointDrawer = new PointDrawer();
  28. this.center;
  29. this.radius;
  30. this.centerAggregator;
  31. this.konvaObject;
  32. this.setElementClass(ELEMENTS_CLASS.CIRCUMFERENCE);
  33. }
  34. // @calledby app/core/models/components/component.js: draw(e): this.drawer.draw(e);
  35. draw (e) {
  36. var auxE = "";
  37. console.log("circumference-drawer.js!draw(e): inicio"); //D
  38. try {
  39. if (e != undefined) {
  40. if (e.target != undefined && e.target.attrs.class != undefined &&
  41. (e.target.attrs.class == ELEMENTS_CLASS.POINT || e.target.attrs.class == ELEMENTS_CLASS.INTERSECTION_POINT) ) {
  42. this.drawByStates(e.target);
  43. return;
  44. }
  45. else
  46. if (e.attrs != undefined && e.attrs.genericObject != undefined) {
  47. this.resolveAggregators([e.attrs.genericObject.center, e.attrs.genericObject.radius], undefined, false);
  48. this.createByCircumference(e.attrs.genericObject);
  49. return;
  50. }
  51. }
  52. auxE += "1, "; //D
  53. const points = Selector.getSelectedPoints();
  54. auxE += "2 (#points=" + (points!=undefined && points.length!=undefined ? points.length : "0") + "), "; //D
  55. if (points == undefined || points.length == 0) {
  56. auxE += "3, "; //D
  57. this.drawByStates();
  58. auxE += "4, "; //D
  59. }
  60. else {
  61. auxE += "5, "; //D
  62. this.drawByPoints(points, undefined, e);
  63. auxE += "6, "; //D
  64. }
  65. } catch (error) {
  66. console.log("circumference-drawer.js!draw(e): Error '" + error + "'; e=" + e + ": path: " + auxE);
  67. // var err = new Error(); console.log(err.stack);
  68. console.trace(); //D present the execution stack - whowever do not apper the lines/names from this Konvas source code...
  69. }
  70. }
  71. drawByPoints (points, aggregators, e) {
  72. aggregators = this.resolveAggregators(points, aggregators, true);
  73. this.centerAggregator = aggregators[0];
  74. this.radiusAggregator = aggregators[1];
  75. this.center = points[0];
  76. this.radius = points[1];
  77. this.createAndDraw(this.center, this.radius);
  78. this.reset();
  79. this.setStatus("");
  80. }
  81. drawByStates (konvaObject) {
  82. var auxE = ""; //D
  83. let aggregator = undefined;
  84. try {
  85. if (konvaObject != undefined) {
  86. aggregator = Objects.getByKonvaObject(konvaObject)[0];
  87. }
  88. if (this.state == undefined) {
  89. this.state = this.states[0];
  90. this.setStatus("Select the first point A, to be the center of the circumference"); //TODO Internacionalizar: Selecione o centro da Circunferência"
  91. }
  92. else
  93. if (this.state == this.states[0]) { // first point A from Circunference(A,B)
  94. this.centerAggregator = aggregator != undefined ? aggregator : this.pointDrawer.drawPoint();
  95. this.center = this.centerAggregator.genericObject;
  96. this.state = this.states[1];
  97. this.setStatus("Select the second point B such that ||A-B|| define the circumference radius"); //TODO Internacionalizar: Selecione o centro da Circunferencia"
  98. }
  99. else
  100. if (this.state == this.states[1]) { // second point B from Circunference(A,B)
  101. this.radiusAggregator = aggregator != undefined ? aggregator : this.pointDrawer.drawPoint();
  102. this.radius = this.radiusAggregator.genericObject;
  103. this.createAndDraw(this.center, this.radius);
  104. this.reset();
  105. this.state = this.states[0];
  106. }
  107. auxE += "13, "; //D
  108. } catch (error) {
  109. console.log("circumference-drawer.js!drawByStates(konvaObject): Error '" + error + "', with " + auxE);
  110. }
  111. }
  112. createAndDraw (center, radius) {
  113. var auxE = ""; // To debug...
  114. try {
  115. this.center = center;
  116. this.radius = radius;
  117. this.circumference = new CircumferenceModel(this.center, this.radius);
  118. const label = Label.draw(true);
  119. this.circumference.setLabel(label);
  120. auxE += "4 (this.circumference.id=" + (this.circumference!=undefined ? this.circumference.id : "<>") + "), "; //D
  121. this.createByCircumference(this.circumference);
  122. } catch (error) {
  123. console.log("circumference-drawer.js!createAndDraw(center, radius): Error '" + error + "', with " + auxE);
  124. }
  125. }
  126. createByCircumference (circumference) {
  127. var auxE = ""; // To debug...
  128. var aggregator = "";
  129. try {
  130. this.circumference = circumference;
  131. this.konvaObject = this.drawCircumference(this.circumference);
  132. aggregator = new DrawerAggregator(this, this.circumference, this.konvaObject, ELEMENTS_CLASS.CIRCUMFERENCE);
  133. super.addAggregator(aggregator);
  134. this.centerAggregator.addAggregator(aggregator);
  135. this.radiusAggregator.addAggregator(aggregator);
  136. SelectableDrawer.drawObject(this.konvaObject);
  137. this.konvaObject.zIndex(0);
  138. super.batchDraw();
  139. } catch (error) {
  140. console.log("circumference-drawer.js!createByCircumference(circumference): Error '" + error + "', with " + auxE);
  141. }
  142. return aggregator;
  143. }
  144. drawCircumference (circumference) {
  145. const circle = new Konva.Arc({
  146. x: circumference.center.posX,
  147. y: circumference.center.posY,
  148. innerRadius: circumference.getRadiusValue(),
  149. outerRadius: circumference.getRadiusValue(),
  150. angle: 360,
  151. fill: "grey",
  152. stroke: "grey",
  153. strokeWidth: 2,
  154. strokeScaleEnabled: false,
  155. transformEnabled: false,
  156. draggable: false,
  157. selectable: false,
  158. index: 0,
  159. class: ELEMENTS_CLASS.CIRCUMFERENCE,
  160. style: { stroke: "grey", fill: "grey" }
  161. });
  162. SelectableDrawer.setSelectableIfIntersectionChanged(circle);
  163. return circle;
  164. }
  165. resolveAggregators (points, aggregators, selected) {
  166. if (this.center == undefined) {
  167. this.center = points[0];
  168. }
  169. if (this.radius == undefined) {
  170. this.radius = points[1];
  171. }
  172. if (aggregators == undefined && selected == true)
  173. aggregators = Selector.getSelectedPointsAggregators();
  174. else {
  175. aggregators = [ Objects.getByGenericObject(points[0])[0], Objects.getByGenericObject(points[1])[0] ];
  176. }
  177. this.centerAggregator = aggregators[0];
  178. this.radiusAggregator = aggregators[1];
  179. return aggregators;
  180. }
  181. update (aggregator, e) {
  182. if (!aggregator.visible) return;
  183. const center = aggregator.genericObject.center;
  184. aggregator.konvaObject.innerRadius(aggregator.genericObject.getRadiusValue());
  185. aggregator.konvaObject.outerRadius(aggregator.genericObject.getRadiusValue());
  186. aggregator.konvaObject.x(center.posX);
  187. aggregator.konvaObject.y(center.posY);
  188. super.batchDraw();
  189. }
  190. } // export class CircumferenceDrawer extends SelectableDrawer