# Class: Ray::Vector3

Inherits:
Object
• Object
show all
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

• (x + other.x, y + other.y, z + other.z).

• (x, y, y).

• (x - other.x, y - other.y, z - other.z).

• (-x, -y, -z).

• #dist(other) ⇒ Float (also: #distance)

The distance between two vectors.

• Dot product (i.e. x * vector.x + y * vector.y + z * vector.z).

• constructor
• #length ⇒ Float (also: #norm)

The length of the vector.

• Normalized vector (i.e. length will be 1).

• Normalizes the vector, by dividing it by its length.

• The x position of the vector.

• Sets the x position of the vector.

• The y position of the vector.

• Sets the y position of the vector.

• The z position of the vector.

• Sets the z position of the vector.

## Constructor Details

### #initialize(x = 0.0, y = 0.0, z = 0.0) ⇒ Object

Parameters:

• x (Float) (defaults to: 0.0)

X position of the vector

• y (Float) (defaults to: 0.0)

Y postiion of the vector

• z (Float) (defaults to: 0.0)

Z postiion of the vector

 ``` 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

• #*(float) ⇒ Ray::Vector3

Returns (x * float, y * float, z * float).

Parameters:

• float (Float)

Returns:

• (x * float, y * float, z * float)

• #*(vector) ⇒ Ray::Vector3

Returns (a.x * b.x, a.y * b.y, a.z * b.z).

Parameters:

• vector

Returns:

• (a.x * b.x, a.y * b.y, a.z * b.z)

 ``` 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).

Parameters:

• other

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```

### #[email protected] ⇒ Ray::Vector3

Returns (x, y, y).

Returns:

• (x, y, y)

 ``` 227``` ```# File 'lib/ray/vector.rb', line 227 def [email protected]; self; end```

### #-(other) ⇒ Ray::Vector3

Returns (x - other.x, y - other.y, z - other.z).

Parameters:

• other

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```

### #[email protected] ⇒ Ray::Vector3

Returns (-x, -y, -z).

Returns:

• (-x, -y, -z)

 ``` 224``` ```# File 'lib/ray/vector.rb', line 224 def [email protected]; Ray::Vector3[-x, -y, -z] end```

### #/(float) ⇒ Ray::Vector3 #/(vector) ⇒ Ray::Vector3

• #/(float) ⇒ Ray::Vector3

Returns (x / float, y / float, z / float).

Parameters:

• float (Float)

Returns:

• (x / float, y / float, z / float)

• #/(vector) ⇒ Ray::Vector3

Returns (a.x / b.x, a.y / b.y, a.z / b.z).

Parameters:

• vector

Returns:

• (a.x / b.x, a.y / b.y, a.z / b.z)

 ``` 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) ⇒ FloatAlso known as: distance

Returns The distance between two vectors.

Parameters:

• other

Returns:

• (Float)

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).

Parameters:

• vector

Returns:

• (Float)

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

Returns:

• (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 ⇒ FloatAlso known as: norm

Returns The length of the vector.

Returns:

• (Float)

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).

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.

Returns:

• (Float)

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

This method returns an undefined value.

Sets the x position of the vector

Parameters:

• x (Float)

New x position

 ``` 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.

Returns:

• (Float)

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

This method returns an undefined value.

Sets the y position of the vector

Parameters:

• y (Float)

New y position

 ``` 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.

Returns:

• (Float)

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

This method returns an undefined value.

Sets the z position of the vector

Parameters:

• z (Float)

New z position

 ``` 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; }```