您的位置:首页 > 数据库

SQL/POSTGIS 使用函数实现ST_Intersects

2016-05-08 21:28 585 查看
create or replace function ST_LineIntersects(x1 float, y1 float, x2 float, y2 float, x3 float, y3 float, x4 float, y4 float)
returns boolean
as $$
declare a1 float;
a2 float;
b1 float;
b2 float;
c1 float;
c2 float;
begin
a1 = y2 - y1;
b1 = x1 - x2;
c1 = a1*x1 + b1*y1;
a2 = y4 - y3;
b2 = x3 - x4;
c2 = a2*x3 + b2*y3;
if (a1*x3 + b1*y3) = c1 and (a1*x4 + b1*y4) = c1 then
begin
if (min(x1,x2)<=min(x3,x4) and min(x3,x4)<=max(x1,x2)) or (min(x1,x2)>=min(x3,x4) and max(x3,x4)>=min(x1,x2)) then
return true;
else
return false;
end if;
end;
elsif ((a1*x3+b1*y3-c1)*(a1*x4+b1*y4-c1)<=0) and ((a2*x1+b2*y1-c2)*(a2*x2+b2*y2-c2)<=0) then
return true;
end if;
return false;
end;
$$ language plpgsql;


算法思路,相交线段必然是一条线段的两点分在另一条线段的两侧,则另L1的两端点在L2的两侧,L2的两端点在L1的两侧,则相交成立。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: