Class: Ray::Vector2
- Inherits:
-
Object
- Object
- Ray::Vector2
- Defined in:
- ext/vector.c,
lib/ray/vector.rb,
ext/vector.c
Overview
This class can be used to represent either a 2D point (x, y) or a size WxH (hence the aliased methods).
Instance Method Summary collapse
- #*(other) ⇒ Object
-
#+(other) ⇒ Ray::Vector2
(x + other.x, y + other.y).
-
#+@ ⇒ Ray::Vector2
(x, y).
-
#-(other) ⇒ Ray::Vector2
(x - other.x, y - other.y).
-
#-@ ⇒ Ray::Vector2
(-x, -y).
- #/(other) ⇒ Object
- #==(other) ⇒ Object
-
#dist(other) ⇒ Float
(also: #distance)
The distance between two vectors.
-
#dot(vector) ⇒ Float
A.x * b.x + a.y * b.y.
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
- #initialize(x = 0.0, y = 0.0) ⇒ Object constructor
- #initialize_copy(other) ⇒ Object
-
#inside?(rect) ⇒ true, false
True if the receive is contained in the rect.
-
#length ⇒ Float
(also: #norm)
The length of the vector.
-
#normalize ⇒ Ray::Vector2
Normalized vector (i.e. length will be 1).
-
#normalize! ⇒ Object
Normalizes the vector, by dividing it by its length.
-
#outside?(rect) ⇒ Boolean
Opposite of #inside?.
- #pretty_print(q) ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object
- #to_vector2 ⇒ Object
-
#x ⇒ Float
(also: #w)
The x position of the vector.
-
#x=(x) ⇒ void
(also: #w=, #width=)
Sets the x position of the vector.
-
#y ⇒ Float
(also: #h)
The y position of the vector.
-
#y=(y) ⇒ void
(also: #h=, #height=)
Sets the y position of the vector.
Constructor Details
#initialize(x = 0.0, y = 0.0) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 |
# File 'ext/vector.c', line 73
static
VALUE ray_vector2_init(int argc, VALUE *argv, VALUE self) {
VALUE rb_x = Qnil, rb_y = Qnil;
rb_scan_args(argc, argv, "02", &rb_x, &rb_y);
say_vector2 *vector = ray_rb2vector2_ptr(self);
if (!NIL_P(rb_x)) vector->x = NUM2DBL(rb_x);
if (!NIL_P(rb_y)) vector->y = NUM2DBL(rb_y);
return self;
}
|
Instance Method Details
#*(float) ⇒ Ray::Vector2 #*(vector) ⇒ Ray::Vector2
38 39 40 41 42 43 44 45 |
# File 'lib/ray/vector.rb', line 38 def *(other) if other.respond_to? :to_vector2 other = other.to_vector2 Ray::Vector2[x * other.x, y * other.y] else Ray::Vector2[x * other, y * other] end end |
#+(other) ⇒ Ray::Vector2
Returns (x + other.x, y + other.y).
19 20 21 22 |
# File 'lib/ray/vector.rb', line 19 def +(other) other = other.to_vector2 Ray::Vector2[x + other.x, y + other.y] end |
#-(other) ⇒ Ray::Vector2
Returns (x - other.x, y - other.y).
26 27 28 29 |
# File 'lib/ray/vector.rb', line 26 def -(other) other = other.to_vector2 Ray::Vector2[x - other.x, y - other.y] end |
#-@ ⇒ Ray::Vector2
Returns (-x, -y).
72 |
# File 'lib/ray/vector.rb', line 72 def -@; Ray::Vector2[-x, -y] end |
#/(float) ⇒ Ray::Vector2 #/(vector) ⇒ Ray::Vector2
54 55 56 57 58 59 60 61 |
# File 'lib/ray/vector.rb', line 54 def /(other) if other.respond_to? :to_vector2 other = other.to_vector2 Ray::Vector2[x / other.x, y / other.y] else Ray::Vector2[x / other, y / other] end end |
#==(other) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/ray/vector.rb', line 106 def ==(other) if other.is_a? Vector2 x == other.x && y == other.y elsif other.is_a? Array if other.size <= 2 other = other.to_vector2 x == other.x && y == other.y else false end elsif other.respond_to? :to_vector2 other = other.to_vector2 x == other.x && y == other.y else false end end |
#dist(other) ⇒ Float Also known as: distance
Returns The distance between two vectors.
101 102 103 |
# File 'lib/ray/vector.rb', line 101 def dist(other) (self - other).length end |
#dot(vector) ⇒ Float
Returns a.x * b.x + a.y * b.y.
66 67 68 69 |
# File 'lib/ray/vector.rb', line 66 def dot(vector) vector = vector.to_vector2 x * vector.x + y * vector.y end |
#eql?(other) ⇒ Boolean
124 125 126 |
# File 'lib/ray/vector.rb', line 124 def eql?(other) self.class == other.class && self == other end |
#hash ⇒ Object
128 129 130 |
# File 'lib/ray/vector.rb', line 128 def hash to_a.hash end |
#initialize_copy(other) ⇒ Object
85 86 87 88 89 |
# File 'ext/vector.c', line 85
static
VALUE ray_vector2_init_copy(VALUE self, VALUE other) {
*ray_rb2vector2_ptr(self) = *ray_rb2vector2_ptr(other);
return self;
}
|
#inside?(rect) ⇒ true, false
Returns True if the receive is contained in the rect.
8 9 10 |
# File 'lib/ray/vector.rb', line 8 def inside?(rect) rect.to_rect.contain? self end |
#length ⇒ Float Also known as: norm
Returns The length of the vector.
78 79 80 |
# File 'lib/ray/vector.rb', line 78 def length Math.sqrt(x * x + y * y) end |
#normalize ⇒ Ray::Vector2
Returns Normalized vector (i.e. length will be 1).
85 86 87 |
# File 'lib/ray/vector.rb', line 85 def normalize self / length end |
#normalize! ⇒ Object
Normalizes the vector, by dividing it by its length.
90 91 92 93 94 95 96 97 |
# File 'lib/ray/vector.rb', line 90 def normalize! length = self.length self.x /= length self.y /= length self end |
#outside?(rect) ⇒ Boolean
Opposite of #inside?
13 14 15 |
# File 'lib/ray/vector.rb', line 13 def outside?(rect) !inside?(rect) end |
#pretty_print(q) ⇒ Object
144 145 146 147 148 149 150 |
# File 'lib/ray/vector.rb', line 144 def pretty_print(q) q.text "(" q.pp(("%g" % x).to_f) # hides simple-precision inacurracy q.text ", " q.pp(("%g" % y).to_f) q.text ")" end |
#to_a ⇒ Object
132 133 134 |
# File 'lib/ray/vector.rb', line 132 def to_a [x, y] end |
#to_s ⇒ Object
140 141 142 |
# File 'lib/ray/vector.rb', line 140 def to_s "(%g, %g)" % [x, y] end |
#to_vector2 ⇒ Object
136 137 138 |
# File 'lib/ray/vector.rb', line 136 def to_vector2 self end |
#x ⇒ Float Also known as: w
Returns The x position of the vector.
92 93 94 95 |
# File 'ext/vector.c', line 92
static
VALUE ray_vector2_x(VALUE self) {
return rb_float_new(ray_rb2vector2_ptr(self)->x);
}
|
#x=(x) ⇒ void Also known as: w=, width=
110 111 112 113 114 115 |
# File 'ext/vector.c', line 110
static
VALUE ray_vector2_set_x(VALUE self, VALUE x) {
rb_check_frozen(self);
ray_rb2vector2_ptr(self)->x = NUM2DBL(x);
return x;
}
|
#y ⇒ Float Also known as: h
Returns The y position of the vector.
98 99 100 101 |
# File 'ext/vector.c', line 98
static
VALUE ray_vector2_y(VALUE self) {
return rb_float_new(ray_rb2vector2_ptr(self)->y);
}
|
#y=(y) ⇒ void Also known as: h=, height=
124 125 126 127 128 129 |
# File 'ext/vector.c', line 124
static
VALUE ray_vector2_set_y(VALUE self, VALUE y) {
rb_check_frozen(self);
ray_rb2vector2_ptr(self)->y = NUM2DBL(y);
return y;
}
|