sfml3.rb

Class: SFML::Vector2

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
ext/system/vec2.c,
ext/system/vec2.c

Overview

A 2D vector of floats, used throughout the library for positions, sizes, scale factors and directions.

Includes Enumerable and behaves like a 2-element sequence: it responds to #each, #to_a and #[], and can be compared or combined with a plain [x, y] Array anywhere a Vector2 is accepted.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#new ⇒ Vector2(0, 0) #new(x, y) ⇒ Vector2(x] #new([x, y]) ⇒ Vector2(x] #new(other_vector2) ⇒ Vector2

Creates a new 2D vector from two coordinates, a 2-element Array, or another Vector2.

Overloads:

  • #new ⇒ Vector2(0, 0)
  • #new(x, y) ⇒ Vector2(x]

    Returns Vector2(x].

  • #new([x, y]) ⇒ Vector2(x]

    Returns Vector2(x].

Raises:

  • (ArgumentError) —

    if given an Array shorter than 2 elements, or an argument count other than 0, 1 or 2



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'ext/system/vec2.c', line 51

static VALUE Vector2_initialize(int argc, VALUE* argv, VALUE self) {
    Vector2* ptr;
    float x = 0;
    float y = 0;

    if (argc == 1 && rb_obj_is_kind_of(argv[0], rb_cVector2)) {
        ptr = Get_Vector2_Struct(argv[0]);
        x = ptr->vec.x;
        y = ptr->vec.y;
    } else if (argc == 1 && RB_TYPE_P(argv[0], T_ARRAY)) {
        if (RARRAY_LEN(argv[0]) < 2) {
            raise_invalid_array_length(2);
        }

        x = NUM2DBL(rb_ary_entry(argv[0], 0));
        y = NUM2DBL(rb_ary_entry(argv[0], 1));
    } else if (argc == 2) {
        x = NUM2DBL(argv[0]);
        y = NUM2DBL(argv[1]);
    } else if (argc != 0) {
        raise_invalid_arguments_excepted(2, argc);
    }

    ptr = Get_Vector2_Struct(self);
    ptr->vec.x = x;
    ptr->vec.y = y;

    return self;
}

Instance Attribute Details

#x ⇒ Float

Returns the X component of the vector.

Returns:

  • (Float)

Returns:

  • (Float) —

    the X component



252
253
254
# File 'ext/system/vec2.c', line 252

static VALUE Vector2_get_x(VALUE self) {
    return DBL2NUM(((Vector2*)Get_Vector2_Struct(self))->vec.x);
}

#y ⇒ Float

Returns the Y component of the vector.

Returns:

  • (Float)

Returns:

  • (Float) —

    the Y component



252
253
254
# File 'ext/system/vec2.c', line 252

static VALUE Vector2_get_y(VALUE self) {
    return DBL2NUM(((Vector2*)Get_Vector2_Struct(self))->vec.y);
}

Instance Method Details

#*(scalar) ⇒ Vector2

Multiplies both components by scalar.

Returns:

Returns:



230
231
232
233
234
235
# File 'ext/system/vec2.c', line 230

static VALUE Vector2_mul(VALUE self, VALUE rb_scalar) {
    Vector2* a = Get_Vector2_Struct(self);
    float scalar = NUM2DBL(rb_scalar);

    return vec2f_to_rb((sfVector2f){a->vec.x * scalar, a->vec.y * scalar});
}

#+(other) ⇒ Vector2

other may be a Vector2 or a 2-element Array.

Returns:

Returns:



202
203
204
205
206
207
# File 'ext/system/vec2.c', line 202

static VALUE Vector2_add(VALUE self, VALUE rb_other) {
    Vector2* a = Get_Vector2_Struct(self);
    sfVector2f b = vec2f_from_rb(rb_other);

    return vec2f_to_rb((sfVector2f){a->vec.x + b.x, a->vec.y + b.y});
}

#-(other) ⇒ Vector2

other may be a Vector2 or a 2-element Array.

Returns:

Returns:

  • (Vector2) —

    componentwise difference



216
217
218
219
220
221
# File 'ext/system/vec2.c', line 216

static VALUE Vector2_sub(VALUE self, VALUE rb_other) {
    Vector2* a = Get_Vector2_Struct(self);
    sfVector2f b = vec2f_from_rb(rb_other);

    return vec2f_to_rb((sfVector2f){a->vec.x - b.x, a->vec.y - b.y});
}

#==(other) ⇒ Boolean

other may be a Vector2 or a 2-element Array.

Returns:

  • (Boolean)

Returns:

  • (Boolean)


180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'ext/system/vec2.c', line 180

static VALUE Vector2_eql(VALUE self, VALUE rb_other) {
    Vector2* a = Get_Vector2_Struct(self);
    sfVector2f b;

    if (rb_obj_is_kind_of(rb_other, rb_cVector2)) {
        b = ((Vector2*)Get_Vector2_Struct(rb_other))->vec;
    } else if (RB_TYPE_P(rb_other, T_ARRAY) && RARRAY_LEN(rb_other) >= 2) {
        b = vec2f_from_rb(rb_other);
    } else {
        return Qfalse;
    }

    return BOOL2RB(a->vec.x == b.x && a->vec.y == b.y);
}

#[](index) ⇒ Float

Returns the component stored at index.

Returns:

  • (Float)

Returns:

  • (Float) —

    x for index 0, y for index 1



159
160
161
# File 'ext/system/vec2.c', line 159

static VALUE Vector2_aref(VALUE self, VALUE rb_index) {
    return rb_ary_entry(Vector2_to_a(self), NUM2LONG(rb_index));
}

#each {|component| ... } ⇒ self

Yields x then y.

Yields:

  • (component)

Returns:

  • (self)

Returns:

  • (self)


143
144
145
146
147
148
149
150
# File 'ext/system/vec2.c', line 143

static VALUE Vector2_each(VALUE self) {
    Vector2* vec = Get_Vector2_Struct(self);

    rb_yield(DBL2NUM(vec->vec.x));
    rb_yield(DBL2NUM(vec->vec.y));

    return self;
}

#size ⇒ Integer

Returns the number of components, always 2.

Returns:

  • (Integer)

Returns:

  • (Integer) —

    always 2



169
170
171
# File 'ext/system/vec2.c', line 169

static VALUE Vector2_size(VALUE self) {
    return INT2NUM(2);
}

#size ⇒ Integer

Returns the number of components, always 2.

Returns:

  • (Integer)

Returns:

  • (Integer) —

    always 2



169
170
171
# File 'ext/system/vec2.c', line 169

static VALUE Vector2_size(VALUE self) {
    return INT2NUM(2);
}

#to_a ⇒ Array, Float

Returns the vector's components as a two-element Array.

Returns ].

Returns:

  • (Array, Float) —

    ]

Returns:

  • (Array<Float>) —

    [x, y]



131
132
133
134
135
# File 'ext/system/vec2.c', line 131

static VALUE Vector2_to_a(VALUE self) {
    Vector2* vec = Get_Vector2_Struct(self);

    return rb_ary_new_from_args(2, DBL2NUM(vec->vec.x), DBL2NUM(vec->vec.y));
}

#to_a ⇒ Array, Float

Returns the vector's components as a two-element Array.

Returns ].

Returns:

  • (Array, Float) —

    ]

Returns:

  • (Array<Float>) —

    [x, y]



131
132
133
134
135
# File 'ext/system/vec2.c', line 131

static VALUE Vector2_to_a(VALUE self) {
    Vector2* vec = Get_Vector2_Struct(self);

    return rb_ary_new_from_args(2, DBL2NUM(vec->vec.x), DBL2NUM(vec->vec.y));
}

#to_s ⇒ String

Returns a human-readable "(x, y)" representation.

Returns:

  • (String)

Returns:

  • (String) —

    "(x, y)"



243
244
245
246
247
248
249
250
# File 'ext/system/vec2.c', line 243

static VALUE Vector2_to_s(VALUE self) {
    Vector2* a = Get_Vector2_Struct(self);
    char buffer[64];

    snprintf(buffer, sizeof(buffer), "(%g, %g)", a->vec.x, a->vec.y);

    return rb_str_new2(buffer);
}