determine line intersecting with rect?

Blitz3D Forums/Blitz3D Programming/determine line intersecting with rect?

mrtricks(Posted 2004) [#1]
Hi, I know this is Blitz 3D specific, but I couldn't see a 2D specific forum.

I have X & Y co-ords for each end of a line; an X & Y coord and a Height & Width value for a rectangle. I want to draw the line, but only the portion that is inside the rect (if any). So I need two new XY coords for the line.

I could do loads of nested If... Thens checking each point against all others but I'm sure there must be a quite simple formula/equation to determine the two new endpoints quite neatly. But I can't think of it. Anyone know one offhand?


DJWoodgate(Posted 2004) [#2]
I'm sure there was a line routine on the code archives that clips to the screen boundary, by Skidracer I think, which may be helpful. From memory though it may be embedded in another bit of code by Blitz Support.

Here... http://www.blitzbasic.co.nz/codearcs/codearcs.php?code=380

Edit. You could of course just use a viewport but if you need the new endpoints then an adaptation of this should be able to provide them.


mrtricks(Posted 2004) [#3]
That example is a little different to what I was after but thanks anyway. I've cobbled something together okay in the meantime.