Got part way to what I think you're after and ran out of time. This will find where a ray hits a box. You just need to go through the hits and find the one closest to the origin of the ray. use mouse and left mouse b to try it..
Global width=800,height=600 boxx1=100 boxy1=100 boxx2=200 boxy2=200 Type hitinfo Field x#,y#,side$ End Type Type rayinfo Field x1#,y1#,anglefromhoriz# End Type Type boxinfo Field tlx#,tly#,brx#,bry# End Type box1.boxinfo=New boxinfo ray1.rayinfo=New rayinfo Graphics 800,600,16,0 While Not KeyDown(1) Cls If MouseHit(1) ray1\x1=Rand(0.,800.) ray1\y1=Rand(0.,600.) ray1\anglefromhoriz=Rand(0,360) EndIf box1\tlx=MouseX() box1\tly=MouseY() box1\brx=box1\tlx+100 box1\bry=box1\tly+200 Color 0,150,150 Rect box1\tlx,box1\tly,box1\brx-box1\tlx,box1\bry-box1\tly,1 Color 0,255,0 Line ray1\x1,ray1\y1,ray1\x1+1000.*Cos(ray1\anglefromhoriz),ray1\y1-1000.*Sin(ray1\anglefromhoriz) hit1.hitinfo=wherehit(ray1,box1) Flip Wend Function wherehit.hitinfo(ray.rayinfo,box.boxinfo) hit.hitinfo=New hitinfo Text 20,20,ray\anglefromhoriz ;equation of line. Grad=sin (anglefromhoriz) ; y=sin(anglefromhoriz) x + y1 - sin (anglefromhoriz) x1 m#=-Tan(ray\anglefromhoriz) c#=ray\y1+Tan(ray\anglefromhoriz)*ray\x1 ;y pos of hit on left of box at x=tlx... yhitleft#=m*box\tlx+c ;Does this y pos lie within the y extent of box? hitleft=(yhitleft>=box\tly) And (yhitleft<=box\bry) yhitright#=m*box\brx+c hitright=(yhitright>=box\tly) And (yhitright<=box\bry) ; x= (y-c)/m xhittop#=(box\tly-c)/m hittop=(xhittop>=box\tlx) And (xhittop<=box\brx) xhitbottom#=(box\bry-c)/m hitbottom=(xhitbottom>=box\tlx) And (xhitbottom<=box\brx) Color Rand(100,255),Rand(100.,255),0 If hittop Then Rect xhittop-1,box\tly-1,3,3 If hitbottom Then Rect xhitbottom-1,box\bry-1,3,3 If hitleft Then Rect box\tlx-1,yhitleft-1,3,3 If hitright Then Rect box\brx-1,yhitright-1,3,3 If Not(hittop Or hitbottom Or hitleft Or hitright) hit\side$="None" EndIf Return hit End Function