_______________
- Code: Select all
InterseccionRectLine(Rectangulo:TRect;puntoinicial,puntofinal:Tpoint):boolean; // Licensed under BSD Elias L. 2005
var a,b,ix,iy:extended;
begin;
Result:=false;
if (((rectangulo.Left<puntoinicial.X) and (puntoinicial.X<rectangulo.Right)) and//si al menos uno de los puntos
((rectangulo.top<puntoinicial.y) and (puntoinicial.y<rectangulo.bottom))) or //de la linea esta dentro del Rectangulo...
(((rectangulo.Left<puntofinal.X) and (puntofinal.X<rectangulo.Right)) and
((rectangulo.top<puntofinal.y) and (puntofinal.y<rectangulo.bottom))) then Result:=true
else begin
if (puntoinicial.x<>puntofinal.x) and (puntoinicial.y<>puntofinal.y) then begin //si la linea es diagonal
a:=(puntofinal.Y*puntoinicial.X - puntoinicial.Y*puntofinal.X)/(puntoinicial.X-puntofinal.X); //hallar a(intercepción)
b:=(puntofinal.Y-puntoinicial.Y)/(puntofinal.X-puntoinicial.X); //y b(pendiente) para la acuacion de la recta y=a+bx
ix:=(rectangulo.Top-a)/b; //hallar interseccion de la linea con el lado superior en x
if (((puntoinicial.x<=ix) and (ix<=puntofinal.x)) or ((puntofinal.x<=ix) and (ix<=puntoinicial.x))) and //Comprobar límites
((rectangulo.left<=ix) and (ix<=rectangulo.Right)) then result:=true;
ix:=(rectangulo.bottom-a)/b; //hallar interseccion de la linea con el lado inferior en x
if (((puntoinicial.x<=ix) and (ix<=puntofinal.x)) or ((puntofinal.x<=ix) and (ix<=puntoinicial.x))) and //Comprobar límites
((rectangulo.left<=ix) and (ix<=rectangulo.Right)) then result:=true;
iy:=(b*rectangulo.Left)+a; //hallar interseccion de la linea con el lado izquierdo en y
if (((puntoinicial.y<=iy) and (iy<=puntofinal.y)) or ((puntofinal.y<=iy) and (iy<=puntoinicial.y))) and //Comprobar límites
((rectangulo.top<=iy) and (iy<=rectangulo.bottom)) then result:=true;
iy:=(b*rectangulo.right)+a; //hallar interseccion de la linea con el lado derecho en y
if (((puntoinicial.y<=iy) and (iy<=puntofinal.y)) or ((puntofinal.y<=iy) and (iy<=puntoinicial.y))) and //Comprobar límites
((rectangulo.top<=iy) and (iy<=rectangulo.bottom)) then result:=true;end;
if (puntoinicial.y=puntofinal.y) then //si la linea es horizontal
if ((rectangulo.top<=puntoinicial.y) and (puntoinicial.y<=rectangulo.Bottom)) and //Comprobar límites
( ((puntoinicial.x<=rectangulo.right)and(rectangulo.right<=puntofinal.x)) or
((puntofinal.x<=rectangulo.right)and(rectangulo.right<=puntoinicial.x)) or
((puntoinicial.x<=rectangulo.left)and(rectangulo.left<=puntofinal.x)) or
((puntofinal.x<=rectangulo.left)and(rectangulo.left<=puntoinicial.x)) ) then Result:=true;
if (puntoinicial.x=puntofinal.x) then //si la linea es vertical
if ((rectangulo.left<=puntoinicial.x) and (puntoinicial.x<=rectangulo.right)) and //Comprobar límites
( ((puntoinicial.y<=rectangulo.bottom)and(rectangulo.bottom<=puntofinal.y)) or
((puntofinal.y<=rectangulo.bottom)and(rectangulo.bottom<=puntoinicial.y)) or
((puntoinicial.y<=rectangulo.top)and(rectangulo.top<=puntofinal.y)) or
((puntofinal.y<=rectangulo.top)and(rectangulo.top<=puntoinicial.y)) ) then Result:=true;end;
end;