uzi-vector2 0.0.1
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/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Rakefile +2 -0
- data/lib/uzi-vector2.rb +80 -0
- data/lib/uzi-vector2/version.rb +5 -0
- data/spec/vector2_spec.rb +124 -0
- data/uzi-vector2.gemspec +23 -0
- metadata +89 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
data/lib/uzi-vector2.rb
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
module Uzi
|
2
|
+
class Vector2
|
3
|
+
attr_accessor :x, :y
|
4
|
+
|
5
|
+
def initialize(x=0.0, y=0.0)
|
6
|
+
@x, @y = x.to_f, y.to_f
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
def -(v2)
|
12
|
+
Vector2.new( @x - v2.x, @y - v2.y )
|
13
|
+
end
|
14
|
+
|
15
|
+
def +(v2)
|
16
|
+
Vector2.new( @x + v2.x, @y + v2.y )
|
17
|
+
end
|
18
|
+
|
19
|
+
def *(scalar)
|
20
|
+
Vector2.new( @x * scalar, @y * scalar )
|
21
|
+
end
|
22
|
+
|
23
|
+
def /(scalar)
|
24
|
+
Vector2.new( @x / scalar, @y / scalar )
|
25
|
+
end
|
26
|
+
|
27
|
+
def -@
|
28
|
+
Vector2.new( -@x, -@y )
|
29
|
+
end
|
30
|
+
|
31
|
+
def ==(v2)
|
32
|
+
@x == v2.x && @y == v2.y
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
def length
|
38
|
+
Math.sqrt( @x**2 + @y**2 )
|
39
|
+
end
|
40
|
+
|
41
|
+
def dot(v2)
|
42
|
+
@x * v2.x + @y * v2.y
|
43
|
+
end
|
44
|
+
|
45
|
+
def normalize
|
46
|
+
len = length
|
47
|
+
|
48
|
+
Vector2.new( @x / len, @y / len )
|
49
|
+
end
|
50
|
+
|
51
|
+
def normalize!
|
52
|
+
len = length
|
53
|
+
|
54
|
+
@x /= len
|
55
|
+
@y /= len
|
56
|
+
|
57
|
+
return len
|
58
|
+
end
|
59
|
+
|
60
|
+
def perp
|
61
|
+
Vector2.new( @y, -@x )
|
62
|
+
end
|
63
|
+
|
64
|
+
def rotate(theta)
|
65
|
+
theta *= Math::PI / 180.0
|
66
|
+
|
67
|
+
tx = (@x * Math.cos(theta)) - (@y * Math.sin(theta))
|
68
|
+
ty = (@x * Math.sin(theta)) + (@y * Math.cos(theta))
|
69
|
+
|
70
|
+
Vector2.new(tx, ty)
|
71
|
+
end
|
72
|
+
|
73
|
+
def rotate!(theta)
|
74
|
+
v2 = self.rotate(theta)
|
75
|
+
@x, @y = v2.x, v2.y
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
Vec2 = Vector2
|
80
|
+
end
|
@@ -0,0 +1,124 @@
|
|
1
|
+
require 'uzi-vector2'
|
2
|
+
include Uzi
|
3
|
+
|
4
|
+
describe Vector2 do
|
5
|
+
before :each do
|
6
|
+
@v1 = Vec2.new(10, 10)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should have a shortened name of Vec2" do
|
10
|
+
Vec2.should eql(Vector2)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should have a default value of 0, 0" do
|
14
|
+
v = Vec2.new
|
15
|
+
|
16
|
+
v.x.should == 0
|
17
|
+
v.y.should == 0
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should be constructable" do
|
21
|
+
v = Vec2.new(10.0, 10.0)
|
22
|
+
|
23
|
+
v.x.should == 10.0
|
24
|
+
v.y.should == 10.0
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should initialize with floats when given integers" do
|
28
|
+
v = Vec2.new(10, 10)
|
29
|
+
|
30
|
+
v.x.should be_a(Float)
|
31
|
+
v.y.should be_a(Float)
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
it "should be able to be added to another vector" do
|
36
|
+
v2 = Vec2.new(5, 5)
|
37
|
+
v3 = @v1 + v2
|
38
|
+
|
39
|
+
v3.should be_a(Vec2)
|
40
|
+
v3.should == Vec2.new(15, 15)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should be able to be subtracted" do
|
44
|
+
v2 = Vec2.new(5, 5)
|
45
|
+
v3 = @v1 - v2
|
46
|
+
|
47
|
+
v3.should be_a(Vec2)
|
48
|
+
v3.should == Vec2.new(5, 5)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should be able to be multipled by a scalar" do
|
52
|
+
v2 = @v1 * 2
|
53
|
+
|
54
|
+
v2.should be_a(Vec2)
|
55
|
+
v2.should == Vec2.new(20, 20)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should be able to be divided by a scalar" do
|
59
|
+
v2 = @v1 / 2
|
60
|
+
|
61
|
+
v2.should be_a(Vec2)
|
62
|
+
v2.should == Vec2.new(5, 5)
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should be able to be negated" do
|
66
|
+
v2 = -@v1
|
67
|
+
|
68
|
+
v2.should be_a(Vec2)
|
69
|
+
v2.should == Vec2.new(-10, -10)
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should return a correct length" do
|
73
|
+
@v1.length.should be_close(14.1421, 0.0001)
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should be able to calculate a dot product" do
|
77
|
+
v1 = Vec2.new(1, 0)
|
78
|
+
v2 = Vec2.new(0, 1)
|
79
|
+
dot = v1.normalize.dot(v2)
|
80
|
+
|
81
|
+
dot.should be_a(Numeric)
|
82
|
+
dot.should == 0
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should be able to normalize" do
|
86
|
+
ref = Vec2.new(0, 1)
|
87
|
+
|
88
|
+
v2 = @v1.normalize
|
89
|
+
|
90
|
+
v2.should be_a(Vec2)
|
91
|
+
v2.length.should be_close(1.0, 0.001)
|
92
|
+
ref.normalize.dot(@v1.normalize).should be_close(ref.dot(v2), 0.001)
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should be able to normalize!" do
|
96
|
+
ref = Vec2.new(0, 1)
|
97
|
+
v2 = @v1.dup
|
98
|
+
|
99
|
+
@v1.normalize!
|
100
|
+
@v1.length.should be_close(1.0, 0.001)
|
101
|
+
ref.dot(@v1).should be_close(ref.dot(v2.normalize), 0.001)
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should be able to return a perpendicular vector" do
|
105
|
+
v2 = @v1.perp
|
106
|
+
|
107
|
+
v2.should be_a(Vec2)
|
108
|
+
@v1.normalize.dot( v2.normalize ).should be_close(0.0, 0.001)
|
109
|
+
end
|
110
|
+
|
111
|
+
it "should be able to rotate by a number of degrees" do
|
112
|
+
v2 = @v1.rotate(90.0)
|
113
|
+
|
114
|
+
v2.should be_a(Vec2)
|
115
|
+
@v1.normalize.dot( v2.normalize ).should be_close(0.0, 0.001)
|
116
|
+
end
|
117
|
+
|
118
|
+
it "should be able to rotate! by a number of degrees" do
|
119
|
+
v2 = @v1.dup
|
120
|
+
@v1.rotate!(90.0)
|
121
|
+
|
122
|
+
@v1.normalize.dot( v2.normalize ).should be_close(0.0, 0.001)
|
123
|
+
end
|
124
|
+
end
|
data/uzi-vector2.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "uzi-vector2/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "uzi-vector2"
|
7
|
+
s.version = Uzi::Vector2::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Michael Morin"]
|
10
|
+
s.email = ["michael.c.morin@gmail.com"]
|
11
|
+
s.homepage = "http://rubygems.org/gems/uzi-vector2"
|
12
|
+
s.summary = %q{A simple 2D vector class}
|
13
|
+
s.description = %q{A simple 2D vector class}
|
14
|
+
|
15
|
+
#s.rubyforge_project = "uzi-vector2"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
s.add_development_dependency 'rspec', '~> 2.0.0'
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: uzi-vector2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Michael Morin
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-10-14 00:00:00 -04:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rspec
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 15
|
30
|
+
segments:
|
31
|
+
- 2
|
32
|
+
- 0
|
33
|
+
- 0
|
34
|
+
version: 2.0.0
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
description: A simple 2D vector class
|
38
|
+
email:
|
39
|
+
- michael.c.morin@gmail.com
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files: []
|
45
|
+
|
46
|
+
files:
|
47
|
+
- .gitignore
|
48
|
+
- Gemfile
|
49
|
+
- Rakefile
|
50
|
+
- lib/uzi-vector2.rb
|
51
|
+
- lib/uzi-vector2/version.rb
|
52
|
+
- spec/vector2_spec.rb
|
53
|
+
- uzi-vector2.gemspec
|
54
|
+
has_rdoc: true
|
55
|
+
homepage: http://rubygems.org/gems/uzi-vector2
|
56
|
+
licenses: []
|
57
|
+
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
hash: 3
|
69
|
+
segments:
|
70
|
+
- 0
|
71
|
+
version: "0"
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
hash: 3
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
version: "0"
|
81
|
+
requirements: []
|
82
|
+
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 1.3.7
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: A simple 2D vector class
|
88
|
+
test_files:
|
89
|
+
- spec/vector2_spec.rb
|