Class: Ray::Vector3
- Inherits:
-
Object
- Object
- Ray::Vector3
- Defined in:
- ext/vector.c,
lib/ray/vector.rb,
ext/vector.c
Overview
Represents a (x, y, z) point in 3D space.
Instance Method Summary collapse
- #*(other) ⇒ Object
-
#+(other) ⇒ Ray::Vector3
(x + other.x, y + other.y, z + other.z).
-
#+@ ⇒ Ray::Vector3
(x, y, y).
-
#-(other) ⇒ Ray::Vector3
(x - other.x, y - other.y, z - other.z).
-
#-@ ⇒ Ray::Vector3
(-x, -y, -z).
- #/(other) ⇒ Object
- #==(other) ⇒ Object
-
#dist(other) ⇒ Float
(also: #distance)
The distance between two vectors.
-
#dot(vector) ⇒ Float
Dot product (i.e. x * vector.x + y * vector.y + z * vector.z).
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
- #initialize(x = 0.0, y = 0.0, z = 0.0) ⇒ Object constructor
- #initialize_copy(other) ⇒ Object
-
#length ⇒ Float
(also: #norm)
The length of the vector.
-
#normalize ⇒ Ray::Vector3
Normalized vector (i.e. length will be 1).
-
#normalize! ⇒ Object
Normalizes the vector, by dividing it by its length.
- #to_a ⇒ Object
- #to_s ⇒ Object
- #to_vector3 ⇒ Object
-
#x ⇒ Float
The x position of the vector.
-
#x=(x) ⇒ void
Sets the x position of the vector.
-
#y ⇒ Float
The y position of the vector.
-
#y=(y) ⇒ void
Sets the y position of the vector.
-
#z ⇒ Float
The z position of the vector.
-
#z=(z) ⇒ void
Sets the z position of the vector.
Constructor Details
#initialize(x = 0.0, y = 0.0, z = 0.0) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'ext/vector.c', line 145
static
VALUE ray_vector3_init(int argc, VALUE *argv, VALUE self) {
VALUE rb_x = Qnil, rb_y = Qnil, rb_z = Qnil;
rb_scan_args(argc, argv, "03", &rb_x, &rb_y, &rb_z);
say_vector3 *vector = ray_rb2vector3_ptr(self);
if (!NIL_P(rb_x)) vector->x = NUM2DBL(rb_x);
if (!NIL_P(rb_y)) vector->y = NUM2DBL(rb_y);
if (!NIL_P(rb_z)) vector->z = NUM2DBL(rb_z);
return self;
}
|
Instance Method Details
#*(float) ⇒ Ray::Vector3 #*(vector) ⇒ Ray::Vector3
183 184 185 186 187 188 189 190 |
# File 'lib/ray/vector.rb', line 183 def *(other) if other.respond_to? :to_vector3 other = other.to_vector3 Ray::Vector3[x * other.x, y * other.y, z * other.z] else Ray::Vector3[x * other, y * other, z * other] end end |
#+(other) ⇒ Ray::Vector3
Returns (x + other.x, y + other.y, z + other.z).
164 165 166 167 |
# File 'lib/ray/vector.rb', line 164 def +(other) other = other.to_vector3 Ray::Vector3[x + other.x, y + other.y, z + other.z] end |
#-(other) ⇒ Ray::Vector3
Returns (x - other.x, y - other.y, z - other.z).
171 172 173 174 |
# File 'lib/ray/vector.rb', line 171 def -(other) other = other.to_vector3 Ray::Vector3[x - other.x, y - other.y, z - other.z] end |
#-@ ⇒ Ray::Vector3
Returns (-x, -y, -z).
216 |
# File 'lib/ray/vector.rb', line 216 def -@; Ray::Vector3[-x, -y, -z] end |
#/(float) ⇒ Ray::Vector3 #/(vector) ⇒ Ray::Vector3
199 200 201 202 203 204 205 206 |
# File 'lib/ray/vector.rb', line 199 def /(other) if other.respond_to? :to_vector3 other = other.to_vector3 Ray::Vector3[x / other.x, y / other.y, z / other.z] else Ray::Vector3[x / other, y / other, z / other] end end |
#==(other) ⇒ Object
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/ray/vector.rb', line 251 def ==(other) if other.is_a? Vector3 x == other.x && y == other.y && z == other.z elsif other.is_a? Array if other.size <= 3 other = other.to_vector3 x == other.x && y == other.y && z == other.z else false end elsif other.respond_to? :to_vector3 other = other.to_vector3 x == other.x && y == other.y && z == other.z else false end end |
#dist(other) ⇒ Float Also known as: distance
Returns The distance between two vectors.
246 247 248 |
# File 'lib/ray/vector.rb', line 246 def dist(other) (self - other).length end |
#dot(vector) ⇒ Float
Returns Dot product (i.e. x * vector.x + y * vector.y + z * vector.z).
210 211 212 213 |
# File 'lib/ray/vector.rb', line 210 def dot(vector) vector = vector.to_vector3 x * vector.x + y * vector.y + z * vector.z end |
#eql?(other) ⇒ Boolean
269 270 271 |
# File 'lib/ray/vector.rb', line 269 def eql?(other) self.class == other.class && self == other end |
#hash ⇒ Object
273 274 275 |
# File 'lib/ray/vector.rb', line 273 def hash to_a.hash end |
#initialize_copy(other) ⇒ Object
158 159 160 161 162 |
# File 'ext/vector.c', line 158
static
VALUE ray_vector3_init_copy(VALUE self, VALUE other) {
*ray_rb2vector3_ptr(self) = *ray_rb2vector3_ptr(other);
return self;
}
|
#length ⇒ Float Also known as: norm
Returns The length of the vector.
222 223 224 |
# File 'lib/ray/vector.rb', line 222 def length Math.sqrt(x * x + y * y + z * z) end |
#normalize ⇒ Ray::Vector3
Returns Normalized vector (i.e. length will be 1).
229 230 231 |
# File 'lib/ray/vector.rb', line 229 def normalize self / length end |
#normalize! ⇒ Object
Normalizes the vector, by dividing it by its length.
234 235 236 237 238 239 240 241 242 |
# File 'lib/ray/vector.rb', line 234 def normalize! length = self.length self.x /= length self.y /= length self.z /= length self end |
#to_a ⇒ Object
281 282 283 |
# File 'lib/ray/vector.rb', line 281 def to_a [x, y, z] end |
#to_s ⇒ Object
277 278 279 |
# File 'lib/ray/vector.rb', line 277 def to_s "(%g, %g, %g)" % [x, y, z] end |
#to_vector3 ⇒ Object
285 286 287 |
# File 'lib/ray/vector.rb', line 285 def to_vector3 self end |
#x ⇒ Float
Returns The x position of the vector.
165 166 167 168 |
# File 'ext/vector.c', line 165
static
VALUE ray_vector3_x(VALUE self) {
return rb_float_new(ray_rb2vector3_ptr(self)->x);
}
|
#x=(x) ⇒ void
189 190 191 192 193 194 |
# File 'ext/vector.c', line 189
static
VALUE ray_vector3_set_x(VALUE self, VALUE x) {
rb_check_frozen(self);
ray_rb2vector3_ptr(self)->x = NUM2DBL(x);
return x;
}
|
#y ⇒ Float
Returns The y position of the vector.
171 172 173 174 |
# File 'ext/vector.c', line 171
static
VALUE ray_vector3_y(VALUE self) {
return rb_float_new(ray_rb2vector3_ptr(self)->y);
}
|
#y=(y) ⇒ void
203 204 205 206 207 208 |
# File 'ext/vector.c', line 203
static
VALUE ray_vector3_set_y(VALUE self, VALUE y) {
rb_check_frozen(self);
ray_rb2vector3_ptr(self)->y = NUM2DBL(y);
return y;
}
|
#z ⇒ Float
Returns The z position of the vector.
177 178 179 180 |
# File 'ext/vector.c', line 177
static
VALUE ray_vector3_z(VALUE self) {
return rb_float_new(ray_rb2vector3_ptr(self)->z);
}
|
#z=(z) ⇒ void
217 218 219 220 221 222 |
# File 'ext/vector.c', line 217
static
VALUE ray_vector3_set_z(VALUE self, VALUE z) {
rb_check_frozen(self);
ray_rb2vector3_ptr(self)->z = NUM2DBL(z);
return z;
}
|