elektronaut-vector2d 0.5.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/History.txt +14 -0
- data/MIT-LICENSE.txt +22 -0
- data/README.rdoc +32 -0
- data/lib/vector2d.rb +155 -0
- metadata +57 -0
data/History.txt
ADDED
data/MIT-LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2006-2009 Inge Jørgensen
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
4
|
+
obtaining a copy of this software and associated documentation
|
5
|
+
files (the "Software"), to deal in the Software without
|
6
|
+
restriction, including without limitation the rights to use,
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
copies of the Software, and to permit persons to whom the
|
9
|
+
Software is furnished to do so, subject to the following
|
10
|
+
conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
== Vector2d
|
2
|
+
|
3
|
+
Vector2d allows for easy handling of two-dimensionals coordinates and vectors.
|
4
|
+
It's very flexible, most methods accepts arguments as strings, arrays, hashes
|
5
|
+
or Vector2d objects.
|
6
|
+
|
7
|
+
== Licence
|
8
|
+
|
9
|
+
(The MIT License)
|
10
|
+
|
11
|
+
Copyright (c) 2006-2009 Inge Jørgensen
|
12
|
+
|
13
|
+
Permission is hereby granted, free of charge, to any person
|
14
|
+
obtaining a copy of this software and associated documentation
|
15
|
+
files (the "Software"), to deal in the Software without
|
16
|
+
restriction, including without limitation the rights to use,
|
17
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
18
|
+
copies of the Software, and to permit persons to whom the
|
19
|
+
Software is furnished to do so, subject to the following
|
20
|
+
conditions:
|
21
|
+
|
22
|
+
The above copyright notice and this permission notice shall be
|
23
|
+
included in all copies or substantial portions of the Software.
|
24
|
+
|
25
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
26
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
27
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
28
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
29
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
30
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
31
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
32
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/lib/vector2d.rb
ADDED
@@ -0,0 +1,155 @@
|
|
1
|
+
$:.unshift File.dirname(__FILE__)
|
2
|
+
|
3
|
+
# Vector2d allows for easy handling of two-dimensionals coordinates and vectors.
|
4
|
+
# It's very flexible, most methods accepts arguments as strings, arrays, hashes
|
5
|
+
# or Vector2d objects.
|
6
|
+
class Vector2d
|
7
|
+
|
8
|
+
VERSION_STRING = [0, 5, 1].join('.') #:nodoc:
|
9
|
+
|
10
|
+
# X axis
|
11
|
+
attr_accessor :x
|
12
|
+
# Y axis
|
13
|
+
attr_accessor :y
|
14
|
+
|
15
|
+
# Creates a new vector.
|
16
|
+
# The following examples are all valid:
|
17
|
+
#
|
18
|
+
# Vector2d.new(150, 100)
|
19
|
+
# Vector2d.new(150.0, 100.0)
|
20
|
+
# Vector2d.new("150x100")
|
21
|
+
# Vector2d.new("150.0x100.0")
|
22
|
+
# Vector2d.new([150,100})
|
23
|
+
# Vector2d.new({:x => 150, :y => 100})
|
24
|
+
# Vector2d.new({"x" => 150.0, "y" => 100.0})
|
25
|
+
# Vector2d.new(Vector2d.new(150, 100))
|
26
|
+
def initialize(*args)
|
27
|
+
args.flatten!
|
28
|
+
if (args.length == 1)
|
29
|
+
if (args[0].kind_of?(String) && args[0].match(/^[\s]*[\d\.]*[\s]*x[\s]*[\d\.]*[\s]*$/))
|
30
|
+
args = args[0].split("x")
|
31
|
+
elsif args[0].kind_of?(Vector2d)
|
32
|
+
args = [args[0].x, args[0].y]
|
33
|
+
elsif args[0].kind_of?(Hash)
|
34
|
+
args[0] = args[0][:x] if args[0][:x]
|
35
|
+
args[0] = args[0]["x"] if args[0]["x"]
|
36
|
+
args[1] = args[0][:y] if args[0][:y]
|
37
|
+
args[1] = args[0]["y"] if args[0]["y"]
|
38
|
+
else
|
39
|
+
args = [args[0], args[0]]
|
40
|
+
end
|
41
|
+
end
|
42
|
+
@x, @y = args[0].to_f, args[1].to_f
|
43
|
+
end
|
44
|
+
|
45
|
+
# Compares two vectors.
|
46
|
+
def ==(comp)
|
47
|
+
( comp.x == @x && comp.y == y )
|
48
|
+
end
|
49
|
+
|
50
|
+
# Returns a string representation of the vector.
|
51
|
+
#
|
52
|
+
# Example:
|
53
|
+
# Vector2d.new( 150, 100 ).to_s # "150x100"
|
54
|
+
def to_s
|
55
|
+
"#{@x}x#{@y}"
|
56
|
+
end
|
57
|
+
|
58
|
+
# Length of vector
|
59
|
+
def length
|
60
|
+
Math.sqrt((@x*@x)+(@y*@y))
|
61
|
+
end
|
62
|
+
|
63
|
+
# Set new length.
|
64
|
+
def length=(new_length)
|
65
|
+
v = self * (new_length/length)
|
66
|
+
@x, @y = v.x, v.y
|
67
|
+
end
|
68
|
+
|
69
|
+
# Returns a normalized (length = 1.0) version of the vector.
|
70
|
+
def normalize
|
71
|
+
self.dup.normalize!
|
72
|
+
end
|
73
|
+
|
74
|
+
# In-place form of Vector2d.normalize.
|
75
|
+
def normalize!
|
76
|
+
self.length = 1.0
|
77
|
+
self
|
78
|
+
end
|
79
|
+
|
80
|
+
# Rounds coordinates to nearest integer.
|
81
|
+
def round
|
82
|
+
self.dup.round!
|
83
|
+
end
|
84
|
+
|
85
|
+
# In-place form of Vector2d.round.
|
86
|
+
def round!
|
87
|
+
@x, @y = @x.round, @y.round
|
88
|
+
self
|
89
|
+
end
|
90
|
+
|
91
|
+
# Returns the aspect ratio of the vector.
|
92
|
+
def aspect_ratio
|
93
|
+
(@x/@y).abs
|
94
|
+
end
|
95
|
+
|
96
|
+
# Multiply vectors. If args is a single Numeric, both axis will be multiplied.
|
97
|
+
def *(*vector_or_number)
|
98
|
+
v = Vector2d::new(vector_or_number)
|
99
|
+
Vector2d.new(@x*v.x, @y*v.y)
|
100
|
+
end
|
101
|
+
|
102
|
+
# Divide vectors. If args is a single Numeric, both axis will be divided.
|
103
|
+
def /(*vector_or_number)
|
104
|
+
v = Vector2d::new(vector_or_number)
|
105
|
+
Vector2d.new(@x/v.x, @y/v.y)
|
106
|
+
end
|
107
|
+
|
108
|
+
# Add vectors. If args is a single Numeric, it will be added to both axis.
|
109
|
+
def +(*vector_or_number)
|
110
|
+
v = Vector2d::new(vector_or_number)
|
111
|
+
Vector2d.new(@x+v.x, @y+v.y)
|
112
|
+
end
|
113
|
+
|
114
|
+
# Subtract vectors. If args is a single Numeric, it will be subtracted from both axis.
|
115
|
+
def -(*vector_or_number)
|
116
|
+
v = Vector2d::new(vector_or_number)
|
117
|
+
Vector2d.new(@x-v.x, @y-v.y)
|
118
|
+
end
|
119
|
+
|
120
|
+
# Constrain/expand so that both coordinates fit within (the square implied by) another vector.
|
121
|
+
# This is useful for resizing images to fit a certain size while keeping aspect ratio.
|
122
|
+
#
|
123
|
+
# == Examples
|
124
|
+
#
|
125
|
+
# my_image = Vector2d.new("320x200") # Creates a new vector object
|
126
|
+
# my_image.constrain_both(100) # Returns a new vector: x=100, y=63
|
127
|
+
# my_image.constrain_both(150, 50) # Returns a new vector: x=80, y=50
|
128
|
+
# my_image.constrain_both("150x50") # Equal to constrain_both( 150, 50 )
|
129
|
+
# my_image.constrain_both("x100") # Returns a new vector: x=160, y=100
|
130
|
+
#
|
131
|
+
# == Note
|
132
|
+
#
|
133
|
+
# Either axis will be disregarded if zero or nil (see the last example). This is a feature, not a bug. ;)
|
134
|
+
def constrain_both(*vector_or_number)
|
135
|
+
scale = Vector2d::new(vector_or_number) / self
|
136
|
+
(self * ((scale.y==0||(scale.x>0&&scale.x<scale.y)) ? scale.x : scale.y))
|
137
|
+
end
|
138
|
+
|
139
|
+
# Constrain/expand so that one of the coordinates fit within (the square implied by) another vector.
|
140
|
+
#
|
141
|
+
# == Example
|
142
|
+
#
|
143
|
+
# my_image = Vector2d.new("320x200") # Creates a new vector object
|
144
|
+
# my_image.constrain_one(100, 100) # Returns a new vector: x=160, y=100
|
145
|
+
def constrain_one( *vector_or_number )
|
146
|
+
scale = Vector2d::new(vector_or_number) / self
|
147
|
+
if (scale.x > 0 && scale.y > 0)
|
148
|
+
scale = (scale.x<scale.y) ? scale.y : scale.x
|
149
|
+
self * scale
|
150
|
+
else
|
151
|
+
constrain_both(args)
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
end
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: elektronaut-vector2d
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- "Inge J\xC3\xB8rgensen"
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-20 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Vector2d allows for easy handling of two-dimensionals coordinates and vectors.
|
17
|
+
email: inge@melektronaut.no
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- History.txt
|
26
|
+
- README.rdoc
|
27
|
+
- MIT-LICENSE.txt
|
28
|
+
- lib/vector2d.rb
|
29
|
+
has_rdoc: true
|
30
|
+
homepage: http://github.com/elektronaut/Vector2d
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options:
|
33
|
+
- --inline-source
|
34
|
+
- --charset=UTF-8
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: "0"
|
42
|
+
version:
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
requirements: []
|
50
|
+
|
51
|
+
rubyforge_project: vector2d
|
52
|
+
rubygems_version: 1.2.0
|
53
|
+
signing_key:
|
54
|
+
specification_version: 2
|
55
|
+
summary: Vector2d allows for easy handling of two-dimensionals coordinates and vectors.
|
56
|
+
test_files: []
|
57
|
+
|