bivector 0.0.4 → 0.0.5
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/Rakefile +7 -7
- data/lib/bivector.rb +76 -76
- data/test/test_bivector.rb +63 -8
- metadata +2 -2
data/Rakefile
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
require 'rake/testtask'
|
2
|
-
|
3
|
-
Rake::TestTask.new do |t|
|
4
|
-
t.libs << 'test'
|
5
|
-
end
|
6
|
-
|
7
|
-
desc "Run tests"
|
1
|
+
require 'rake/testtask'
|
2
|
+
|
3
|
+
Rake::TestTask.new do |t|
|
4
|
+
t.libs << 'test'
|
5
|
+
end
|
6
|
+
|
7
|
+
desc "Run tests"
|
8
8
|
task :default => :test
|
data/lib/bivector.rb
CHANGED
@@ -1,76 +1,76 @@
|
|
1
|
-
class Bivector
|
2
|
-
attr_accessor :x, :y
|
3
|
-
|
4
|
-
def initialize(xCoordinate, yCoordinate)
|
5
|
-
@x, @y = xCoordinate, yCoordinate
|
6
|
-
end
|
7
|
-
|
8
|
-
def +(another)
|
9
|
-
if another.is_a? Bivector
|
10
|
-
Bivector.new(x + another.x, y + another.y)
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
def -(another)
|
15
|
-
if another.is_a? Bivector
|
16
|
-
Bivector.new(x - another.x, y - another.y)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def *(aNumber)
|
21
|
-
end
|
22
|
-
|
23
|
-
def norm
|
24
|
-
(x**2 + y**2)**0.5
|
25
|
-
end
|
26
|
-
alias :length :norm
|
27
|
-
|
28
|
-
def zero?
|
29
|
-
return (x == 0 && y ==0) ? true : false
|
30
|
-
end
|
31
|
-
alias :zero_vector? :zero?
|
32
|
-
alias :null_vector? :zero?
|
33
|
-
|
34
|
-
def dot_product(another)
|
35
|
-
if another.is_a? Bivector
|
36
|
-
x * another.x + y * another.y
|
37
|
-
end
|
38
|
-
end
|
39
|
-
alias :inner_product :dot_product
|
40
|
-
|
41
|
-
def cross_product(another)
|
42
|
-
if another.is_a? Bivector
|
43
|
-
Bivector.new()
|
44
|
-
end
|
45
|
-
end
|
46
|
-
alias :outer_product :cross_product
|
47
|
-
|
48
|
-
def unit_vector
|
49
|
-
return nil if self.zero?
|
50
|
-
Bivector.new(x / norm, y / norm)
|
51
|
-
end
|
52
|
-
|
53
|
-
def angles_to(another, style =
|
54
|
-
if another.is_a? Bivector
|
55
|
-
angle = Math.acos(dot_product(another) / ( norm * another.norm))
|
56
|
-
case style
|
57
|
-
when
|
58
|
-
return angle
|
59
|
-
when
|
60
|
-
return angle * 180 / Math::PI
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
def parallels_to?(another)
|
66
|
-
if another.is_a? Bivector
|
67
|
-
self.zero? || another.zero? || x * another.y - y * another.x == 0
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
def perpendicular_to?(another)
|
72
|
-
if another.is_a? Bivector
|
73
|
-
self.dot_product(another) == 0
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
1
|
+
class Bivector
|
2
|
+
attr_accessor :x, :y
|
3
|
+
|
4
|
+
def initialize(xCoordinate, yCoordinate)
|
5
|
+
@x, @y = xCoordinate, yCoordinate
|
6
|
+
end
|
7
|
+
|
8
|
+
def +(another)
|
9
|
+
if another.is_a? Bivector
|
10
|
+
Bivector.new(x + another.x, y + another.y)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def -(another)
|
15
|
+
if another.is_a? Bivector
|
16
|
+
Bivector.new(x - another.x, y - another.y)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def *(aNumber)
|
21
|
+
end
|
22
|
+
|
23
|
+
def norm
|
24
|
+
(x**2 + y**2)**0.5
|
25
|
+
end
|
26
|
+
alias :length :norm
|
27
|
+
|
28
|
+
def zero?
|
29
|
+
return (x == 0 && y ==0) ? true : false
|
30
|
+
end
|
31
|
+
alias :zero_vector? :zero?
|
32
|
+
alias :null_vector? :zero?
|
33
|
+
|
34
|
+
def dot_product(another)
|
35
|
+
if another.is_a? Bivector
|
36
|
+
x * another.x + y * another.y
|
37
|
+
end
|
38
|
+
end
|
39
|
+
alias :inner_product :dot_product
|
40
|
+
|
41
|
+
def cross_product(another)
|
42
|
+
if another.is_a? Bivector
|
43
|
+
Bivector.new()
|
44
|
+
end
|
45
|
+
end
|
46
|
+
alias :outer_product :cross_product
|
47
|
+
|
48
|
+
def unit_vector
|
49
|
+
return nil if self.zero?
|
50
|
+
Bivector.new(x / norm, y / norm)
|
51
|
+
end
|
52
|
+
|
53
|
+
def angles_to(another, style = :rad)
|
54
|
+
if another.is_a? Bivector
|
55
|
+
angle = Math.acos(dot_product(another) / ( norm * another.norm))
|
56
|
+
case style
|
57
|
+
when :rad
|
58
|
+
return angle
|
59
|
+
when :ang
|
60
|
+
return angle * 180 / Math::PI
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def parallels_to?(another)
|
66
|
+
if another.is_a? Bivector
|
67
|
+
self.zero? || another.zero? || x * another.y - y * another.x == 0
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def perpendicular_to?(another)
|
72
|
+
if another.is_a? Bivector
|
73
|
+
self.dot_product(another) == 0
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
data/test/test_bivector.rb
CHANGED
@@ -1,8 +1,63 @@
|
|
1
|
-
require 'test/unit'
|
2
|
-
require 'bivector'
|
3
|
-
|
4
|
-
class BivectorTest < Test::Unit::TestCase
|
5
|
-
def
|
6
|
-
|
7
|
-
|
8
|
-
|
1
|
+
require 'test/unit'
|
2
|
+
require 'bivector'
|
3
|
+
|
4
|
+
class BivectorTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
@v1 = Bivector.new(1, 0)
|
7
|
+
@v2 = Bivector.new(3, 3)
|
8
|
+
@v3 = Bivector.new(3, 4)
|
9
|
+
@v4 = Bivector.new(-1 ,1)
|
10
|
+
@v5 = Bivector.new(-1, -1)
|
11
|
+
@zero_vector = Bivector.new(0, 0)
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_plus
|
15
|
+
expect = Bivector.new(4, 4)
|
16
|
+
result = @v1 + @v3
|
17
|
+
assert_equal expect.x, result.x
|
18
|
+
assert_equal expect.y, result.y
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_minus
|
22
|
+
expect = Bivector.new(4, 5)
|
23
|
+
result = @v3 - @v5
|
24
|
+
assert_equal expect.x, result.x
|
25
|
+
assert_equal expect.y, result.y
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_mutiplied_by_a_number
|
29
|
+
expect = Bivector.new(1.5, 2)
|
30
|
+
result = @v3 * 0.5
|
31
|
+
flunk
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_norm
|
35
|
+
assert_equal 5, @v3.norm
|
36
|
+
assert_equal 5, @v3.length
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_is_zero_vector
|
40
|
+
assert @zero_vector.zero?
|
41
|
+
assert @zero_vector.null_vector?
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_dot_product
|
45
|
+
assert_equal 3, @v1.dot_product(@v2)
|
46
|
+
assert_equal -7, @v3.dot_product(@v5)
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_angles_calculation
|
50
|
+
assert_in_delta 45, @v1.angles_to(@v2, :ang), 0.0001
|
51
|
+
assert_in_delta 90, @v2.angles_to(@v4, :ang), 0.0001
|
52
|
+
assert_in_delta 135, @v1.angles_to(@v4, :ang), 0.0001
|
53
|
+
assert_in_delta 180, @v2.angles_to(@v5, :ang), 0.0001
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_parallel
|
57
|
+
assert @v2.parallels_to? @v5
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_perpendicular
|
61
|
+
assert @v2.perpendicular_to?(@v4)
|
62
|
+
end
|
63
|
+
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.0.5
|
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-19 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: A 2D Vector
|
15
15
|
email: zwt315@163.com
|