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.
- #pretty_print(q) ⇒ Object
- #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
191 192 193 194 195 196 197 198 |
# File 'lib/ray/vector.rb', line 191 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).
172 173 174 175 |
# File 'lib/ray/vector.rb', line 172 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).
179 180 181 182 |
# File 'lib/ray/vector.rb', line 179 def -(other) other = other.to_vector3 Ray::Vector3[x - other.x, y - other.y, z - other.z] end |
#-@ ⇒ Ray::Vector3
Returns (-x, -y, -z).
224 |
# File 'lib/ray/vector.rb', line 224 def -@; Ray::Vector3[-x, -y, -z] end |
#/(float) ⇒ Ray::Vector3 #/(vector) ⇒ Ray::Vector3
207 208 209 210 211 212 213 214 |
# File 'lib/ray/vector.rb', line 207 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
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/ray/vector.rb', line 259 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.
254 255 256 |
# File 'lib/ray/vector.rb', line 254 def dist(other) (self - other).length end |
#dot(vector) ⇒ Float
Returns Dot product (i.e. x * vector.x + y * vector.y + z * vector.z).
218 219 220 221 |
# File 'lib/ray/vector.rb', line 218 def dot(vector) vector = vector.to_vector3 x * vector.x + y * vector.y + z * vector.z end |
#eql?(other) ⇒ Boolean
277 278 279 |
# File 'lib/ray/vector.rb', line 277 def eql?(other) self.class == other.class && self == other end |
#hash ⇒ Object
281 282 283 |
# File 'lib/ray/vector.rb', line 281 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.
230 231 232 |
# File 'lib/ray/vector.rb', line 230 def length Math.sqrt(x * x + y * y + z * z) end |
#normalize ⇒ Ray::Vector3
Returns Normalized vector (i.e. length will be 1).
237 238 239 |
# File 'lib/ray/vector.rb', line 237 def normalize self / length end |
#normalize! ⇒ Object
Normalizes the vector, by dividing it by its length.
242 243 244 245 246 247 248 249 250 |
# File 'lib/ray/vector.rb', line 242 def normalize! length = self.length self.x /= length self.y /= length self.z /= length self end |
#pretty_print(q) ⇒ Object
289 290 291 292 293 294 295 296 297 |
# File 'lib/ray/vector.rb', line 289 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 ", " q.pp(("%g" % z).to_f) q.text ")" end |
#to_a ⇒ Object
299 300 301 |
# File 'lib/ray/vector.rb', line 299 def to_a [x, y, z] end |
#to_s ⇒ Object
285 286 287 |
# File 'lib/ray/vector.rb', line 285 def to_s "(%g, %g, %g)" % [x, y, z] end |
#to_vector3 ⇒ Object
303 304 305 |
# File 'lib/ray/vector.rb', line 303 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;
}
|