Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
#Compile Exe
#Dim All
#Include "Win32API.inc"
%IDC_Button = 500
Global hDlg As Dword
Function PBMain() As Long
Dialog New Pixels, 0, "PowerBASIC",300,300,200,200, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, %IDC_Button,"Push", 50,10,100,20
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Select Case Cb.Msg
Case %WM_Command
Select Case Cb.Ctl
Case %IDC_Button
Local RLine, RRect As Rect
'Rect enclosing LINE
RLine.nLeft = 0 : RLine.nTop = 7
RLine.nRight = 9 : RLine.nBottom = -5
'Rect to check for intersection with LINE
RRect.nLeft = 0 : RRect.nTop = 5
RRect.nRight = 5 : RRect.nBottom = 0
? IIf$(LineIntersectsRect(RLine,RRect), "Intersects", "Does not Intersect")
End Select
End Select
End Function
Function LineIntersectsRect(RLine As Rect, RRect As Rect) As Long
Local d1,d2,d3,d4,a,b As Long
a = RLine.nRight - RLine.nLeft : b = RLine.nBottom - RLine.nTop
Function = 1
d1 = a*(RRect.nTop - RLine.nTop) - b*(RRect.nLeft - RLine.nLeft)
d2 = a*(RRect.nBottom - RLine.nTop) - b*(RRect.nLeft - RLine.nLeft)
d3 = a*(RRect.nBottom - RLine.nTop) - b*(RRect.nRight - RLine.nLeft)
d4 = a*(RRect.nTop - RLine.nTop) - b*(RRect.nRight - RLine.nLeft)
If d1=0 Or d2=0 Or d3=0 Or d4=0 Then Exit Function
If (Sgn(d1) = Sgn(d2)) And (Sgn(d1) = Sgn(d3)) And (Sgn(d1) = Sgn(d4)) Then Function = 0
End Function
'gbs_01338
'Date: 05-11-2013
http://www.garybeene.com/sw/gbsnippets.htm