V
V
Vinogradov2022-02-23 16:41:22
Pascal
Vinogradov, 2022-02-23 16:41:22

How to write an algorithm for finding the intersection points of two segments?

I have a lab in the Lazarus development environment. It is not possible to compose an algorithm for finding the intersection points of two segments. The task itself is below:

Two segments A and B are given, given by the coordinates of their ends (AX1,AY1), (AX2,AY2) and
(BX1,BY1),(BX2,BY2) - integers. Determine if they intersect, in case of intersection,
calculate the coordinates (real numbers) of the point of their intersection.

Here is the code that I got:

procedure TForm1.Button1Click(Sender: TObject);

const e=0.0001;
var x1,y1,x2,y2,x3,y3,x4,y4,x,y:real;
  str1,str2:string;
begin
clrscr;
Memo1.clear();

x1 := StrToInt(Edit1.Text);
y1 := StrToInt(Edit2.Text);
x2 := StrToInt(Edit3.Text);
y2 := StrToInt(Edit4.Text);

x3 := StrToInt(Edit5.Text);
y3 := StrToInt(Edit6.Text);
x4 := StrToInt(Edit7.Text);
y4 := StrToInt(Edit8.Text);


if(abs(x1-x2)<e)and(abs(x3-x4)<e)or(abs((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1))<e)then
Memo1.lines.add('Отрезки параллельны')
else
 begin
  if(abs(x1-x2)<e)and(abs(y3-y4)<e)then
   begin
    x:=x1;
    y:=y3;
   end
  else if(abs(x3-x4)<e)and(abs(y1-y2)<e)then
   begin
    x:=x3;
    y:=y1;
   end
  else
   begin


    x:=-((x1*y2-x2*y1)*(x4-x3)-(x3*x4-x4*y3)*(x2-x1))/((y1-y2)*(x4-x3)-(y3-y4)*(x2-x1));
    y:=((y3-y4)*(-x)-(x3*y4-x4*y3))/(x4-x3);
    if((x>=x1)and(x<=x2))or((x>=x2)and(x<=x))then begin
    str1:=FloatToStr(x);
    str2:=FloatToStr(y);
    Memo1.lines.add(str1+str2);
    end

    else Memo1.Lines.add('Не пересекаются');

   end;

 end;
end;
end.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
HemulGM, 2022-02-23
@HemulGM

And what does Pascal have to do with it, and especially the Lazarus environment? This is your algorithm crookedly works. Problems are not solved here, problems are solved here.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question