December 04, 2020, 11:42:41 AM

### Author Topic: [bb] Line Overlap Circle by nawi [ 1+ years ago ]  (Read 431 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bb] Line Overlap Circle by nawi [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : Line Overlap Circle
Author : nawi
Posted : 1+ years ago

Description : Check if line is overlapping circle. Code translated from google to BlitzBasic by Nawi and touched by Beaker.

Code :
Code: BlitzBasic
2.         ;'Calc Closest Point To circle center
3.         Local dx31#=cx#-x1#
4.         Local dx21#=x2#-x1#
5.         Local dy31#=cy#-y1#
6.         Local dy21#=y2#-y1#
7.         Local d#=((dx21#*dx21#)+(dy21#*dy21#))
8.         If d#<>0 Then d#=((dx31#*dx21#)+(dy31#*dy21#))/d#
9.         ;'Clip To the line segments legal bounds
10.         If d#<0.0 Then d#=0
11.         If d#>1.0 Then d#=1
12.         Local dx#=cx#-(x1#+(dx21#*d#))
13.         Local dy#=cy#-(y1#+(dy21#*d#))
15.                 ;'Line intersects circle
16.                 Return 1
17.         EndIf
18.         Return 0
19. End Function

Pongo(Posted 1+ years ago)

very slight optimization,... since you don't need to know the actual distance, just check it you can avoid using Sqrchange this line:If Radius# => Sqr((dx#*dx#)+(dy#*dy#))to this:If Radius# * Radius# => (dx#*dx#)+(dy#*dy#)in my quick tests it came out slightly faster, and I don't think I broke the function.

Ross C(Posted 1+ years ago)

Thank you for this!