SRL Archive - SRL Archives

Intersect


Special Ed

2008-05-02 19:25:35

Finds where LineA and LineB intersect. I'm keeping them as TBoxs since there isn't anything called TLine… at least not that I know of. Maybe TPoints would be better but this served it purpose so I thought I'd share it with the community.

program New;
var
Pt: TPoint;

function Intersect(LineA, LineB: TBox): TPoint;
var
A, B, C: array [0..1] of Integer;
Det: Integer;
begin
A[0]:= LineA.y2 - LineA.y1;
B[0]:= LineA.x1 - LineA.x2;
C[0]:= (A[0] * LineA.x1) + (B[0] * LineA.y1);

A[1]:= LineB.y2 - LineB.y1;
B[1]:= LineB.x1 - LineB.x2;
C[1]:= (A[1] * LineB.x1) + (B[1] * LineB.y1);

Det:= (A[0] * B[1]) - (A[1] * B[0]);
if(Det <> 0)then
begin
Result.x:= ((B[1] * C[0]) - (B[0] * C[1])) div Det;
Result.y:= ((A[0] * C[1]) - (A[1] * C[0])) div Det;
end else
begin
Result.x:= -1;
Result.y:= -1;
end;
end;


begin
Pt:= Intersect(IntToBox(0, 0, 4, 4), IntToBox(4, 0, 0, 4));
Writeln('x:= ' + inttostr(Pt.x));
Writeln('y:= ' + inttostr(Pt.y));
end.

Camaro'

2008-05-02 20:12:02

From: Special Ed
Finds where LineA and LineB intersect. I'm keeping them as TBoxs since there isn't anything called TLine… at least not that I know of. Maybe TPoints would be better but this served it purpose so I thought I'd share it with the community.

program New;
var
Pt: TPoint;

function Intersect(LineA, LineB: TBox): TPoint;
var
A, B, C: array [0..1] of Integer;
Det: Integer;
begin
A[0]:= LineA.y2 - LineA.y1;
B[0]:= LineA.x1 - LineA.x2;
C[0]:= (A[0] * LineA.x1) + (B[0] * LineA.y1);

A[1]:= LineB.y2 - LineB.y1;
B[1]:= LineB.x1 - LineB.x2;
C[1]:= (A[1] * LineB.x1) + (B[1] * LineB.y1);

Det:= (A[0] * B[1]) - (A[1] * B[0]);
if(Det <> 0)then
begin
Result.x:= ((B[1] * C[0]) - (B[0] * C[1])) div Det;
Result.y:= ((A[0] * C[1]) - (A[1] * C[0])) div Det;
end else
begin
Result.x:= -1;
Result.y:= -1;
end;
end;


begin
Pt:= Intersect(IntToBox(0, 0, 4, 4), IntToBox(4, 0, 0, 4));
Writeln('x:= ' + inttostr(Pt.x));
Writeln('y:= ' + inttostr(Pt.y));
end.


Wow, never thought of that idea

Bobarkinator

2008-05-02 20:18:17

Did the site I post help?

Special Ed

2008-05-02 20:24:18

From: Bobarkinator
Did the site I post help?


Yes, Yes it did… except they did it in C++ and had some stuff missing out. I had an idea of how to do it but this just helped me execute it.

Bobarkinator

2008-05-02 23:09:52

Ah, just wondering. Looks like you did a find job though.

Special Ed

2008-05-02 23:47:22

From: Bobarkinator
Ah, just wondering. Looks like you did a find job though.


Thank you! I wonder if anyone other than me and bullzeye will actually use it though?

bullzeye95

2008-05-03 00:42:16

From: Special Ed
Thank you! I wonder if anyone other than me and bullzeye will actually use it though?


It's always good to have lying around ;)

Sir R. M8gic1an

2008-05-03 04:35:24

what were you thinking of using it for when you made it? :p just cuious.

~RM

Gu3st...

2008-05-03 04:45:32

what does it do? :(

I feel left behind

Bobarkinator

2008-05-03 08:46:08

From: Zytex!
what does it do? :(

I feel left behind



Finds the point where 2 lines intersect.

Gu3st...

2008-05-03 14:12:06

*dictionary-time* Aha! thanks

kinda cool… :P

MylesMadness

2008-05-03 20:07:35

why do I think this is for msi…

Special Ed

2008-05-03 22:01:47

From: Mylesmadness
why do I think this is for msi…


Because I'm to un-motivated (Better word for lazy) to do any work that isn't for MSI right now :p

@Rasta - Me and bullzeye are using it for a walking system he thought of. This is needed to find the center of the road.

n3ss3s

2008-05-04 06:34:00

No difference but the name'll just suit better -



Type
TLine = Record
x1, y1, x2, y2: Integer;
end;


almost

2008-05-04 10:37:27

Here's the one i made for my 3DModel drawer thingy-
function Intersection(x1, y1, x2, y2, x3, y3, x4, y4: integer): TPoint;
var
Denom: integer;
begin
Denom:= ((y2 - y1) * (x3 - x4) - (y4 - y3) * (x1 - x2));
if Denom = 0 then
begin
Result.x:= 0;
Result.y:= 0;
Exit;
end;
Result.x:=((x1 - x2) * (x4 * y3 - x3 * y4) - (x3 - x4) * (x2 * y1 - x1 * y2)) / Denom;
Result.y:=((y4 - y3) * (x2 * y1 - x1 * y2) - (y2 - y1) * (x4 * y3 - x3 * y4)) / Denom;
end;

Special Ed

2008-05-04 12:56:38

Heh… nice almost. That's literally the exact same thing just without the in-betweens I had.

Oh well, I'll use your instead if you don't mind :p

almost

2008-05-04 19:20:55

Yup that's fine

The Claw

2008-05-05 06:02:01

I could of used this in my Maths Methods SAC today :) apart from the fact i can do it by hand, lol