ZOJ 1081 Points Within 은 다각형 내 에 있 는 지 판단 합 니 다.
방법:
방사선 L 과 다각형 의 교점 을 계산 할 때 1.다각형 의 수평 변 에 대해 고려 하지 않 는 다.2。다각형 의 정점 과 L 이 교차 하 는 경우 이 정점 이 자신 이 속 한 변 의 세로 좌표 가 비교적 큰 정점 이 라면 계산 하고 그렇지 않 으 면 무시 합 니 다.3。P 가 다각형 가장자리 에 있 는 경우 P 가 다각형 줄 에 속 하 는 지 직접 판단 할 수 있 습 니 다.교점 개수 가 홀수 라면 다각형 안에 있 고 그렇지 않 으 면 없다.이로부터 알고리즘 의 위조 코드 는 다음 과 같다. count ← 0; P 를 단점 으로 하여 오른쪽 에서 왼쪽으로 방사선 L 을 만 듭 니 다. for 다각형 의 각 변 s do if P 는 사 이 드 s 에 있어 요. then return true; if s 수준 아니에요. then if s 의 한 점 은 L 에 있 습 니 다. if 이 점 은 s 양 끝 점 중 세로 좌표 가 비교적 큰 점 이다. then count ← count+1 else if s 와 L 이 교차 합 니 다. then count ← count+1; if count mod 2 = 1 then return true; else return false; 그 중에서 방사선 L 을 만 드 는 방법 은 P '의 세로 좌 표를 P 와 같 고 가로 좌 표를 정 무한대 (큰 정수) 로 설정 하면 P 와 P' 가 방사선 L 을 확정 하 는 것 이다.
var
n,tot,m,i,p,q:longint;
x,y:array[1..100] of longint;
function max(x,y:longint):longint;
begin
if x>y then exit(x)
else exit(y);
end;
function min(x,y:longint):longint;
begin
if x<y then exit(x)
else exit(y);
end;
function pd(p,q:longint):boolean;
var
p1,q1,sum,s,i,s1:longint;
begin
p1:=maxlongint div 200000;
q1:=q;
sum:=0;
for i:=1 to n do
begin
s:=(x[i]-x[i+1])*(q-y[i+1])-(p-x[i+1])*(y[i]-y[i+1]);
if (s=0)and(p<=max(x[i],x[i+1]))and(p>=min(x[i],x[i+1]))and(q<=max(y[i],y[i+1]))and(q>=min(y[i],y[i+1])) then exit(true);
if y[i]=y[i+1] then continue;
s:=(p1-p)*(y[i]-q)-(q1-q)*(x[i]-p);
if (s=0)and(x[i]<=max(p,p1))and(x[i]>=min(p,p1))and(y[i]<=max(q,q1))and(y[i]>=min(q,q1)) then
begin
if y[i]>y[i+1] then inc(sum);
continue;
end;
s:=(p1-p)*(y[i+1]-q)-(q1-q)*(x[i+1]-p);
if (s=0)and(x[i+1]<=max(p,p1))and(x[i+1]>=min(p,p1))and(y[i+1]<=max(q,q1))and(y[i+1]>=min(q,q1)) then
begin
if y[i+1]>y[i] then inc(sum);
continue;
end;
s:=(p1-p)*(y[i]-q)-(q1-q)*(x[i]-p);
s1:=(p1-p)*(y[i+1]-q)-(q1-q)*(x[i+1]-p);
if s*s1>0 then continue;
s:=(x[i+1]-x[i])*(q-y[i])-(y[i+1]-y[i])*(p-x[i]);
s1:=(x[i+1]-x[i])*(q1-y[i])-(y[i+1]-y[i])*(p1-x[i]);
if s*s1>0 then continue;
inc(sum);
end;
if sum mod 2=0
then exit(false)
else exit(true);
end;
begin
read(n);
while n>0 do
begin
if tot>0 then writeln;
inc(tot);
writeln('Problem ',tot,':');
readln(m);
for i:=1 to n do
readln(x[i],y[i]);
x[n+1]:=x[1];
y[n+1]:=y[1];
for i:=1 to m do
begin
readln(p,q);
if pd(p,q)
then writeln('Within')
else writeln('Outside');
end;
read(n);
end;
end.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.