[VBA] vector operation

벡터 계산을 위한 구조체 선언

Option Explicit

Type Vector3D '3차원 벡터를 위한 자료 구조
  X As Double
  Y As Double
  Z As Double
End Type

내적 계산하기

Function DotProduct(v1 As Vector3D, v2 As Vector3D) As Double
DotProduct = v1.X * v2.X + v1.Y * v2.Y + v1.Z * v2.Z
End Function

벡터의 합

Function VectorAddition(v1 As Vector3D, v2 As Vector3D) As Vector3D
Dim c As Vector3D
'
c.X = v1.X + v2.X
c.Y = v1.Y + v2.Y
c.Z = v1.Z + v2.Z
'
VectorAddition = c
End Function

외적

Function CrossProduct(v1 As Vector3D, v2 As Vector3D) As Vector3D
Dim c As Vector3D
'
c.X = v1.Y * v2.Z - v1.Z * v2.Y
c.Y = v1.Z * v2.X - v1.X * v2.Z
c.Z = v1.X * v2.Y - v1.Y * v2.X
'
CrossProduct = c
End Function

벡터의 크기

Function Norm(v1 As Vector3D) As Double
Norm = Sqr(v1.X * v1.X + v1.Y * v1.Y + v1.Z * v1.Z)
End Function

벡터의 차를 위한 함수

Function MinusVector(v1 As Vector3D) As Vector3D
Dim r As Vector3D
'
r.X = -v1.X
r.Y = -v1.Y
r.Z = -v1.Z
'
MinusVector = r
End Function

벡터의 비교

Function Equals(v1 As Vector3D, v2 As Vector3D) As Boolean
If (v1.X = v2.X And v1.Y = v2.Y And v1.Z = v2.Z) Then
Equals = True
Else
Equals = False
End If
End Function
Print Friendly, PDF & Email