Ver Mensaje Individual
  #3  
Antiguo 21-11-2018
cloayza cloayza is offline
Miembro
 
Registrado: may 2003
Ubicación: San Pedro de la Paz, Chile
Posts: 913
Reputación: 22
cloayza Tiene un aura espectacularcloayza Tiene un aura espectacular
Acá mas que una ayuda...
Código Delphi [-]
...

  type TPunto = record
       x,y: Double;
  end;

  type TSolucion = record
     Interseccion: Boolean; {Indica si las rectas se Intersectan}
     X,Y: Double; {Punto de Interseccion}
   end;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    function LineIntersection(A, B, C, D:TPunto):TSolucion;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{
Fuente https://www.geeksforgeeks.org/progra...-of-two-lines/

Adaptado a Delphi por mi...
}

function TForm1.LineIntersection(A, B, C, D:TPunto):TSolucion;
var
   a1, b1, c1, a2, b2, c2, det:double;
begin
      a1 := B.y - A.y;
      b1 := A.x - B.x;
      c1 := a1*(A.x) + b1*(A.y);

      a2 := D.y - C.y;
      b2 := C.x - D.x;
      c2 := a2*(C.x)+ b2*(C.y);

      {Calculo del derminante}
      det := a1*b2 - a2*b1;

      {Si det=0, son rectas paralelas}
      Result.Interseccion:=(det <> 0);
      Result.x:=-1;
      Result.y:=-1;

      if Result.Interseccion then
      begin
           {Calcula el punto de intercepción}
           Result.x := (b2*c1 - b1*c2)/det;
           Result.y := (a1*c2 - a2*c1)/det;
      end;
end;

//****************************************************************************//
// Determines if two lines intersect.                                         //
//****************************************************************************//
procedure TForm1.Button1Click(Sender: TObject);
 var
     A, B, C, D:TPunto;
     solucion:TSolucion;
 begin
      {Recta 1}
      A.X:=1; A.Y:=2;
      B.X:=4; B.Y:=5;

      {Recta 2}
      C.X:=2; C.Y:=4;
      D.X:=5; D.Y:=4;

      Solucion:=LineIntersection(A, B, C, D);

      if not Solucion.Interseccion then
         Showmessage('Rectas paralelas ')
      else
         ShowMessage(format('Las rectas de intersectan en el punto (%f,%f)',[Solucion.X, Solucion.Y]));
end;

end.
Responder Con Cita