bivector 0.0.5 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +15 -0
- data/lib/bivector.rb +58 -7
- data/test/test_bivector.rb +9 -5
- metadata +3 -2
data/README.md
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# BiVector
|
2
|
+
|
3
|
+
## Decription
|
4
|
+
|
5
|
+
Sorry for that this is just a toy from which I learn how to make a ruby gem.
|
6
|
+
|
7
|
+
Here I provide a quite simple 2D vector class, and some methods to deal with the basic mathematical calculations between bivectors.
|
8
|
+
|
9
|
+
## Install
|
10
|
+
|
11
|
+
`gem install bivector`
|
12
|
+
|
13
|
+
## Test
|
14
|
+
|
15
|
+
To run the tests: `$ rake test`
|
data/lib/bivector.rb
CHANGED
@@ -4,6 +4,14 @@ class Bivector
|
|
4
4
|
def initialize(xCoordinate, yCoordinate)
|
5
5
|
@x, @y = xCoordinate, yCoordinate
|
6
6
|
end
|
7
|
+
|
8
|
+
def to_s
|
9
|
+
"(#{x}, #{y})"
|
10
|
+
end
|
11
|
+
|
12
|
+
def ==(another)
|
13
|
+
another.is_a?(Bivector) && x == another.x && y == another.y
|
14
|
+
end
|
7
15
|
|
8
16
|
def +(another)
|
9
17
|
if another.is_a? Bivector
|
@@ -18,6 +26,9 @@ class Bivector
|
|
18
26
|
end
|
19
27
|
|
20
28
|
def *(aNumber)
|
29
|
+
if aNumber.is_a? Numeric
|
30
|
+
Bivector.new(x * aNumber, y * aNumber)
|
31
|
+
end
|
21
32
|
end
|
22
33
|
|
23
34
|
def norm
|
@@ -52,13 +63,7 @@ class Bivector
|
|
52
63
|
|
53
64
|
def angles_to(another, style = :rad)
|
54
65
|
if another.is_a? Bivector
|
55
|
-
|
56
|
-
case style
|
57
|
-
when :rad
|
58
|
-
return angle
|
59
|
-
when :ang
|
60
|
-
return angle * 180 / Math::PI
|
61
|
-
end
|
66
|
+
BV::Angle.new(Math.acos(dot_product(another) / ( norm * another.norm))).values_in(style)
|
62
67
|
end
|
63
68
|
end
|
64
69
|
|
@@ -73,4 +78,50 @@ class Bivector
|
|
73
78
|
self.dot_product(another) == 0
|
74
79
|
end
|
75
80
|
end
|
81
|
+
|
82
|
+
def theta(style = :rad)
|
83
|
+
BV::Angle.new(Math.atan(y/x)).values_in(style)
|
84
|
+
end
|
85
|
+
|
86
|
+
def polar
|
87
|
+
"(#{norm}, #{theta})"
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
module BV
|
92
|
+
class Angle
|
93
|
+
attr :value, :model
|
94
|
+
|
95
|
+
def initialize(value, model = :rad)
|
96
|
+
@value = value
|
97
|
+
@model = model
|
98
|
+
end
|
99
|
+
|
100
|
+
def to_s
|
101
|
+
value
|
102
|
+
end
|
103
|
+
|
104
|
+
def switch_model
|
105
|
+
case model
|
106
|
+
when :ang then rad_value
|
107
|
+
when :rad then ang_value
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def rad_value
|
112
|
+
model == :rad ? value : value * Math::PI / 180
|
113
|
+
end
|
114
|
+
|
115
|
+
def ang_value
|
116
|
+
model == :ang ? value : value * 180 / Math::PI
|
117
|
+
end
|
118
|
+
|
119
|
+
def values_in(style = :rad)
|
120
|
+
style == model ? value : switch_model
|
121
|
+
end
|
122
|
+
|
123
|
+
def tan
|
124
|
+
Math.tan(self.rad_value)
|
125
|
+
end
|
126
|
+
end
|
76
127
|
end
|
data/test/test_bivector.rb
CHANGED
@@ -14,21 +14,19 @@ class BivectorTest < Test::Unit::TestCase
|
|
14
14
|
def test_plus
|
15
15
|
expect = Bivector.new(4, 4)
|
16
16
|
result = @v1 + @v3
|
17
|
-
|
18
|
-
assert_equal expect.y, result.y
|
17
|
+
assert(expect == result)
|
19
18
|
end
|
20
19
|
|
21
20
|
def test_minus
|
22
21
|
expect = Bivector.new(4, 5)
|
23
22
|
result = @v3 - @v5
|
24
|
-
|
25
|
-
assert_equal expect.y, result.y
|
23
|
+
assert(expect == result)
|
26
24
|
end
|
27
25
|
|
28
26
|
def test_mutiplied_by_a_number
|
29
27
|
expect = Bivector.new(1.5, 2)
|
30
28
|
result = @v3 * 0.5
|
31
|
-
|
29
|
+
assert(expect == result)
|
32
30
|
end
|
33
31
|
|
34
32
|
def test_norm
|
@@ -60,4 +58,10 @@ class BivectorTest < Test::Unit::TestCase
|
|
60
58
|
def test_perpendicular
|
61
59
|
assert @v2.perpendicular_to?(@v4)
|
62
60
|
end
|
61
|
+
|
62
|
+
def test_theta
|
63
|
+
assert_in_delta 0, @v1.theta, 0.0001
|
64
|
+
assert_in_delta 45, @v2.theta(:ang), 0.0001
|
65
|
+
assert_in_delta 225, @v5.theta(:ang), 0.0001
|
66
|
+
end
|
63
67
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bivector
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-02-
|
12
|
+
date: 2013-02-22 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: A 2D Vector
|
15
15
|
email: zwt315@163.com
|
@@ -20,6 +20,7 @@ files:
|
|
20
20
|
- lib/bivector.rb
|
21
21
|
- Rakefile
|
22
22
|
- test/test_bivector.rb
|
23
|
+
- README.md
|
23
24
|
homepage: https://github.com/hegwin/bivector
|
24
25
|
licenses: []
|
25
26
|
post_install_message:
|