Vladimir_S |
12.11.2012 12:31 |
Цитата:
Сообщение от Сергей1991
(Сообщение 818340)
Треугольник задан координатами вершин.Определить количество общих точек данного треугольника с отрезком,заданным координатами концов.
Помогите написать эту программу в паскале))))))
|
Да пожалуйста, какие проблемы:
Код:
Var
Vertex:Array[1..2,1..3] of real;
SideCoef:Array[1..3,1..3] of real;
La,Lb,Lc,P1x,P2x,P1y,P2y,Xcp,Ycp:real;
CP_in:Array[1..2,1..3] of real;
i,j,N:Byte;
Procedure Vert2Ln(x1,y1,x2,y2:real;var C1,C2,C3:real);
begin
If x1=x2 then
begin
C1:=1;
C2:=0;
C3:=-x1;
end
else
If y1=y2 then
begin
C1:=0;
C2:=1;
C3:=-y1;
end
else
begin
C1:=1/(x2-x1);
C2:=-1/(y2-y1);
C3:=y1/(y2-y1)-x1/(x2-x1);
end;
end;
Function Min(q1,q2:real):real;
begin
If q1<q2 then Min:=q1 else Min:=q2;
end;
Function Max(q1,q2:real):real;
begin
If q1>q2 then Max:=q1 else Max:=q2;
end;
Begin
Writeln('Triangular vertex coordinates:');
For j:=1 to 3 do
For i:=1 to 2 do
begin
If i=1 then write('x') else write('y');
write(j,'= ');
Readln(Vertex[i,j]);
end;
Vert2Ln(Vertex[1,1],Vertex[2,1],
Vertex[1,2],Vertex[2,2],
SideCoef[1,1],SideCoef[1,2],SideCoef[1,3]);
Vert2Ln(Vertex[1,2],Vertex[2,2],
Vertex[1,3],Vertex[2,3],
SideCoef[2,1],SideCoef[2,2],SideCoef[2,3]);
Vert2Ln(Vertex[1,3],Vertex[2,3],
Vertex[1,1],Vertex[2,1],
SideCoef[3,1],SideCoef[3,2],SideCoef[3,3]);
Writeln('Line point coordinates:');
Write('x1= ');
Readln(P1x);
Write('y1= ');
Readln(P1y);
Write('x2= ');
Readln(P2x);
Write('y2= ');
Readln(P2y);
Vert2Ln(P1x,P1y,P2x,P2y,La,Lb,Lc);
N:=0;
For i:=1 to 3 do
If SideCoef[i,1]*Lb-La*SideCoef[i,2]<>0 then
begin
Xcp:=(SideCoef[i,2]*Lc-SideCoef[i,3]*Lb)/(SideCoef[i,1]*Lb-La*SideCoef[i,2]);
Ycp:=(SideCoef[i,3]*La-SideCoef[i,1]*Lc)/(SideCoef[i,1]*Lb-La*SideCoef[i,2]);
if (((i=1) and
(Xcp<=Max(Vertex[1,1],Vertex[1,2])) and
(Xcp>=Min(Vertex[1,1],Vertex[1,2])) and
(Ycp<=Max(Vertex[2,1],Vertex[2,2])) and
(Ycp>=Min(Vertex[2,1],Vertex[2,2]))) or
((i=2) and
(Xcp<=Max(Vertex[1,2],Vertex[1,3])) and
(Xcp>=Min(Vertex[1,2],Vertex[1,3])) and
(Ycp<=Max(Vertex[2,2],Vertex[2,3])) and
(Ycp>=Min(Vertex[2,2],Vertex[2,3]))) or
((i=3) and
(Xcp<=Max(Vertex[1,3],Vertex[1,1])) and
(Xcp>=Min(Vertex[1,3],Vertex[1,1])) and
(Ycp<=Max(Vertex[2,3],Vertex[2,1])) and
(Ycp>=Min(Vertex[2,3],Vertex[2,1])))) and
((Xcp<=Max(P1x,P2x)) and
(Xcp>=Min(P1x,P2x)) and
(Ycp<=Max(P1y,P2y)) and
(Ycp>=Min(P1y,P2y))) then
begin
Inc(N);
CP_in[1,N]:=Xcp;
CP_in[2,N]:=Ycp;
end;
end;
If ((N=2) and (CP_in[1,1]=CP_in[1,2]) and (CP_in[2,1]=CP_in[2,2])) or
((N=3) and (((CP_in[1,1]=CP_in[1,2]) and (CP_in[2,1]=CP_in[2,2])) or
((CP_in[1,2]=CP_in[1,3]) and (CP_in[2,2]=CP_in[2,3])) or
((CP_in[1,3]=CP_in[1,1]) and (CP_in[2,3]=CP_in[2,1])))) then
Dec(N);
Writeln('Number of points = ',N);
Readln;
End.
|