dacha
    Preparing search index...

    Class VectorOps

    Utility class providing static methods for vector operations and geometric calculations.

    This class contains mathematical operations for working with 2D vectors, points, and geometric shapes. All methods are static and can be used without instantiating the class.

    // Create a vector from an angle
    const direction = VectorOps.getVectorByAngle(Math.PI / 4); // 45 degrees

    // Calculate dot product
    const dot = VectorOps.dotProduct(point, direction);

    // Project a point onto an edge
    const projected = VectorOps.projectPointToEdge(point, edge);

    // Check if point is inside polygon
    const inside = VectorOps.isPointInPolygon(point, polygonEdges);
    Index
    • Calculates the cross product of two points.

      Parameters

      • point1: Point

        First point to use in the cross product

      • point2: Point

        Second point to use in the cross product

      Returns number

      Scalar value representing the cross product of the two points

      const point1 = { x: 3, y: 4 };
      const point2 = { x: 1, y: 2 };
      const cross = VectorOps.crossProduct(point1, point2);
    • Calculates the dot product of a point and a vector.

      Parameters

      • point: Point

        Point to use in the dot product

      • vector: Point

        Vector to use in the dot product

      Returns number

      Dot product result

      const point = { x: 3, y: 4 };
      const vector = new Vector(1, 2);
      const dot = VectorOps.dotProduct(point, vector);
    • Fixes floating-point calculation errors by converting very small numbers to zero.

      Parameters

      • value: number

        Number to fix

      Returns number

      Fixed number (0 if smaller than epsilon, otherwise unchanged)

    • Returns the closest point on an edge segment to the given point.

      Parameters

      • point: Point

        Point to project. Should have properties x and y.

      • edge: Edge

        Edge to project onto, defined by two endpoints point1 and point2. Each endpoint should have properties x and y.

      Returns Point

      Projected point on the edge, with x and y coordinates.

      const point = { x: 3, y: 4 };
      const edge = { point1: { x: 0, y: 0 }, point2: { x: 10, y: 0 } };
      const projected = VectorOps.getClosestPointOnEdge(point, edge);
    • Calculates the normal vector of a line segment.

      Parameters

      • x1: number

        X coordinate of the first point

      • x2: number

        X coordinate of the second point

      • y1: number

        Y coordinate of the first point

      • y2: number

        Y coordinate of the second point

      Returns Vector

      A normalized Vector representing the line's normal

      // Get normal of a horizontal line
      const normal = VectorOps.getNormal(0, 10, 0, 0);
    • Creates a unit vector from an angle in radians.

      Parameters

      • angle: number

        Angle in radians

      Returns Vector

      A normalized Vector pointing in the direction of the angle

      // Create a vector pointing down
      const angle = VectorOps.getVectorByAngle(Math.PI / 2);
    • Determines if a point is inside a polygon.

      Parameters

      • point: Point

        The point to test. Should have properties x and y.

      • polygon: Edge[]

        An array of edges representing the polygon. Each edge is defined by two endpoints point1 and point2, where each endpoint has properties x and y.

      Returns boolean

      Returns true if the point is inside the polygon, otherwise false.

      The algorithm may be inaccurate in edge cases, such as when the point lies exactly on a polygon corner.

      const point = { x: 5, y: 5 };
      const polygon = [
      { point1: { x: 2, y: 12 }, point2: { x: 12, y: 12 } },
      { point1: { x: 12, y: 12 }, point2: { x: 12, y: 2 } },
      { point1: { x: 12, y: 2 }, point2: { x: 2, y: 2 } },
      { point1: { x: 2, y: 2 }, point2: { x: 2, y: 12 } },
      ];
      const isInside = VectorOps.isPointInPolygon(point, polygon);
    • Rotates a point around the origin by the given angle.

      Parameters

      • point: Point

        Point to rotate

      • angle: number

        Angle in radians

      Returns Point

      Rotated point