Skip to content

If denominators in LINE/LINE are 0 then you divide by zero. #37

Description

@Czapa10

You should return false if the denominators are zero.

btw You can optimize this function by a lot. Denominators for uA and uB are exactly the same so you should compute the denominator once and save it to a variable. Then you can reuse the variable for both uA and uB.
Also certain subtractions is you algorithm are done twice.
This is my C++ version:

fn Intersect
(line Line1, line Line2)
{
    v2 A = Line1.Start;
    v2 B = Line1.End;
    v2 C = Line2.Start;
    v2 D = Line2.End;
    
    v2 BA = B - A;
    v2 DC = D - C;
    v2 AC = A - C;
    
    f32 Denominator = DC.Y*BA.X - DC.X*BA.Y;
    if(Denominator == 0)
        return false;
    
    f32 U1 = (DC.X*AC.Y - DC.Y*AC.X) / Denominator; 
    f32 U2 = (BA.X*AC.Y - BA.Y*AC.X) / Denominator;
    
    return U1 >= 0 && U1 <= 1 && U2 >= 0 && U2 <= 1;
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions