onMouseDown()

in src/engine/tools23d/areatool.js [235:387]


  onMouseDown(x, y, zoom, posX, posY) {
    const MINIMUM_COUNT_OF_VERTEX = 3;
    const MINIMUM_LENGTH_OF_VERTEX = 2;
    const ACCURACY = 2;
    const INDENTATION = 0.05;
    this.m_runningState = true;
    this.m_vertexes.push({ x, y });
    if (this.m_runningState) {
      let i;
      let length;
      length = this.m_vertexes.length;
      if (length > this.last_length + MINIMUM_COUNT_OF_VERTEX) {
        if (this.connect_line === true) {
          x = this.m_vertexes[this.last_length].x;
          y = this.m_vertexes[this.last_length].y;
          let xText = x,
            yText = y;
          if (x >= this.m_vertexes[this.last_length].x) {
            xText += INDENTATION;
          } else {
            xText -= INDENTATION;
          }
          if (y >= this.m_vertexes[this.last_length].y) {
            yText += INDENTATION;
          } else {
            yText -= INDENTATION;
          }
          this.m_runningState = false;
          for (let l = 0; l < MINIMUM_LENGTH_OF_VERTEX; l++) {
            const dist = this.m_distances.pop();
            this.m_scene.remove(dist.line.getRenderObject());
            this.m_scene.remove(dist.text);
          }
          this.m_vertexes.pop();
          length = this.m_vertexes.length;
          this.m_area = this.calculateArea(length) * zoom * zoom;
          this.drawLine(
            this.m_vertexes[length - 1],
            this.m_vertexes[this.last_length],
            `${this.m_area.toFixed(ACCURACY)} mm^2`,
            xText,
            yText
          );
          this.m_runningState = false;
          this.connect_line = false;
          this.last_length = length;
          this.m_vertexes2 = [];
          for (i = 0; i < this.m_distances.length; ++i) {
            const xS = (this.m_distances[i].line.getxS() + (1 - 1 / zoom)) * zoom + posX;
            const yS = (this.m_distances[i].line.getyS() - (1 - 1 / zoom)) * zoom + posY;
            const xE = (this.m_distances[i].line.getxE() + (1 - 1 / zoom)) * zoom + posX;
            const yE = (this.m_distances[i].line.getyE() - (1 - 1 / zoom)) * zoom + posY;
            this.m_vertexes2.push({
              xS,
              yS,
              xE,
              yE,
            });
          }
        } else {
          /**checking for point of intersection between new line and every line in array*/
          for (i = this.last_length; i < length - this.last_length - MINIMUM_LENGTH_OF_VERTEX + this.last_length; i++) {
            /** getting result of intersection*/
            const result = this.lineIntersect(
              this.m_vertexes[i],
              this.m_vertexes[i + 1],
              this.m_vertexes[length - MINIMUM_LENGTH_OF_VERTEX],
              this.m_vertexes[length - 1]
            );
            if (result !== null) {
              if (result.seg1 === true && result.seg2 === true) {
                x = result.x;
                y = result.y;
                /**clearing graphics*/
                for (let j = this.last_length; j < length - 1; j++) {
                  const dist = this.m_distances.pop();
                  this.m_scene.remove(dist.line.getRenderObject());
                  this.m_scene.remove(dist.text);
                }
                /**if intersection detected on line 'i' - removing all points before i*/
                for (let k = this.last_length; k <= i; k++) {
                  this.m_vertexes.shift();
                }
                /**removing last point*/
                this.m_vertexes.pop();
                /**adding point of intersection in the beggining of array*/
                this.m_vertexes.splice(this.last_length, 0, { x, y });
                length = this.m_vertexes.length;
                /**calculating area*/
                this.m_area = this.calculateArea(length) * zoom * zoom;
                /**redraw lines with new coordinates and print area*/
                for (i = this.last_length; i < length - 1; i++) {
                  this.drawLine(
                    this.m_vertexes[i],
                    this.m_vertexes[i + 1],
                    ' ',
                    0.5 * (this.m_vertexes[i + 1].x + this.m_vertexes[i].x) - 0.0,
                    0.5 * (this.m_vertexes[i + 1].y + this.m_vertexes[i].y) - 0.0
                  );
                }
                /** connect first and last point*/
                this.drawLine(
                  this.m_vertexes[length - 1],
                  this.m_vertexes[this.last_length],
                  ' ',
                  0.5 * (this.m_vertexes[length - 1].x + this.m_vertexes[this.last_length].x) - 0.0,
                  0.5 * (this.m_vertexes[length - 1].y + this.m_vertexes[this.last_length].y) - 0.0
                );
                /** drawing size of area on screen*/
                this.drawLine(
                  this.m_vertexes[length - 1],
                  this.m_vertexes[this.last_length],
                  `${this.m_area.toFixed(ACCURACY)} mm^2`,
                  x,
                  y
                );
                /**ending of intersection*/
                this.m_runningState = false;
                this.last_length = length;
                this.m_vertexes2 = [];
                for (i = 0; i < this.m_distances.length; ++i) {
                  const xS = (this.m_distances[i].line.getxS() + (1 - 1 / zoom)) * zoom + posX;
                  const yS = (this.m_distances[i].line.getyS() - (1 - 1 / zoom)) * zoom + posY;
                  const xE = (this.m_distances[i].line.getxE() + (1 - 1 / zoom)) * zoom + posX;
                  const yE = (this.m_distances[i].line.getyE() - (1 - 1 / zoom)) * zoom + posY;
                  this.m_vertexes2.push({
                    xS,
                    yS,
                    xE,
                    yE,
                  });
                }
                /** delete dublicated last line*/
                const PENULTIMATE = 2;
                const dist = this.m_distances[this.m_distances.length - PENULTIMATE];
                this.m_scene.remove(dist.line.getRenderObject());
                this.m_scene.remove(dist.text);
                this.m_distances.splice(this.m_distances.length - PENULTIMATE, 1);
                break;
              }
            }
          }
        }
      }
      /** set point for the next line */
      this.m_xStart = x;
      this.m_yStart = y;
      if (this.m_runningState === true) {
        const line = new Line2D(this.m_scene, this.m_lineWidth, x, y, x, y, this.m_linesMaterial);
        this.m_distances.push({ line });
      }
    }
  }