Intersection of two line segments

Share for you function to calculate two line segments intersection.
line segments intersection

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
function intersection(p11,p12,p21,p22)
        local Z  = (p12.y-p11.y)*(p21.x-p22.x)-(p21.y-p22.y)*(p12.x-p11.x);
        local Ca = (p12.y-p11.y)*(p21.x-p11.x)-(p21.y-p11.y)*(p12.x-p11.x);
        local Cb = (p21.y-p11.y)*(p21.x-p22.x)-(p21.y-p22.y)*(p21.x-p11.x);

        if Z == 0 and Ca == 0 and Cb == 0 then return nil end


        if Z == 0 then return nil end


        local Ua = Ca/Z;
        local Ub = Cb/Z;

        local pt = {}
        pt.x = p11.x + (p12.x - p11.x) * Ub;
        pt.y = p11.y + (p12.y - p11.y) * Ub;

        if 0 <= Ua and Ua <= 1 and 0 <= Ub and Ub <= 1 then
--          ( (Ua == 0)||(Ua == 1)||(Ub == 0)||(Ub == 1) ) ?
--              result.type = ctOnBounds :
--              result.type = ctInBounds;
        else
            return nil
        end

        return pt
end

4 thoughts on “Intersection of two line segments

  1. I see a lot of interesting content on your blog. You have to spend a lot of time writing, i know how to save you a lot of work, there is a tool that creates unique,
    google friendly posts in couple of minutes, just type in google
    - k2 unlimited content

  2. Finally i quit my regular job, now i earn a lot
    of money online you should try too, just type in google – bluehand roulette system

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>