January 26, 2021, 12:02:29 PM

### Author Topic: [bmx] Intersection of line and rectangle by Warpy [ 1+ years ago ]  (Read 611 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bmx] Intersection of line and rectangle by Warpy [ 1+ years ago ]
« on: June 29, 2017, 12:28:38 AM »
Title : Intersection of line and rectangle
Author : Warpy
Posted : 1+ years ago

Description : Following from <a href="../Community/postse94e-2.html?topic=83890" target="_blank">this post[/url], here's how to find the point of intersection of a line and a rectangle.

It's sort of assumed that the end point of the line is inside the box, and the start point is outside the box. A little test beforehand can make sure of that.

Code :
Code: BlitzMax
1. Function boxintersect(last_x#, last_y#, x#, y#, bx1#, by1#, bx2#, by2#, ix# Var, iy# Var)
2.         'finds point of intersection of line from (last_x,last_y) to (x,y)
3.         'with the box (bx1,by1,bx2,by2). the ix and iy parameters are var pointers, which means
4.         'the function fills in the values of the point of intersection in the variables that you give.
5.         'the function also returns true if there is an intersection, and false if there is none.
6.
7.         If Not (x>=bx1 And x<=bx2 And y>=by1 And y<=by2) Return False
8.
9.         If last_x < bx1 And x >= bx1            'does it cross left edge?
10.                 iy# = last_y + (y - last_y) * (bx1-last_x)/(x-last_x)
11.                 If iy>=by1 And iy<=by2                  'is intersection point on left edge?
12.                         ix# = bx1
13.                         Return True
14.                 EndIf
15.         ElseIf last_x > bx2 And x <= bx2        'does it cross right edge?
16.                 iy# = last_y + (y - last_y) * (bx2 - last_x)/(x - last_x)
17.                 If iy>=by1 And iy<=by2                  'is intersection point on right edge?
18.                         ix# = bx2
19.                         Return True
20.                 EndIf
21.         EndIf
22.
23.         If last_y < by1 And y >= by1            'does it cross top edge?
24.                 ix# = last_x + (x - last_x) * (by1 - last_y)/(y - last_y)
25.                 If ix>=bx1 And ix<=bx2                  'is intersection point on top edge?
26.                         iy# = by1
27.                         Return True
28.                 EndIf
29.         ElseIf last_y > by2 And y <= by2        'does it cross bottom edge?
30.                 ix# = last_x + (x - last_x) * (by2 - last_y)/(y - last_y)
31.                 If ix>=bx1 And ix<=bx2                  'is intersection point on bottom edge?
32.                         iy# = by2
33.                         Return True
34.                 EndIf
35.         EndIf
36. End Function
37.
38.
39.
40.
41.
42. 'example
43. Graphics 800,600,0
44.
45. Global box1#,boy1#,box2#,boy2#
46. Global ox#,oy#,nx#,ny#
47.
48. Function maketest()
49.         box1=Rnd(200,300)
50.         boy1=Rnd(100,200)
51.         box2=Rnd(500,600)
52.         boy2=Rnd(400,500)
53.
54.         nx=Rnd(box1,box2)
55.         ny=Rnd(boy1,boy2)
56.         an#=Rnd(360)
57.         ox=300*Cos(an)+400
58.         oy=300*Sin(an)+300
59.
60. End Function
61.
62. maketest
63.
64. While Not (KeyHit(KEY_ESCAPE) Or AppTerminate())
65.
66.         If KeyHit(KEY_SPACE)
67.                 'make a new box and line
68.                 maketest
69.         EndIf
70.
71.         DrawLine box1,boy1,box2,boy1
72.         DrawLine box1,boy1,box1,boy2
73.         DrawLine box1,boy2,box2,boy2
74.         DrawLine box2,boy1,box2,boy2
75.
76.         DrawLine ox,oy,nx,ny
77.
78.         Local ix#,iy#   'variables to store point of intersection in
79.         boxintersect( ox,oy,nx,ny, box1,boy1,box2,boy2, ix,iy )
80.
81.         DrawOval ix-3,iy-3,6,6
82.
83.
84.         Flip
85.         Cls
86. Wend