# Class: Ray::Vector2

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

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

• (x, y).

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

• (-x, -y).

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

The distance between two vectors.

• A.x * b.x + a.y * b.y.

• constructor
• True if the receive is contained in the rect.

• #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.

• Opposite of #inside?.

• #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

Parameters:

• x (Float) (defaults to: 0.0)

X position of the vector

• y (Float) (defaults to: 0.0)

Y postiion of the vector

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

• #*(float) ⇒ Ray::Vector2

Returns (x * float, y * float).

Parameters:

• float (Float)

Returns:

• (x * float, y * float)

• #*(vector) ⇒ Ray::Vector2

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

Parameters:

• vector

Returns:

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

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

Parameters:

• other

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

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

Returns (x, y).

Returns:

• (x, y)

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

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

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

Parameters:

• other

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

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

Returns (-x, -y).

Returns:

• (-x, -y)

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

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

• #/(float) ⇒ Ray::Vector2

Returns (x / float, y / float).

Parameters:

• float (Float)

Returns:

• (x / float, y / float)

• #/(vector) ⇒ Ray::Vector2

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

Parameters:

• vector

Returns:

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

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

Returns The distance between two vectors.

Parameters:

• other

Returns:

• (Float)

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.

Parameters:

Returns:

• (Float)

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

Returns:

• (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.

Returns:

• (true, false)

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

Returns The length of the vector.

Returns:

• (Float)

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

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?

Returns:

• (Boolean)
 ``` 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 ⇒ FloatAlso known as: w

Returns The x position of the vector.

Returns:

• (Float)

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) ⇒ voidAlso known as: w=, width=

This method returns an undefined value.

Sets the x position of the vector

Parameters:

• x (Float)

New x position

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

Returns The y position of the vector.

Returns:

• (Float)

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) ⇒ voidAlso known as: h=, height=

This method returns an undefined value.

Sets the y position of the vector

Parameters:

• y (Float)

New y position

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