This example creates a vector class in which the vector can be stored as direction/magnitude, or as x/y. Internally, it is stored the same way so uses properties to convert between the two. This is handy as it allows you to change implementation behind the scenes without needing to change the interface as well.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
Namespace myapp
# Import "<std>"
Using std . .
Class Vector
Private
Field _x : Double
Field _y : Double
Public
Property x : Double ( )
Return _x
Setter ( n : Double )
_x = n
End
Property y : Double ( )
Return _y
Setter ( n : Double )
_y = n
end
Property direction : Double ( )
Return ATan2 ( _y , _x )
Setter ( n : Double )
Local magnitude : Double = Sqrt ( _x * _x + _y * _y )
_x = Cos ( n ) * magnitude
_y = Sin ( n ) * magnitude
End
Property magnitude : Double ( )
Return Sqrt ( _x * _x + _y * _y )
Setter ( n : Double )
Local direction : Double = ATan2 ( _y , _x )
_x = Cos ( direction ) * n
_y = Sin ( direction ) * n
End
Method normalize ( )
Self . magnitude = 1
End method
End
Function Main ( )
Local vector : = New Vector
vector . x = 10
vector . y = 10
Print "direction = " + vector . direction
Print "magnitude = " + vector . magnitude
vector . normalize ( )
Print "normalized direction = " + vector . direction
Print "normalized magnitude = " + vector . magnitude
Print "normalized x = " + vector . x
Print "normlaized y = " + vector . y
End