3차원 벡터(또는 점)에 대한 구조체(structure)를 아래와 같이 작성할 수 있다.
<특이점>
1) 벡터의 성분은 X, Y, Z로 명백하므로 그냥 Public 변수로 설정함
2) 배열로 선언된 벡터 성분과도 호환되도록 함
- VB.NET의 Default Property를 이용함
- 사용예 :
[code lang-vb]Dim v1,v2 As Vector3D
Dim innerProduct As Single = v1(0)*v2(0) + v1(1)*v2(1) + v1(2)*v2(2)[/code]

구조체 소스코드
[code lang-vb]Public Structure Vector3D
    Public X, Y, Z As Single
    Public Shared ReadOnly ZeroVector As Vector3D = New Vector3D(0, 0, 0)

#Region "Constructors"
    'Private Sub New()
    'Nothing -> No default initialization
    'End Sub
    Public Sub New(ByVal x As Single, ByVal y As Single, ByVal z As Single)
        Me.X = x
        Me.Y = y
        Me.Z = z
    End Sub
    Public Sub New(ByVal v As Vector3D)
        Me.New(v.X, v.Y, v.Z) 'copy constructor
    End Sub
#End Region

#Region "Properties"
    ''' <summary>
    ''' 배열로 선언된 벡터 성분과 호환이 되도록 작성함
    ''' </summary>
    ''' <param name="index">성분의 위치를 지정하는 값 0:x, 1:y, 2:z</param>
    ''' <value>해당성분의 값</value>
    ''' <returns>해당성분의 값</returns>
    ''' <remarks>
    ''' 왜 호환되게 했나고요?
    ''' c/c++로 짠 코드를 변환하다 보면, 배열로 간단하게 해결한 것들이 많아서요.
    ''' </remarks>
    Default Public Property Elements(ByVal index As Short) As Single
        Get
            Dim i As Short = index Mod 3'실수를 방지하기 위해서
            Select Case i
                Case 0
                    Return X
                Case 1
                    Return Y
                Case 2
                    Return Z
            End Select
        End Get
        Set(ByVal value As Single)
            Dim i As Short = index Mod 3
            Select Case i
                Case 0
                    X = value
                Case 1
                    Y = value
                Case 2
                    Z = value
            End Select
        End Set
    End Property
#End Region
''각종 Property와 Operator 등은 생략 ^^
End Class[/code]

Posted by solarview

2008/06/21 04:48 2008/06/21 04:48
, , ,
Response
No Trackback , No Comment
RSS :
http://www.solarview.net/rss/response/174

Structure와 Class

선택사항을 저장하는 구조체를 만들었는데, 원본 구조체의 값이 변경되지 않았다. 구조체를 전달할 때, ByRef를 사용했음에도 불구하고 원본 값이 변경되지 않았다. 그래서 클래스로 변경하니 원본의 값이 변경되었다.
구조체를 사용해서 인자를 전달하면, 원본에는 아무런 변화가 없다.

Posted by solarview

2008/02/19 04:22 2008/02/19 04:22
, , , , ,
Response
No Trackback , No Comment
RSS :
http://www.solarview.net/rss/response/189