【洛谷 1355】神秘大三角

来源:互联网 发布:慢慢买数据怎么看 编辑:程序博客网 时间:2024/05/16 14:04

问题描述
给出三角形的三点坐标,另给出一点,判断这个点与三角形关系。在三角形内输出1,在外输出2,在线段上输出3,顶点上输出4.
样例输入
(0,0)
(3,0)
(0,3)
(1,1)
样例输出
1
算法讨论
读入后看是否在顶点上,在的话则输出4并终止;利用叉积分别求出点于三边关系,若有任意一边的叉积为0 且点在这条线段内 ,则输出3;若在三角形内则点都会在三条边的同一侧(枚举顶点时要顺着一个方向),即叉积都为正数或都为负数,则在里面,输出1;若不是则在外面,输出2.

var  a:array[0..4,1..2] of longint;  i,j,n,m1,m2,m3,t,p,f1,f2:longint;  st:string;function cp(x1,y1,x2,y2,x,y:longint):longint;var  h:longint;begin  h:=(x1-x)*(y2-y)-(x2-x)*(y1-y);  exit(h)end;function check(x1,y1,x2,y2,x,y:longint):boolean;begin  if (x>x1) and (x>x2) or (y>y1) and (y>y2) or (x<x1) and (x<x2) or (y<y1) and (y<y2)    then exit(false)    else exit(true)end;begin  for i:=1 to 4 do    begin      readln(st);      for j:=2 to length(st) do        if st[j]=','          then break;      t:=1;      p:=j;      for j:=p-1 downto 2 do        begin          val(st[j],n);          a[i,1]:=a[i,1]+n*t;          t:=t*10        end;      inc(p);      for j:=2 to length(st) do        if st[j]=')'          then break;      t:=1;      for j:=length(st)-1 downto p do        begin          val(st[j],n);          a[i,2]:=a[i,2]+n*t;          t:=t*10        end;    end;  for i:=1 to 3 do    if (a[i,1]=a[4,1]) and (a[i,2]=a[4,2])      then begin             write(4);             halt           end;  m1:=cp(a[2,1],a[2,2],a[4,1],a[4,2],a[1,1],a[1,2]);  if (m1=0) and (check(a[1,1],a[1,2],a[2,1],a[2,2],a[4,1],a[4,2]))    then begin           write(3);           halt         end;  if m1>0    then inc(f1)    else inc(f2);  m2:=cp(a[3,1],a[3,2],a[4,1],a[4,2],a[2,1],a[2,2]);  if (m2=0) and (check(a[2,1],a[2,2],a[3,1],a[3,2],a[4,1],a[4,2]))    then begin           write(3);           halt         end;  if m2>0    then inc(f1)    else inc(f2);  m3:=cp(a[1,1],a[1,2],a[4,1],a[4,2],a[3,1],a[3,2]);  if (m3=0) and (check(a[3,1],a[3,2],a[1,1],a[1,2],a[4,1],a[4,2]))    then begin           write(3);           halt         end;  if m3>0    then inc(f1)    else inc(f2);  if (f1=3) or (f2=3)    then write(1)    else write(2)end.

这里写图片描述
Pixiv ID:61826383

0 0