

namespace Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Edge;

public struct EdgeOrigin
{
    public Vector2 Location { get; set; }
    public float Rotation { get; set; }
    public bool Found { get; set; }
    public float endX => Location.X + MathF.Cos(Rotation) * 1000;
    public float endY => Location.Y + MathF.Sin(Rotation) * 1000;
    public float startX => Location.X - MathF.Cos(Rotation) * 1000;
    public float startY => Location.Y - MathF.Sin(Rotation) * 1000;
    public float Slope=>(endY-startY)/(endX-startX);
    public float YIntercept => startY - (Slope * startX);
    public bool Intersection(EdgeOrigin other, out float x, out float y)
    {
        x = 0;
        y = 0;
        // Line 1
        Vector2 line1Start = new Vector2(startX, startY);
        

        // Line 2
        Vector2 line2Start = new Vector2(other.startX, other.startY);
        

        // Calculate the slopes of the lines
        double line1Slope = Slope;
        double line2Slope = other.Slope;

        if (line1Slope == line2Slope) return false;

        // Calculate the y-intercepts of the lines
        double line1Intercept = line1Start.Y - line1Slope * line1Start.X;
        double line2Intercept = line2Start.Y - line2Slope * line2Start.X;

        // Calculate the intersection point
        double intersectionX = (line2Intercept - line1Intercept) / (line1Slope - line2Slope);
        double intersectionY = line1Slope * intersectionX + line1Intercept;

        // Create the intersection point object
        x = (float)intersectionX;
        y = (float)intersectionY;
        return true;
    }

    public override string ToString()
    {
        return $"{Location.ToString()} {Rotation} rad";
    }

 
}