Determine if two triangles overlap: Difference between revisions

m
m (corrected a misspelling.)
Line 1,903:
1) isInside
 
TestGiven ifA, aB, pointC, P is insidein athe triangle ABC if the three cross-products
PA^PB, PB^PC and PC^PA are of equal sign.
 
{def isInside
{lambda {p1:a p2:b p3:c q:p}
{let { {x1:ax {car p1:a}} {y1:ay {cdr p1:a}}
{x2:bx {car p2:b}} {y2:by {cdr p2:b}}
{x3:cx {car p3:c}} {y3:cy {cdr p3:c}}
{xp:px {car q:p}} {yp:py {cdr q:p}}
} {let { {:w1 {- {* {- :px :ax} {- :cy :ay}}
}
{let { {w1 {- {* {- xp x1} {- y3 y1}} {* {- x3:cx x1:ax} {- yp:py y1:ay}} }}
{w2 {- {* {- xp x2} {:w2 {- y1 y2}} {* {- x1:px x2:bx} {- yp:ay y2:by}} }}
{w3 {- {* {- xp x3} {- y2 y3}} {* {- x2:ax x3:bx} {- yp:py y3:by}} }}
} {or:w3 {and- {>= w1 0}* {>=- w2:px 0:cx} {>=- w3:by 0:cy}}
{and {< w1 0} {<* w2{- :bx 0:cx} {<- w3:py 0}:cy}} }}}}
} {or {and {>= :w1 0} {>= :w2 0} {>= :w3 0}}
{and then{< x else}:w1 0} :p0{< :p1 :p2w2 :q00} {< :q1 :q2w3 0}}} }}}}
-> isInside
 
2) overlapping
 
TestFor ifevery apoints pointin isthe insiderectangle bothsurrounding two given triangles.
Ifwe compute the number of shared points inside both. If it is zero,null trianglesthey don't overlap.
 
{def overlap
Line 1,927 ⟶ 1,931:
{def overlap.row
{lambda {:p0 :p1 :p2 :q0 :q1 :q2 :w :h :y}
{S.map {{lambda {:p0 :p1 :p2 :q0 :q1 :q2 :qp}
{S.mapif {{lambdaand {isInside :p0 :p1 :p2 :q0 :q1 :q2 :qp}
{if {and {isInside :p0q0 :p1q1 :p2q2 :qp}}
then x else}} :p0 :p1 {isInside:p2 :q0 :q1 :q2 :qp}}
then x else}} :p0 :p1 :p2 :q0 :q1 :q2}
{S.map {{lambda {:y :x} {cons :x :y}} :y}
{S.serie :w :h} }}}}
Line 1,940 ⟶ 1,943:
-> overlap
 
Given coordonnees will just be multiplied by 10scaled to become integers., here miltiplied by 10
If the number of shared points is zero, triangles don't overlap.
 
{overlap {cons 0 0} {cons 50 0} {cons 0 50}