Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Czapa10 opened this issue Feb 14, 2024 · 0 comments
Open

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

Czapa10 opened this issue Feb 14, 2024 · 0 comments

Comments

@Czapa10
Copy link

Czapa10 commented Feb 14, 2024

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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant