본문 바로가기

Window Programming/VB

사각형이나 원의 충돌을 감지하는 모듈

 01 : '--------------------------------------------------------------------------
02 : '제작 : 백종민 zhsptm@paran.com
03 : '설명 : 충돌 관련 모듈
04 : '함수 : SquareHit(사각형 충돌) CircleHit(원 충돌) CircleAngle(원 충돌 각도)
05 : '--------------------------------------------------------------------------
06 : Option Explicit
07 : Private Declare Function IntersectRect Lib "user32.dll" (ByRef lpDestRect As RECT, ByRef lpSrc1Rect As RECT, ByRef lpSrc2Rect As RECT) As Long
08 : Private Declare Function IsRectEmpty Lib "user32.dll" (ByRef lpRect As RECT) As Long
09 :
10 : Private Type RECT
11 :      Left As Long
12 :      Top As Long
13 :      Right As Long
14 :      Bottom As Long
15 : End Type
16 : Private Type CircleSenterType
17 :      X As Long
18 :      Y As Long
19 : End Type
20 : Public Const PI = 3.1415926535
21 :
22 : Public Function SquareHit(ByVal Left1 As Long, ByVal Top1 As Long, ByVal Width1 As Long, ByVal Height1 As Long, ByVal Left2 As Long, ByVal Top2 As Long, ByVal width2 As Long, ByVal height2 As Long) As Boolean
23 :      Dim Rect1 As RECT, Rect2 As RECT, Intersected As RECT
24 :      With Rect1
25 :           .Left = Left1
26 :           .Top = Top1
27 :           .Right = Left1 + Width1
28 :           .Bottom = Top1 + Height1
29 :      End With
30 :      With Rect2
31 :           .Left = Left2
32 :           .Top = Top2
33 :           .Right = Left2 + width2
34 :           .Bottom = Top2 + height2
35 :      End With
36 :      Call IntersectRect(Intersected, Rect1, Rect2)
37 :      SquareHit = Not CBool(IsRectEmpty(Intersected))
38 : End Function
39 :
40 : Public Function CircleHit(ByVal Left1 As Long, ByVal Top1 As Long, ByVal Width1 As Long, ByVal Left2 As Long, ByVal Top2 As Long, ByVal width2 As Long) As Boolean
41 :      Dim Distance1 As Double
42 :      Dim Distance2 As Double
43 :      Dim Center1 As CircleSenterType
44 :      Dim Center2 As CircleSenterType
45 :     
46 :      Center1.X = Left1 + Width1 / 2
47 :      Center1.Y = Top1 + Width1 / 2
48 :      Center2.X = Left2 + width2 / 2
49 :      Center2.Y = Top2 + width2 / 2
50 :     
51 :      Distance1 = Abs(Center1.X - Center2.X) ^ 2 + Abs(Center1.Y - Center2.Y) ^ 2
52 :     
53 :      Distance1 = Math.Sqr(Distance1)
54 :      Distance2 = Width1 / 2 + width2 / 2
55 :      CircleHit = False
56 :      If Distance1 <= Distance2 Then CircleHit = True
57 : End Function
58 :
59 : Private Function ACos(ByRef Rate As Double) As Double
60 :      If Abs(Rate) > 1 Then Exit Function
61 :     
62 :      If Rate = 0 Then
63 :           ACos = Atn(1) * 2
64 :      Else
65 :           ACos = Atn(Sqr(1 - Rate ^ 2) / Rate)
66 :           If Rate < 0 Then ACos = ACos + Atn(1) * 4
67 :      End If
68 : End Function
69 :
70 : Public Function CircleAngle(ByRef X1 As Double, ByRef Y1 As Double, ByRef X2 As Double, _
71 : ByRef Y2 As Double, ByRef Rid As Boolean) As Double
72 :
73 :      Dim XLen As Double, YLen As Double, Hypotenuse As Double
74 :      XLen = Abs(X1 - X2): YLen = Abs(Y1 - Y2)
75 :      Hypotenuse = Sqr(XLen ^ 2 + YLen ^ 2)
76 :      CircleAngle = ACos(XLen / Hypotenuse)
77 :     
78 :      If Rid = True Then CircleAngle = CircleAngle * 180 / PI
79 : End Function
80 :

SquareHit(X1, Y1, 가로1, 세로1, X2, Y2, 가로2, 세로2)
CircleHit(X1, Y1, 지름1, X2, Y2, 지름2)
CircleAngle(X1, Y1, X2, Y2, 호도각으로 반환?)

P.S. 호도각-원 한바퀴를 360으로, 라디안-반지름이 1인 원에서 호의 길이가 1인 부채꼴의 각이 기본단위