From 23bdc3e89937ac74466ef4e63c77dd845305e1fb Mon Sep 17 00:00:00 2001
From: liqikun <liqikun@3dnest.cn>
Date: Tue, 28 May 2024 11:06:02 +0800
Subject: [PATCH] =?UTF-8?q?feat:=20=E7=BB=98=E5=88=B6=E5=A4=9A=E8=BE=B9?=
 =?UTF-8?q?=E5=BD=A2=E7=BB=88=E7=82=B9=E5=90=B8=E9=99=84=E6=95=88=E6=9E=9C?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 src/polygon/polygon.ts | 41 +++++++++++++++++++++++++++++++++++++++--
 1 file changed, 39 insertions(+), 2 deletions(-)

diff --git a/src/polygon/polygon.ts b/src/polygon/polygon.ts
index 2726caa..d069ec0 100644
--- a/src/polygon/polygon.ts
+++ b/src/polygon/polygon.ts
@@ -22,17 +22,54 @@ export default class Polygon extends Base {
    * Add points only on click events
    */
   addPoint(cartesian: Cartesian3) {
-    this.points.push(cartesian);
+    let endPoint = cartesian;
+    if (this.points.length > 2) {
+      endPoint = this.getEndPoint(cartesian);
+    }
+    this.points.push(endPoint);
     if (this.points.length === 1) {
       this.onMouseMove();
     }
+    if (this.points.length > 2 && this.comparePositions(this.points[0], endPoint)) {
+      this.finishDrawing();
+    }
+  }
+
+  /**
+   * Compare whether the positions of two points are equal.
+   */
+  comparePositions(point1: Cartesian3, point2: Cartesian3) {
+    const lnglat1 = this.cartesianToLnglat(point1);
+    const lnglat2 = this.cartesianToLnglat(point2);
+    return lnglat1[0] === lnglat2[0] && lnglat1[1] === lnglat2[1];
+  }
+
+  /**
+   * Calculate the distance between two points
+   */
+  calculateDistance(point1: Cartesian3, point2: Cartesian3) {
+    return this.cesium.Cartesian3.distance(point1, point2);
+  }
+
+  /**
+   * get end point
+   */
+  getEndPoint(cartesian: Cartesian3) {
+    let endPoint = cartesian;
+    let distance = this.calculateDistance(this.points[0], cartesian);
+    if (distance < 100) endPoint = this.points[0];
+    return endPoint;
   }
 
   /**
    * Draw a shape based on mouse movement points during the initial drawing.
    */
   updateMovingPoint(cartesian: Cartesian3) {
-    const tempPoints = [...this.points, cartesian];
+    let endPoint = cartesian;
+    if (this.points.length > 2) {
+      endPoint = this.getEndPoint(cartesian);
+    }
+    const tempPoints = [...this.points, endPoint];
     this.setGeometryPoints(tempPoints);
     if (tempPoints.length === 2) {
       this.addTempLine();