欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

凸包。

程序员文章站 2022-04-01 16:11:35
...
[url]http://blog.sina.com.cn/s/blog_9dff1a750101ag0l.html[/url]



const zero=1e-6; maxn=100000;
type point=record x,y:extended; end;
var p:array[1..maxn]of point;
ch:array[1..maxn]of longint;
temp,n,m,i,j,k:longint;
function sgn(x:extended):longint; inline;
begin
if abs(x)<zero then exit(0);
if x<0 then sgn:=-1 else sgn:=1;
end;
function cross(a,b,c:point):extended; inline;
begin
cross:=(b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
end;
function dist(a,b:point):extended; inline;
begin
dist:=sqr(a.x-b.x)+sqr(a.y-b.y);
end;
function cmp(a,b,c:point):boolean; inline;
var temp:longint;
begin
temp:=sgn(cross(a,b,c));
if temp<>0 then exit(temp<0); {*B}
cmp:=dist(a,b)<dist(a,c); {*A}
end;
begin
assign(input,'Hull.in'); reset(input);
assign(output,'Hull1.out'); rewrite(output);
readln(n);
for i:=1 to n do
begin
readln(p[i].x,p[i].y);
if (j=0)or(p[i].x<p[j].x)or
(sgn(p[i].x-p[j].x)=0)and(p[i].y<p[j].y)
then j:=i;
end;
temp:=j;
while true do
begin
k:=0;
inc(m); ch[m]:=j;
for i:=1 to n do
if (i<>j)and((k=0)or
cmp(p[j],p[k],p[i]))
then k:=i;
if k=temp then break;
j:=k;
end;
for i:=1 to m do
writeln(p[ch[i]].x:0:2,' ',p[ch[i]].y:0:2);
close(input); close(output);
end.
相关标签: 凸包