Allegro使用Skill语言实现根据两点p1,p2确定的线段判断是否与bbox构成的矩形相交的函数
/*****************判断两点p1,p2确定的线段是否与bbox构成的矩形相交的算法*******************/
defun(isLineIntersectRectangle (p1 p2 bbox)
x1=car(p1)
y1=nth(1 p1)
x2=car(p2)
y2=nth(1 p2)
lineHeight=y1-y2
lineWidth=x2-x1
xb=car(car(bbox))
yb=nth(1 car(bbox))
xe=car(nth(1 bbox))
ye=nth(1 nth(1 bbox))
c=x1*y2-x2*y1
let((res)
if(lineHeight*xb+lineWidth*yb+c>=0 && lineHeight*xe+lineWidth*ye+c<=0
||(lineHeight*xb+lineWidth*yb+c<=0 && lineHeight*xe+lineWidth*ye+c>=0)
||(lineHeight*xb+lineWidth*ye+c>=0 && lineHeight*xe+lineWidth*yb+c<=0)
||(lineHeight*xb+lineWidth*ye+c<=0 && lineHeight*xe+lineWidth*yb+c>=0) then
if(xb>xe then
temp=xb
xb=xe
xe=temp
)
if(yb<ye then
temp1=yb
yb=ye
ye=temp1
)
if(x1<xb && x2<xb || (x1>xe && x2>xe) || (y1>yb && y2>yb) || (y1<ye && y2<ye) then
res=nil
else
res=t
)
else
res=nil
)
res=res
)
)
x=isLineIntersectRectangle(-5:0 105:100 list(0:0 100:100));x=t,相交
x=isLineIntersectRectangle(-5:0 -5:100 list(0:0 100:100));x=nil,不相交
x=isLineIntersectRectangle(0:0 100:100 list(0:0 100:100));x=t,相交