Untitled

mail@pastecode.io avatar
unknown
javascript
7 months ago
1.1 kB
2
Indexable
Never
function crossProduct(p1: IRSOPoint, p2: IRSOPoint): IRSOPoint {
    return {
      id: '',
      calcId: 0,
      type: 0,
      status: 0,
      x: p1.y * p2.z - p1.z * p2.y,
      y: p1.z * p2.x - p1.x * p2.z,
      z: p1.x * p2.y - p1.y * p2.x
    };
  }

  function triangleArea(p1: IRSOPoint, p2: IRSOPoint, p3: IRSOPoint): number {
    const vector1 = { id: '', calcId: 0, type: 0, status: 0, x: p2.x - p1.x, y: p2.y - p1.y, z: p2.z - p1.z };
    const vector2 = { id: '', calcId: 0, type: 0, status: 0, x: p3.x - p1.x, y: p3.y - p1.y, z: p3.z - p1.z };
    const cross = crossProduct(vector1, vector2);
    return 0.5 * Math.sqrt(cross.x * cross.x + cross.y * cross.y + cross.z * cross.z);
  }

  function testCalculatePolygonArea3D(points: IRSOPoint[], scale: number): number {
    if (points.length < 3) {
      throw new Error("Minimum 3 points are required to form a polygon");
    }

    let area = 0;

    const referencePoint = points[0];
    for (let i = 1; i < points.length - 1; i++) {
      area += triangleArea(referencePoint, points[i], points[i + 1]);
    }

    return area * scale * scale;
  }
Leave a Comment