[SGU]124. Broken line

来源:互联网 发布:java语言就业培训 编辑:程序博客网 时间:2024/04/27 21:29

Analysis

    判断一个点是否在一个多边形之内。这个多边形不一定是凸多边形,但是所有的边都平行于坐标轴,那么从要判断的点做一条射线,判断与多边形的交点个数是否为奇数即可。我是从该点沿y轴正方向做一条射线,判断相交时注意端点的情况。

Accepted Code

type    point=record        x,y:longint;    end;var    p:array[1..10000,1..2] of point;    k:point;    count,i,n:longint;procedure swap(var a,b:longint);var    tmp:longint;begin    tmp:=a;    a:=b;    b:=tmp;end;begin    readln(n);    for i:=1 to n do    begin        readln(p[i,1].x,p[i,1].y,p[i,2].x,p[i,2].y);        if p[i,2].x<p[i,1].x then            swap(p[i,1].x,p[i,2].x);        if p[i,2].y<p[i,1].y then            swap(p[i,1].y,p[i,2].y);    end;    readln(k.x,k.y);    count:=0;    for i:=1 to n do    begin        if ((p[i,1].x=k.x) and (p[i,2].x=k.x) and (p[i,1].y<=k.y) and (p[i,2].y>=k.y)) or ((p[i,1].y=k.y) and (p[i,2].y=k.y) and (p[i,1].x<=k.x) and (p[i,2].x>=k.x)) then        begin            writeln('BORDER');            exit;        end;        if ((p[i,1].y>k.y) and (p[i,1].x<k.x) and (k.x<=p[i,2].x)) then            inc(count);    end;    if count and 1>0 then        writeln('INSIDE')    else        writeln('OUTSIDE');end.


原创粉丝点击