Skip to content

[API Proposal]: Vector2.PerpDot #80739

@PavielKraskouski

Description

@PavielKraskouski

Background and motivation

The Perp Dot Product can be used to check the winding order of triangle vertices, check if a point is inside a triangle, calculate barycentric coordinates of a point inside a triangle, triangulate polygons by ear clipping method, etc.

API Proposal

namespace System.Numerics;

public struct Vector2
{
    public static float PerpDot(Vector2 value1, Vector2 value2)
    {
        return value1.X * value2.Y - value1.Y * value2.X;
    }
}

Alternative design:

namespace System.Numerics;

public struct Vector2
{
    public static float PerpDot(Vector2 value1, Vector2 value2)
    {
        return Dot(new Vector2(-value1.Y, value1.X), value2);
    }
}

API Usage

using System.Numerics;

public bool ClockwiseTriangle(Vector2 a, Vector2 b, Vector2 c)
{
    return Vector2.PerpDot(b - a, c - a) < 0;
}

public bool PointInTriangle(Vector2 p, Vector2 a, Vector2 b, Vector2 c)
{
    float u = Vector2.PerpDot(b - p, c - p);
    float v = Vector2.PerpDot(c - p, a - p);
    float w = Vector2.PerpDot(a - p, b - p);
    return (u >= 0 && v >= 0 && w >= 0) ||
           (u <= 0 && v <= 0 && w <= 0);
}

public (float, float, float) Barycentric(Vector2 p, Vector2 a, Vector2 b, Vector2 c)
{
    float denom = 1f / PerpDot(b - a, c - a);
    float u = Vector2.PerpDot(b - p, c - p) * denom;
    float v = Vector2.PerpDot(c - p, a - p) * denom;
    return (u, v, 1 - u - v);
}

Alternative Designs

No response

Risks

No response

Metadata

Metadata

Assignees

Labels

api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.Numericsneeds-further-triageIssue has been initially triaged, but needs deeper consideration or reconsideration

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions