Fri Aug 31, 2012 10:09 pm
Admin Cho tam giác ABC và đoạn thẳng MN . Tính phần đoạn thẳng nằm trong tam giác .
- Code:
Const fi='doanthang.inp'; fo='doanthang.out';
Type diem=record x,y:Integer; End;
Type dt=Record a,b,c:Integer; End;
Var M,N:diem;
dem,t:Integer;
s2,s:real;
Tg:Array[1..3] Of diem;
f,g:text;
Procedure doctep;
Var i:Integer;
Begin
Assign(f,fi);
Reset(f);
Readln(f,n.x,n.y);
Readln(f,m.x,m.y);
t:=3;
For i:=1 to t do
Read(f,tg[i].x,tg[i].y);
Close(f);
Assign(g,fo);
Rewrite(g);
End;
Function Line(M1,A1,B1:Diem):Integer;
Begin
Line:=(B1.y-M1.y)*(A1.x-M1.x)-(A1.y-M1.y)*(B1.x-M1.x);
End;
Procedure XDDT(a2,b2:diem; Var t1:dt);
Begin
t1.a:=A2.y-B2.y;
t1.b:=B2.x-A2.x;
t1.c:=A2.x*B2.y-A2.y*B2.x;
End;
Function xuly(x1,x2:diem):boolean;
Var j1,i,j,k:Integer; v1,v2,v3:diem; t2,t1:dt; s3,s4:Real;
chon:Array[1..3] Of boolean;
Begin
xuly:=false;
{-----K-tra Neu mot diem trung voi dinh tam giac-----}
For i:=1 to t do
If (tg[i].x=x1.x) and (tg[i].y=x1.y) then
Begin
If i=1 then begin v1:=tg[i]; v2:=tg[i+1]; v3:=tg[i+2]; End
Else Begin v1:=tg[i]; v2:=tg[i-1]; v3:=tg[i+1]; End;
For j:=1 to t-1 do
If (tg[j].x=x2.x) and (tg[j].y=x2.y) then Begin xuly:=true; Exit; End;
If (Line(x2,v2,v3)*Line(x1,v2,v3)>0) then
If (Line(x2,v1,v3)*Line(v2,v1,v3)<0) or (Line(x2,v1,v2)*Line(v3,v1,v2)<0) then
begin xuly:=true; Exit; End;
If (Line(x2,v1,v3)=0) or (Line(x2,v1,v2)=0) then
Begin xuly:=true; Exit; End;
If (Line(x2,v2,v3)=0) or( (Line(x2,v2,v3)*Line(x1,v2,v3) >0) and (Line(x2,v1,v2)*Line(v3,v1,v2) >0) and (Line(x2,v1,v3)*Line(v2,v1,v3)>0)) then
Begin Writeln(g,sqrt(sqr(x1.x-x2.x)+sqr(x1.y-x2.y)):3:2); exit; End;
If Line(x1,v2,v3)*Line(x2,v2,v3) <0 then
Begin
xddt(v2,v3,t1);
Writeln(g,abs(t1.a*x1.x+T1.b*x1.y+t1.c)/sqrt(sqr(t1.a)+sqr(t1.b)):3:2); Exit;
End;
End;
{-----K-tra Neu mot diem nam tren canh tam giac-----}
For i:=2 to t do
For j:=1 to i-1 do
If Line(x1,tg[i],tg[j])=0 then
Begin
For j1:=1 to t-1 do If (j1<>i) and (J1<>j) then k:=j1;
If Line(x2,tg[i],tg[j])=0 then begin xuly:=true; Exit; End;
If (Line(x2,tg[i],tg[k])=0) or (Line(x2,tg[j],tg[k])=0) then
Begin Writeln(g,sqrt(sqr(x1.x-x2.x)+sqr(x1.y-x2.y)):3:2); exit; End;
If Line(x1,tg[i],tg[k])*Line(x2,tg[i],tg[k]) <0 then
Begin xddt(tg[i],tg[k],t1);
Writeln(g,abs(t1.a*x1.x+T1.b*x1.y+t1.c)/sqrt(sqr(t1.a)+sqr(t1.b)):3:2); Exit;
End;
If Line(x1,tg[j],tg[k])*Line(x2,tg[j],tg[k]) <0 then
Begin xddt(tg[j],tg[k],t1);
Writeln(g,abs(t1.a*x1.x+T1.b*x1.y+t1.c)/sqrt(sqr(t1.a)+sqr(t1.b)):3:2); Exit;
End;
End;
{-----K-tra 1 diem nam trong tam giac------}
v1:=tg[1];v2:=tg[2];v3:=tg[3];
If( Line(x1,v1,v2)*Line(v3,v1,v2) >0) and (Line(x1,v1,v3)*Line(v2,v1,v3)>0) and (Line(x1,v2,v3)*Line(v1,v2,v3)>0) then
Begin
If( Line(x2,v1,v2)*Line(v3,v1,v2) >0) and (Line(x2,v1,v3)*Line(v2,v1,v3)>0) and (Line(x2,v2,v3)*Line(v1,v2,v3)>0) then
Begin xuly:=true; Exit; End;
If (Line(x2,v1,v2)=0) or (Line(x2,v1,v3)=0) or (Line(x2,v2,v3)=0) then
Begin Writeln(g,sqrt(sqr(x1.x-x2.x)+sqr(x1.y-x2.y)):3:2); exit; End;
For i:=2 to t do
For k:=1 to i-1 do
If Line(x1,tg[i],tg[k])*Line(x2,tg[i],tg[k]) <0 then
Begin xddt(tg[i],tg[k],t1); Writeln(g,abs(t1.a*x1.x+T1.b*x1.y+t1.c)/sqrt(sqr(t1.a)+sqr(t1.b)):3:2); Exit; End;
End;
{-----K-Tra Neu mot diem nam ngoai-----}
For i:=2 to t do
For j:=1 to i-1 do
Begin For j1:=1 to t do If (j1<>i) and (j1<>j) then k:=j1;
v1:=tg[i];v2:=tg[j];v3:=tg[k];
If (Line(x1,v1,v2)*Line(v3,v1,v2)<0) and (Line(x1,v1,v3)*Line(v2,v1,v3)>0) and (Line(x1,v2,v3)*Line(v1,v2,v3)>0) then
Begin If Line(x2,tg[i],tg[j])=0 then begin xuly:=true; Exit; End;
If (Line(x2,tg[i],tg[k])=0) then
Begin xddt(tg[i],tg[k],t1); Writeln(g,abs(t1.a*x2.x+T1.b*x2.y+t1.c)/sqrt(sqr(t1.a)+sqr(t1.b)):3:2); Exit; End;
If (Line(x2,tg[j],tg[k])=0) then
Begin xddt(tg[j],tg[k],t1);
Writeln(g,abs(t1.a*x2.x+T1.b*x2.y+t1.c)/sqrt(sqr(t1.a)+sqr(t1.b)):3:2); Exit; End;
If (Line(x2,v1,v2)*Line(v3,v1,v2)>0) and (Line(x2,v1,v3)*Line(v2,v1,v3)>0) and (Line(x2,v2,v3)*Line(v1,v2,v3)>0) then
Begin xddt(v1,v2,t1);
Writeln(g,abs(t1.a*x2.x+T1.b*x2.y+t1.c)/sqrt(sqr(t1.a)+sqr(t1.b)):3:2); Exit; End;
If (Line(x2,v1,v3)*Line(v2,v1,v3)<0) and (Line(x2,v1,v2)*Line(v3,v1,v2)>0) and (Line(x2,v2,v3)*Line(v1,v2,v3)>0) then
Begin
xddt(v1,v3,t2);xddt(v1,v2,t1);
s3:=abs(t1.a*x1.x+T1.b*x1.y+t1.c)/sqrt(sqr(t1.a)+sqr(t1.b));
s4:=abs(t2.a*x2.x+T2.b*x2.y+t2.c)/sqrt(sqr(t2.a)+sqr(t2.b));
Writeln(g,sqrt(sqr(x1.x-x2.x)+sqr(x1.y-x2.y))-s3-s4:3:2);
Exit;
End;
If (Line(x2,v2,v3)*Line(v2,v2,v3)<0) and (Line(x2,v1,v2)*Line(v3,v1,v2)>0) and (Line(x2,v1,v3)*Line(v1,v1,v3)>0) then
Begin
xddt(v2,v3,t2);xddt(v1,v2,t1);
s3:=abs(t1.a*x1.x+T1.b*x1.y+t1.c)/sqrt(sqr(t1.a)+sqr(t1.b));
s4:=abs(t2.a*x2.x+T2.b*x2.y+t2.c)/sqrt(sqr(t2.a)+sqr(t2.b));
Writeln(g,sqrt(sqr(x1.x-x2.x)+sqr(x1.y-x2.y))-s3-s4:3:2);
Exit;
End;
End;
End;
End;
Begin
doctep;
If xuly(N,M) then Writeln(g,0);
Close(g);
End.