Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- line segment intersections
- function setup()
- w = 0
- l1ax,l1ay,l1bx,l1by,l2ax,l2ay,l2bx,l2by = randlines()
- watch("l1ax")
- watch("l1ay")
- watch("l1bx")
- watch("l1by")
- watch("l2ax")
- watch("l2ay")
- watch("l2bx")
- watch("l2by")
- end
- -- This function gets called once every frame
- function draw()
- background(0, 0, 0, 255)
- strokeWidth(3)
- showlines(l1ax,l1ay,l1bx,l1by,l2ax,l2ay,l2bx,l2by)
- if w % 30 == 0 then
- l1ax,l1ay,l1bx,l1by,l2ax,l2ay,l2bx,l2by = randlines()
- w = 0
- end
- w = w + 1
- end
- function randlines()
- l1ax = math.random()*WIDTH
- l1ay = math.random()*HEIGHT
- l1bx = math.random()*WIDTH
- l1by = math.random()*HEIGHT
- l2ax = math.random()*WIDTH
- l2ay = math.random()*HEIGHT
- l2bx = math.random()*WIDTH
- l2by = math.random()*HEIGHT
- return l1ax,l1ay,l1bx,l1by,l2ax,l2ay,l2bx,l2by
- end
- function showlines(l1ax,l1ay,l1bx,l1by,l2ax,l2ay,l2bx,l2by)
- stroke(255, 0, 0, 255)
- line(l1ax,l1ay,l1bx,l1by)
- stroke(0, 0, 255, 255)
- line(l2ax,l2ay,l2bx,l2by)
- stroke(0,255,0,255)
- strokeWidth(9)
- it, iax, iay, ibx, iby = getintersection(l1ax,l1ay,l1bx,l1by,l2ax,l2ay,l2bx,l2by)
- if it then
- line(iax,iay,ibx,iby)
- elseif not it then
- line(iax+10,iay+10,ibx-10,iby-10)
- line(iax-10,iay+10,ibx+10,iby-10)
- else
- line(iax,iay,ibx,iby)
- end
- end
- function getintersection(x1, y1, x2, y2, x3, y3, x4, y4)
- --special thanks to voiceofreason
- d = (y4-y3)*(x2-x1)-(x4-x3)*(y2-y1)
- Ua_n = ((x4-x3)*(y1-y3)-(y4-y3)*(x1-x3))
- Ub_n = ((x2-x1)*(y1-y3)-(y2-y1)*(x1-x3))
- if d == 0 then
- if Ua_n == 0 and Ua_n == Ub_n then
- --they are on the same plane
- --they are either
- --the same point
- --the same line
- --two unconnected lines
- --slightly overlapping
- --one within the other
- --find max and least x for line 1
- if x1 > x2 then
- mxl1 = x1 -- max x of line 1
- lxl1 = x2 -- least x of line 1
- elseif l1ax <= l1bx then
- mxl1 = x2 -- max x of line 1
- lxl1 = x1 -- least x of line 1
- end
- --find max and least x for line 2
- if x3 > x4 then
- mxl2 = x3 -- max x of line 2
- lxl2 = x4 -- least x of line 2
- elseif x3 <= x4 then
- mxl2 = x4 -- max x of line 2
- lxl2 = x3 -- least x of line 2
- end
- if (mxl1 < lxl2) or (lxl1 > mxl2) then
- -- no shared x
- mx = (l1ax+l1bx+l2ax+l2bx)/4 --midx
- my = (l1ay+l1by+l2ay+l2by)/4 --midy
- return false,mx,my,mx,my
- end
- --find max and least y for line 1
- if y1 > y2 then
- myl1 = y1 -- max y of line 1
- lyl1 = y2 -- least y of line 1
- elseif y1 <= y2 then
- myl1 = y2 -- max y of line 1
- lyl1 = y1 -- least y of line 1
- end
- --find max and least y for line 2
- if y3 > y4 then
- myl2 = y3 -- max y of line 2
- lyl2 = y4 -- least y of line 1
- elseif y3 <= y4 then
- myl2 = y4 -- max y of line 1
- lyl2 = y3 -- least y of line 1
- end
- if (myl1 < lyl2) or (lyl1 > myl2) then
- -- no shared y
- mx = (l1ax+l1bx+l2ax+l2bx)/4 --midx
- my = (l1ay+l1by+l2ay+l2by)/4 --midy
- return false,mx,my,mx,my
- end
- if (lxl1 == lxl2) and (mxl1 == mxl2) and (lxl1 == mxl1) then
- --x is the same for all points
- if (lyl1 == lyl2) then --the least y is common
- if (myl1 == myl2) then -- the max y is common
- if (lyl1 == myl2) then
- --the lines are really just one point
- return true, x1, y1, x1, y1
- else --they are the same vertical line
- return math.huge, lxl1, lyl1, mxl1, myl1
- end
- elseif (myl1 > myl2) then
- --the vertical line 1 is longer
- return math.huge, lxl1, lyl1, mxl2, myl2
- else --the vertical line 2 is longer
- return math.huge, lxl1, lyl1, mxl1, myl1
- end
- elseif (myl1 == myl2) then -- the max y is common
- --point and same vertical line handled above
- if (lyl1 > lyl2) then -- the vertcal line 2 is longer
- --aka line 2 starts lower than line 1
- return math.huge, lxl1, lyl1, mxl1, myl1
- else -- the vertcal line 1 is longer
- return math.huge, lxl1, lyl1, mxl2, myl2
- end
- elseif (lyl1 == myl2) then
- --the vertical lines are end to end
- --with line 1 above line 2
- return true, lxl1, lyl1, mxl2, myl2
- elseif (lyl2 == myl1) then
- --the vertical lines are end to end
- --with line 2 above line 1
- return true, lxl2, lyl2, mxl1, myl1
- elseif (lyl1 < lyl2) then
- --line 1 is lowest
- if (myl1 < myl2) then
- --line 2 is highest
- --overlapping lines
- return math.huge, lxl2, lyl2, mxl1, myl1
- else
- --line 1 is highest
- --line 2 lies within line 1
- return math.huge, lxl2, lyl2, mxl2, myl2
- end
- else --line 2 is lowest
- if (myl1 < myl2) then
- --line 2 is highest
- --line 1 lies within line 2
- return math.huge, lxl1, lyl1, mxl1, myl1
- else
- --line 1 is highest
- --overlapping lines
- return math.huge, lxl1, lyl1, mxl2, myl2
- end
- end --all vertical line tests
- end --vertical lines
- if (lyl1 == lyl2) and (myl1 == myl2) and (lyl1 == myl1) then
- --y is the same for all points
- if (lxl1 == lxl2) then --the least x is common
- if (mxl1 == mxl2) then -- the max x is common
- --same point handled under vertical lines
- --they are the same horizontal line
- return math.huge, lxl1, lyl1, mxl1, myl1
- elseif (mxl1 > mxl2) then
- --the horizontal line 1 is longer
- return math.huge, lxl1, lyl1, mxl2, myl2
- else --the horizontal line 2 is longer
- return math.huge, lxl1, lyl1, mxl1, myl1
- end
- elseif (mxl1 == mxl2) then -- the max x is common
- --point and same line handled above
- if (lxl1 > lxl2) then -- the horizontal line 2 is longer
- --aka line 2 starts left of line 1
- return math.huge, lxl1, lyl1, mxl1, myl1
- else -- the horizontal line 1 is longer
- return math.huge, lxl1, lyl1, mxl2, myl2
- end
- elseif (lxl1 == mxl2) then
- --the horizontal lines are end to end
- --with line 1 to the right of line 2
- return true, lxl1, lyl1, mxl2, myl2
- elseif (lxl2 == mxl1) then
- --the horizontal lines are end to end
- --with line 2 to the right of line 1
- return true, lxl2, lyl2, mxl1, myl1
- elseif (lxl1 < lxl2) then
- --line 1 is left
- if (mxl1 < mxl2) then
- --line 2 is right
- --overlapping lines
- return math.huge, lxl2, lyl2, mxl1, myl1
- else
- --line 1 is right
- --line 2 lies within line 1
- return math.huge, lxl2, lyl2, mxl2, myl2
- end
- else --line 2 is left
- if (mxl1 < mxl2) then
- --line 2 is right
- --line 1 lies within line 2
- return math.huge, lxl1, lyl1, mxl1, myl1
- else
- --line 1 is left
- --overlapping lines
- return math.huge, lxl1, lyl1, mxl2, myl2
- end
- end --all horizontal line tests
- end --horizontal lines
- if ((lxl1 == lxl2) and (mxl1 == mxl2)) then
- --((lyl1 = lyl2) and (myl1 = myl2))
- --all remaining same lines
- return math.huge, x1, y1, x2, y2
- end
- if (lxl1 == lxl2) then --the least x is common
- if (mxl1 > mxl2) then
- --the line 1 is longer
- return math.huge, lxl1, lyl1, mxl2, myl2
- else --the line 2 is longer
- return math.huge, lxl1, lyl1, mxl1, myl1
- end
- elseif (mxl1 == mxl2) then --the max x is common
- if (lxl1 > lxl2) then
- --the line 1 is longer
- return math.huge, lxl2, lyl2, mxl2, myl2
- else --the line 2 is longer
- return math.huge, lxl1, lyl1, mxl1, myl1
- end
- elseif (lxl1 == mxl2) then
- --the horizontal lines are end to end
- --with line 1 to the right of line 2
- return true, lxl1, lyl1, mxl2, myl2
- elseif (lxl2 == mxl1) then
- --the horizontal lines are end to end
- --with line 2 to the right of line 1
- return true, lxl2, lyl2, mxl1, myl1
- elseif (lxl1 < lxl2) then
- --line 1 is left
- if (mxl1 < mxl2) then
- --line 2 is right
- --overlapping lines
- return math.huge, lxl2, lyl2, mxl1, myl1
- else
- --line 1 is right
- --line 2 lies within line 1
- return math.huge, lxl2, lyl2, mxl2, myl2
- end
- else --line 2 is left
- if (mxl1 < mxl2) then
- --line 2 is right
- --line 1 lies within line 2
- return math.huge, lxl1, lyl1, mxl1, myl1
- else
- --line 1 is left
- --overlapping lines
- return math.huge, lxl1, lyl1, mxl2, myl2
- end
- end
- --return true,x1,y1,x2,y2
- end --lines in same plane / parallel
- mx = (l1ax+l1bx+l2ax+l2bx)/4 --midx
- my = (l1ay+l1by+l2ay+l2by)/4 --midy
- return false,mx,my,mx,my
- --return false
- end
- Ua = Ua_n / d
- Ub = Ub_n / d
- ia = x1 + (Ua*(x2-x1))
- ib = y1 + (Ua*(y2-y1))
- if Ua >= 0 and Ua <= 1 and Ub >= 0 and Ub <= 1 then
- --return true
- return true,ia,ib,ia,ib
- end
- mx = (l1ax+l1bx+l2ax+l2bx)/4 --midx
- my = (l1ay+l1by+l2ay+l2by)/4 --midy
- --return false
- return false,mx,my,mx,my
- end
- function segmentIntersects(x1, y1, x2, y2, x3, y3, x4, y4)
- --special thanks to voiceofreason
- d = (y4-y3)*(x2-x1)-(x4-x3)*(y2-y1)
- Ua_n = ((x4-x3)*(y1-y3)-(y4-y3)*(x1-x3))
- Ub_n = ((x2-x1)*(y1-y3)-(y2-y1)*(x1-x3))
- if d == 0 then
- if Ua_n == 0 and Ua_n == Ub_n then
- return true
- end
- return false
- end
- Ua = Ua_n / d
- Ub = Ub_n / d
- if Ua >= 0 and Ua <= 1 and Ub >= 0 and Ub <= 1 then
- return true
- end
- return false
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement