Class: Snow::Vec2
- Inherits:
-
Data
- Object
- Data
- Snow::Vec2
- Defined in:
- lib/snow-math/vec2.rb,
lib/snow-math/ptr.rb,
lib/snow-math/to_a.rb,
lib/snow-math/inspect.rb,
lib/snow-math/marshal.rb,
lib/snow-math/swizzle.rb,
ext/snow-math/snow-math.c
Overview
A 2-component vector class.
Constant Summary collapse
- POS_X =
self.new(1, 0).freeze
- POS_Y =
self.new(0, 1).freeze
- NEG_X =
self.new(-1, 0).freeze
- NEG_Y =
self.new(0, -1).freeze
- ONE =
self.new(1, 1).freeze
- ZERO =
self.new.freeze
- SIZE =
INT2FIX(sizeof(vec2_t))
- LENGTH =
INT2FIX(sizeof(vec2_t) / sizeof(s_float_t))
- @@SWIZZLE_CHARS =
/^[xy]{2,4}$/
- @@SWIZZLE_MAPPING =
{ 2 => self, 3 => ::Snow::Vec3, 4 => ::Snow::Vec4, 'x' => 0, 'y' => 1 }
Class Method Summary collapse
-
.new(*args) ⇒ Object
(also: [])
Allocates a Vec2.
Instance Method Summary collapse
-
#==(sm_other) ⇒ Object
Tests whether a Vec2 is equivalent to another Vec2, a Vec3, Vec4, or a Quat.
-
#add(*args) ⇒ Object
(also: #+)
Adds this and another vector’s components together and returns the result.
-
#add!(rhs) ⇒ Object
Calls #add(rhs, self).
-
#address ⇒ Object
Returns the memory address of the object.
-
#copy(*args) ⇒ Object
(also: #dup, #clone)
Returns a copy of self.
-
#divide(*args) ⇒ Object
(also: #/)
Divides this vector’s components by a scalar value and returns the result.
-
#divide!(rhs) ⇒ Object
Calls #divide(rhs, self).
-
#dot_product(sm_other) ⇒ Object
(also: #**)
Returns the dot product of this and another Vec2 or the XY components of a Vec3, Vec4, or Quat.
-
#fetch ⇒ Object
(also: #[])
Gets the component of the Vec2 at the given index.
-
#initialize(*args) ⇒ Object
constructor
Sets the Vec2’s components.
-
#inverse(*args) ⇒ Object
(also: #~)
Returns a vector whose components are the multiplicative inverse of this vector’s.
-
#inverse! ⇒ Object
Calls #inverse(self).
-
#length ⇒ Object
Returns the length of the Vec2 in components.
-
#magnitude ⇒ Object
Returns the magnitude of self.
-
#magnitude_squared ⇒ Object
Returns the squared magnitude of self.
-
#multiply(rhs, output = nil) ⇒ Object
(also: #*)
Calls #multiply_vec2 and #scale, respectively.
-
#multiply!(rhs) ⇒ Object
Calls #multiply(rhs, self).
-
#multiply_vec2(*args) ⇒ Object
Multiplies this and another vector’s components together and returns the result.
-
#multiply_vec2!(rhs) ⇒ Object
Calls #multiply_vec2(rhs, self).
-
#negate(*args) ⇒ Object
(also: #-@)
Negates this vector’s components and returns the result.
-
#negate! ⇒ Object
Calls #negate(self).
-
#normalize(*args) ⇒ Object
Returns a vector whose components are the multiplicative inverse of this vector’s.
-
#normalize! ⇒ Object
Calls #normalize(self).
-
#project(*args) ⇒ Object
Projects this vector onto a normal vector and returns the result.
-
#reflect(*args) ⇒ Object
Reflects this vector against a normal vector and returns the result.
-
#scale(*args) ⇒ Object
Scales this vector’s components by a scalar value and returns the result.
-
#scale!(rhs) ⇒ Object
Calls #scale(rhs, self).
-
#set(*args) ⇒ Object
Sets the Vec2’s components.
-
#size ⇒ Object
Returns the length in bytes of the Vec2.
-
#store ⇒ Object
(also: #[]=)
Sets the Vec2’s component at the index to the value.
-
#subtract(*args) ⇒ Object
(also: #-)
Subtracts another vector’s components from this vector’s and returns the result.
-
#subtract!(rhs) ⇒ Object
Calls #subtract(rhs, self).
- #to_quat ⇒ Object
-
#to_s ⇒ Object
Returns a string representation of self.
- #to_vec2 ⇒ Object
- #to_vec3 ⇒ Object
- #to_vec4 ⇒ Object
-
#x ⇒ Object
Returns the X component of the vector.
-
#x=(value) ⇒ Object
Sets the X component of the vector.
-
#y ⇒ Object
Returns the Y component of the vector.
-
#y=(value) ⇒ Object
Sets the Y component of the vector.
Methods included from SwizzleSupport
#__under_method_missing__, #method_missing
Methods included from BaseMarshalSupport
Methods included from InspectSupport
Methods included from ArraySupport
Methods included from FiddlePointerSupport
Constructor Details
#initialize(*args) ⇒ Object
Sets the Vec2’s components.
call-seq:
set(x, y) -> vec2 with components [x, y]
set([x, y]) -> vec2 with components [x, y]
set(vec2) -> copy of vec2
set(vec3) -> vec2 with components [vec3.xy]
set(vec4) -> vec2 with components [vec4.xy]
set(quat) -> vec2 with components [quat.xy]
1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 |
# File 'ext/snow-math/snow-math.c', line 1946
static VALUE sm_vec2_init(int argc, VALUE *argv, VALUE sm_self)
{
vec2_t *self = sm_unwrap_vec2(sm_self, NULL);
size_t arr_index = 0;
rb_check_frozen(sm_self);
switch(argc) {
/* Default value */
case 0: { break; }
/* Copy or by-array */
case 1: {
if (SM_IS_A(argv[0], vec2) ||
SM_IS_A(argv[0], vec3) ||
SM_IS_A(argv[0], vec4) ||
SM_IS_A(argv[0], quat)) {
sm_unwrap_vec2(argv[0], *self);
break;
}
/* Optional offset into array provided */
if (0) {
case 2:
if (!SM_RB_IS_A(argv[0], rb_cArray)) {
self[0][0] = (s_float_t)NUM2DBL(argv[0]);
self[0][1] = (s_float_t)NUM2DBL(argv[1]);
break;
}
arr_index = NUM2SIZET(argv[1]);
}
/* Array of values */
VALUE arrdata = argv[0];
const size_t arr_end = arr_index + 2;
s_float_t *vec_elem = *self;
for (; arr_index < arr_end; ++arr_index, ++vec_elem) {
*vec_elem = (s_float_t)NUM2DBL(rb_ary_entry(arrdata, (long)arr_index));
}
break;
}
default: {
rb_raise(rb_eArgError, "Invalid arguments to initialize/set");
break;
}
} /* switch (argc) */
return sm_self;
}
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Snow::SwizzleSupport
Class Method Details
.new(*args) ⇒ Object Also known as: []
Allocates a Vec2.
call-seq:
new() -> vec2 with components [0, 0]
new(x, y) -> vec2 with components [x, y]
new([x, y]) -> vec2 with components [x, y]
new(vec2) -> copy of vec3
new(vec3) -> vec2 of vec3's x and y components
new(vec4) -> vec2 of vec4's x and y components
new(quat) -> vec2 of quat's x and y components
1926 1927 1928 1929 1930 1931 |
# File 'ext/snow-math/snow-math.c', line 1926
static VALUE sm_vec2_new(int argc, VALUE *argv, VALUE self)
{
VALUE sm_vec = sm_wrap_vec2(g_vec2_zero, self);
rb_obj_call_init(sm_vec, argc, argv);
return sm_vec;
}
|
Instance Method Details
#==(sm_other) ⇒ Object
Tests whether a Vec2 is equivalent to another Vec2, a Vec3, Vec4, or a Quat. When testing for equivalency against 4-component objects, only the first two components are compared.
call-seq:
vec2 == other_vec2 -> bool
vec2 == vec3 -> bool
vec2 == vec4 -> bool
vec2 == quat -> bool
2120 2121 2122 2123 2124 2125 2126 2127 |
# File 'ext/snow-math/snow-math.c', line 2120
static VALUE sm_vec2_equals(VALUE sm_self, VALUE sm_other)
{
if (!RTEST(sm_other) || (!SM_IS_A(sm_other, vec2) && !SM_IS_A(sm_other, vec3) && !SM_IS_A(sm_other, vec4) && !SM_IS_A(sm_other, quat))) {
return Qfalse;
}
return vec2_equals(*sm_unwrap_vec2(sm_self, NULL), *sm_unwrap_vec2(sm_other, NULL)) ? Qtrue : Qfalse;
}
|
#add(*args) ⇒ Object Also known as: +
Adds this and another vector’s components together and returns the result.
call-seq:
add(vec2, output = nil) -> output or new vec2
1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 |
# File 'ext/snow-math/snow-math.c', line 1792
static VALUE sm_vec2_add(int argc, VALUE *argv, VALUE sm_self)
{
VALUE sm_rhs;
VALUE sm_out;
vec2_t *self;
vec2_t *rhs;
rb_scan_args(argc, argv, "11", &sm_rhs, &sm_out);
self = sm_unwrap_vec2(sm_self, NULL);
if (!SM_IS_A(sm_rhs, vec2) && !SM_IS_A(sm_rhs, vec3) && !SM_IS_A(sm_rhs, vec4) && !SM_IS_A(sm_rhs, quat)) {
rb_raise(rb_eTypeError,
kSM_WANT_TWO_TO_FOUR_FORMAT_LIT,
rb_obj_classname(sm_rhs));
return Qnil;
}
rhs = sm_unwrap_vec2(sm_rhs, NULL);
if (argc == 2) {
if (!RTEST(sm_out)) {
goto SM_LABEL(skip_output);
}{
vec2_t *output;
if (!SM_IS_A(sm_out, vec2) && !SM_IS_A(sm_out, vec3) && !SM_IS_A(sm_out, vec4) && !SM_IS_A(sm_out, quat)) {
rb_raise(rb_eTypeError,
kSM_WANT_TWO_TO_FOUR_FORMAT_LIT,
rb_obj_classname(sm_out));
return Qnil;
}
rb_check_frozen(sm_out);
output = sm_unwrap_vec2(sm_out, NULL);
vec2_add(*self, *rhs, *output);
}} else if (argc == 1) {
SM_LABEL(skip_output): {
vec2_t output;
vec2_add(*self, *rhs, output);
sm_out = sm_wrap_vec2(output, rb_obj_class(sm_self));
rb_obj_call_init(sm_out, 0, 0);
}} else {
rb_raise(rb_eArgError, "Invalid number of arguments to add");
}
return sm_out;
}
|
#add!(rhs) ⇒ Object
Calls #add(rhs, self)
call-seq: add!(rhs) -> self
141 142 143 |
# File 'lib/snow-math/vec2.rb', line 141 def add!(rhs) add rhs, self end |
#address ⇒ Object
Returns the memory address of the object.
call-seq: address -> fixnum
6739 6740 6741 6742 6743 6744 |
# File 'ext/snow-math/snow-math.c', line 6739
static VALUE sm_get_address(VALUE sm_self)
{
void *data_ptr = NULL;
Data_Get_Struct(sm_self, void, data_ptr);
return ULL2NUM((unsigned long long)data_ptr);
}
|
#copy(*args) ⇒ Object Also known as: dup, clone
Returns a copy of self.
call-seq:
copy(output = nil) -> output or new vec2
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 |
# File 'ext/snow-math/snow-math.c', line 1482
static VALUE sm_vec2_copy(int argc, VALUE *argv, VALUE sm_self)
{
VALUE sm_out;
vec2_t *self;
rb_scan_args(argc, argv, "01", &sm_out);
self = sm_unwrap_vec2(sm_self, NULL);
if (argc == 1) {
if (!RTEST(sm_out)) {
goto SM_LABEL(skip_output);
}{
vec2_t *output;
if (!SM_IS_A(sm_out, vec2) && !SM_IS_A(sm_out, vec3) && !SM_IS_A(sm_out, vec4) && !SM_IS_A(sm_out, quat)) {
rb_raise(rb_eTypeError,
kSM_WANT_TWO_TO_FOUR_FORMAT_LIT,
rb_obj_classname(sm_out));
return Qnil;
}
rb_check_frozen(sm_out);
output = sm_unwrap_vec2(sm_out, NULL);
vec2_copy (*self, *output);
}} else if (argc == 0) {
SM_LABEL(skip_output): {
vec2_t output;
vec2_copy (*self, output);
sm_out = sm_wrap_vec2(output, rb_obj_class(sm_self));
rb_obj_call_init(sm_out, 0, 0);
}} else {
rb_raise(rb_eArgError, "Invalid number of arguments to copy");
}
return sm_out;
}
|
#divide(*args) ⇒ Object Also known as: /
Divides this vector’s components by a scalar value and returns the result.
call-seq:
divide(scalar, output = nil) -> output or new vec2
2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 |
# File 'ext/snow-math/snow-math.c', line 2084
static VALUE sm_vec2_divide(int argc, VALUE *argv, VALUE sm_self)
{
VALUE sm_out;
VALUE sm_scalar;
s_float_t scalar;
vec2_t *self = sm_unwrap_vec2(sm_self, NULL);
rb_scan_args(argc, argv, "11", &sm_scalar, &sm_out);
scalar = NUM2DBL(sm_scalar);
if (SM_IS_A(sm_out, vec2) || SM_IS_A(sm_out, vec3) || SM_IS_A(sm_out, vec4) || SM_IS_A(sm_out, quat)) {
rb_check_frozen(sm_out);
vec2_divide(*self, scalar, *sm_unwrap_vec2(sm_out, NULL));
} else {
vec2_t out;
vec2_divide(*self, scalar, out);
sm_out = sm_wrap_vec2(out, rb_obj_class(sm_self));
rb_obj_call_init(sm_out, 0, 0);
}
return sm_out;
}
|
#divide!(rhs) ⇒ Object
Calls #divide(rhs, self)
call-seq: divide!(rhs) -> self
162 163 164 |
# File 'lib/snow-math/vec2.rb', line 162 def divide!(rhs) divide rhs, self end |
#dot_product(sm_other) ⇒ Object Also known as: **
Returns the dot product of this and another Vec2 or the XY components of a Vec3, Vec4, or Quat.
call-seq:
dot_product(vec2) -> float
dot_product(vec3) -> float
dot_product(vec4) -> float
dot_product(quat) -> float
1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 |
# File 'ext/snow-math/snow-math.c', line 1895
static VALUE sm_vec2_dot_product(VALUE sm_self, VALUE sm_other)
{
if (!SM_IS_A(sm_other, vec2) &&
!SM_IS_A(sm_other, vec3) &&
!SM_IS_A(sm_other, vec4) &&
!SM_IS_A(sm_other, quat)) {
rb_raise(rb_eArgError,
"Expected a Vec2, Vec3, Vec4, or Quat, got %s",
rb_obj_classname(sm_other));
return Qnil;
}
return DBL2NUM(
vec2_dot_product(
*sm_unwrap_vec2(sm_self, NULL),
*sm_unwrap_vec2(sm_other, NULL)));
}
|
#fetch ⇒ Object Also known as: []
Gets the component of the Vec2 at the given index.
call-seq: fetch(index) -> float
1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 |
# File 'ext/snow-math/snow-math.c', line 1415
static VALUE sm_vec2_fetch (VALUE sm_self, VALUE sm_index)
{
static const int max_index = sizeof(vec2_t) / sizeof(s_float_t);
const vec2_t *self = sm_unwrap_vec2(sm_self, NULL);
int index = NUM2INT(sm_index);
if (index < 0 || index >= max_index) {
rb_raise(rb_eRangeError,
"Index %d is out of bounds, must be from 0 through %d", index, max_index - 1);
}
return DBL2NUM(self[0][NUM2INT(sm_index)]);
}
|
#inverse(*args) ⇒ Object Also known as: ~
Returns a vector whose components are the multiplicative inverse of this vector’s.
call-seq:
inverse(output = nil) -> output or new vec2
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 |
# File 'ext/snow-math/snow-math.c', line 1564
static VALUE sm_vec2_inverse(int argc, VALUE *argv, VALUE sm_self)
{
VALUE sm_out;
vec2_t *self;
rb_scan_args(argc, argv, "01", &sm_out);
self = sm_unwrap_vec2(sm_self, NULL);
if (argc == 1) {
if (!RTEST(sm_out)) {
goto SM_LABEL(skip_output);
}{
vec2_t *output;
if (!SM_IS_A(sm_out, vec2) && !SM_IS_A(sm_out, vec3) && !SM_IS_A(sm_out, vec4) && !SM_IS_A(sm_out, quat)) {
rb_raise(rb_eTypeError,
kSM_WANT_TWO_TO_FOUR_FORMAT_LIT,
rb_obj_classname(sm_out));
return Qnil;
}
rb_check_frozen(sm_out);
output = sm_unwrap_vec2(sm_out, NULL);
vec2_inverse (*self, *output);
}} else if (argc == 0) {
SM_LABEL(skip_output): {
vec2_t output;
vec2_inverse (*self, output);
sm_out = sm_wrap_vec2(output, rb_obj_class(sm_self));
rb_obj_call_init(sm_out, 0, 0);
}} else {
rb_raise(rb_eArgError, "Invalid number of arguments to inverse");
}
return sm_out;
}
|
#inverse! ⇒ Object
Calls #inverse(self)
call-seq: inverse! -> self
100 101 102 |
# File 'lib/snow-math/vec2.rb', line 100 def inverse! inverse self end |
#length ⇒ Object
Returns the length of the Vec2 in components. Result is always 2.
call-seq: length -> fixnum
1469 1470 1471 1472 |
# File 'ext/snow-math/snow-math.c', line 1469
static VALUE sm_vec2_length (VALUE self)
{
return SIZET2NUM(sizeof(vec2_t) / sizeof(s_float_t));
}
|
#magnitude ⇒ Object
Returns the magnitude of self.
call-seq:
magnitude -> float
2040 2041 2042 2043 |
# File 'ext/snow-math/snow-math.c', line 2040
static VALUE sm_vec2_magnitude(VALUE sm_self)
{
return DBL2NUM(vec2_length(*sm_unwrap_vec2(sm_self, NULL)));
}
|
#magnitude_squared ⇒ Object
Returns the squared magnitude of self.
call-seq:
magnitude_squared -> float
2027 2028 2029 2030 |
# File 'ext/snow-math/snow-math.c', line 2027
static VALUE sm_vec2_magnitude_squared(VALUE sm_self)
{
return DBL2NUM(vec2_length_squared(*sm_unwrap_vec2(sm_self, NULL)));
}
|
#multiply(rhs, output = nil) ⇒ Object Also known as: *
Calls #multiply_vec2 and #scale, respectively.
call-seq:
multiply(vec2, output = nil) -> output or new vec2
multiply(scalar, output = nil) -> output or new vec2
123 124 125 126 127 128 129 |
# File 'lib/snow-math/vec2.rb', line 123 def multiply(rhs, output = nil) case rhs when ::Snow::Vec2, ::Snow::Vec3, ::Snow::Vec4, ::Snow::Quat then multiply_vec2(rhs, output) when Numeric then scale(rhs, output) else raise TypeError, "Invalid type for RHS" end end |
#multiply!(rhs) ⇒ Object
Calls #multiply(rhs, self)
call-seq: multiply!(rhs) -> self
134 135 136 |
# File 'lib/snow-math/vec2.rb', line 134 def multiply!(rhs) multiply rhs, self end |
#multiply_vec2(*args) ⇒ Object
Multiplies this and another vector’s components together and returns the result.
call-seq:
multiply_vec2(vec2, output = nil) -> output or new vec2
1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 |
# File 'ext/snow-math/snow-math.c', line 1743
static VALUE sm_vec2_multiply(int argc, VALUE *argv, VALUE sm_self)
{
VALUE sm_rhs;
VALUE sm_out;
vec2_t *self;
vec2_t *rhs;
rb_scan_args(argc, argv, "11", &sm_rhs, &sm_out);
self = sm_unwrap_vec2(sm_self, NULL);
if (!SM_IS_A(sm_rhs, vec2) && !SM_IS_A(sm_rhs, vec3) && !SM_IS_A(sm_rhs, vec4) && !SM_IS_A(sm_rhs, quat)) {
rb_raise(rb_eTypeError,
kSM_WANT_TWO_TO_FOUR_FORMAT_LIT,
rb_obj_classname(sm_rhs));
return Qnil;
}
rhs = sm_unwrap_vec2(sm_rhs, NULL);
if (argc == 2) {
if (!RTEST(sm_out)) {
goto SM_LABEL(skip_output);
}{
vec2_t *output;
if (!SM_IS_A(sm_out, vec2) && !SM_IS_A(sm_out, vec3) && !SM_IS_A(sm_out, vec4) && !SM_IS_A(sm_out, quat)) {
rb_raise(rb_eTypeError,
kSM_WANT_TWO_TO_FOUR_FORMAT_LIT,
rb_obj_classname(sm_out));
return Qnil;
}
rb_check_frozen(sm_out);
output = sm_unwrap_vec2(sm_out, NULL);
vec2_multiply(*self, *rhs, *output);
}} else if (argc == 1) {
SM_LABEL(skip_output): {
vec2_t output;
vec2_multiply(*self, *rhs, output);
sm_out = sm_wrap_vec2(output, rb_obj_class(sm_self));
rb_obj_call_init(sm_out, 0, 0);
}} else {
rb_raise(rb_eArgError, "Invalid number of arguments to multiply");
}
return sm_out;
}
|
#multiply_vec2!(rhs) ⇒ Object
Calls #multiply_vec2(rhs, self)
call-seq: multiply_vec2!(rhs) -> self
114 115 116 |
# File 'lib/snow-math/vec2.rb', line 114 def multiply_vec2!(rhs) multiply_vec2 rhs, self end |
#negate(*args) ⇒ Object Also known as: -@
Negates this vector’s components and returns the result.
call-seq:
negate(output = nil) -> output or new vec2
1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 |
# File 'ext/snow-math/snow-math.c', line 1604
static VALUE sm_vec2_negate(int argc, VALUE *argv, VALUE sm_self)
{
VALUE sm_out;
vec2_t *self;
rb_scan_args(argc, argv, "01", &sm_out);
self = sm_unwrap_vec2(sm_self, NULL);
if (argc == 1) {
if (!RTEST(sm_out)) {
goto SM_LABEL(skip_output);
}{
vec2_t *output;
if (!SM_IS_A(sm_out, vec2) && !SM_IS_A(sm_out, vec2) && !SM_IS_A(sm_out, vec4) && !SM_IS_A(sm_out, quat)) {
rb_raise(rb_eTypeError,
kSM_WANT_TWO_TO_FOUR_FORMAT_LIT,
rb_obj_classname(sm_out));
return Qnil;
}
rb_check_frozen(sm_out);
output = sm_unwrap_vec2(sm_out, NULL);
vec2_negate (*self, *output);
}} else if (argc == 0) {
SM_LABEL(skip_output): {
vec2_t output;
vec2_negate (*self, output);
sm_out = sm_wrap_vec2(output, rb_obj_class(sm_self));
rb_obj_call_init(sm_out, 0, 0);
}} else {
rb_raise(rb_eArgError, "Invalid number of arguments to negate");
}
return sm_out;
}
|
#negate! ⇒ Object
Calls #negate(self)
call-seq: negate! -> self
107 108 109 |
# File 'lib/snow-math/vec2.rb', line 107 def negate! negate self end |
#normalize(*args) ⇒ Object
Returns a vector whose components are the multiplicative inverse of this vector’s.
call-seq:
normalize(output = nil) -> output or new vec2
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 |
# File 'ext/snow-math/snow-math.c', line 1523
static VALUE sm_vec2_normalize(int argc, VALUE *argv, VALUE sm_self)
{
VALUE sm_out;
vec2_t *self;
rb_scan_args(argc, argv, "01", &sm_out);
self = sm_unwrap_vec2(sm_self, NULL);
if (argc == 1) {
if (!RTEST(sm_out)) {
goto SM_LABEL(skip_output);
}{
vec2_t *output;
if (!SM_IS_A(sm_out, vec2) && !SM_IS_A(sm_out, vec3) && !SM_IS_A(sm_out, vec4) && !SM_IS_A(sm_out, quat)) {
rb_raise(rb_eTypeError,
kSM_WANT_TWO_TO_FOUR_FORMAT_LIT,
rb_obj_classname(sm_out));
return Qnil;
}
rb_check_frozen(sm_out);
output = sm_unwrap_vec2(sm_out, NULL);
vec2_normalize (*self, *output);
}} else if (argc == 0) {
SM_LABEL(skip_output): {
vec2_t output;
vec2_normalize (*self, output);
sm_out = sm_wrap_vec2(output, rb_obj_class(sm_self));
rb_obj_call_init(sm_out, 0, 0);
}} else {
rb_raise(rb_eArgError, "Invalid number of arguments to normalize");
}
return sm_out;
}
|
#normalize! ⇒ Object
Calls #normalize(self)
call-seq: normalize! -> self
93 94 95 |
# File 'lib/snow-math/vec2.rb', line 93 def normalize! normalize self end |
#project(*args) ⇒ Object
Projects this vector onto a normal vector and returns the result.
call-seq:
project(normal, output = nil) -> output or new vec2
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 |
# File 'ext/snow-math/snow-math.c', line 1644
static VALUE sm_vec2_project(int argc, VALUE *argv, VALUE sm_self)
{
VALUE sm_rhs;
VALUE sm_out;
vec2_t *self;
vec2_t *rhs;
rb_scan_args(argc, argv, "11", &sm_rhs, &sm_out);
self = sm_unwrap_vec2(sm_self, NULL);
if (!SM_IS_A(sm_rhs, vec2) && !SM_IS_A(sm_rhs, vec3) && !SM_IS_A(sm_rhs, vec4) && !SM_IS_A(sm_rhs, quat)) {
rb_raise(rb_eTypeError,
kSM_WANT_TWO_TO_FOUR_FORMAT_LIT,
rb_obj_classname(sm_rhs));
return Qnil;
}
rhs = sm_unwrap_vec2(sm_rhs, NULL);
if (argc == 2) {
if (!RTEST(sm_out)) {
goto SM_LABEL(skip_output);
}{
vec2_t *output;
if (!SM_IS_A(sm_out, vec2) && !SM_IS_A(sm_out, vec3) && !SM_IS_A(sm_out, vec4) && !SM_IS_A(sm_out, quat)) {
rb_raise(rb_eTypeError,
kSM_WANT_TWO_TO_FOUR_FORMAT_LIT,
rb_obj_classname(sm_out));
return Qnil;
}
rb_check_frozen(sm_out);
output = sm_unwrap_vec2(sm_out, NULL);
vec2_project(*self, *rhs, *output);
}} else if (argc == 1) {
SM_LABEL(skip_output): {
vec2_t output;
vec2_project(*self, *rhs, output);
sm_out = sm_wrap_vec2(output, rb_obj_class(sm_self));
rb_obj_call_init(sm_out, 0, 0);
}} else {
rb_raise(rb_eArgError, "Invalid number of arguments to project");
}
return sm_out;
}
|
#reflect(*args) ⇒ Object
Reflects this vector against a normal vector and returns the result.
call-seq:
reflect(normal, output = nil) -> output or new vec2
1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 |
# File 'ext/snow-math/snow-math.c', line 1693
static VALUE sm_vec2_reflect(int argc, VALUE *argv, VALUE sm_self)
{
VALUE sm_rhs;
VALUE sm_out;
vec2_t *self;
vec2_t *rhs;
rb_scan_args(argc, argv, "11", &sm_rhs, &sm_out);
self = sm_unwrap_vec2(sm_self, NULL);
if (!SM_IS_A(sm_rhs, vec2) && !SM_IS_A(sm_rhs, vec3) && !SM_IS_A(sm_rhs, vec4) && !SM_IS_A(sm_rhs, quat)) {
rb_raise(rb_eTypeError,
kSM_WANT_TWO_TO_FOUR_FORMAT_LIT,
rb_obj_classname(sm_rhs));
return Qnil;
}
rhs = sm_unwrap_vec2(sm_rhs, NULL);
if (argc == 2) {
if (!RTEST(sm_out)) {
goto SM_LABEL(skip_output);
}{
vec2_t *output;
if (!SM_IS_A(sm_out, vec2) && !SM_IS_A(sm_out, vec3) && !SM_IS_A(sm_out, vec4) && !SM_IS_A(sm_out, quat)) {
rb_raise(rb_eTypeError,
kSM_WANT_TWO_TO_FOUR_FORMAT_LIT,
rb_obj_classname(sm_out));
return Qnil;
}
rb_check_frozen(sm_out);
output = sm_unwrap_vec2(sm_out, NULL);
vec2_reflect(*self, *rhs, *output);
}} else if (argc == 1) {
SM_LABEL(skip_output): {
vec2_t output;
vec2_reflect(*self, *rhs, output);
sm_out = sm_wrap_vec2(output, rb_obj_class(sm_self));
rb_obj_call_init(sm_out, 0, 0);
}} else {
rb_raise(rb_eArgError, "Invalid number of arguments to reflect");
}
return sm_out;
}
|
#scale(*args) ⇒ Object
Scales this vector’s components by a scalar value and returns the result.
call-seq:
scale(scalar, output = nil) -> output or new vec2
2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 |
# File 'ext/snow-math/snow-math.c', line 2053
static VALUE sm_vec2_scale(int argc, VALUE *argv, VALUE sm_self)
{
VALUE sm_out;
VALUE sm_scalar;
s_float_t scalar;
vec2_t *self = sm_unwrap_vec2(sm_self, NULL);
rb_scan_args(argc, argv, "11", &sm_scalar, &sm_out);
scalar = NUM2DBL(sm_scalar);
if (SM_IS_A(sm_out, vec2) || SM_IS_A(sm_out, vec3) || SM_IS_A(sm_out, vec4) || SM_IS_A(sm_out, quat)) {
rb_check_frozen(sm_out);
vec2_scale(*self, scalar, *sm_unwrap_vec2(sm_out, NULL));
} else {
vec2_t out;
vec2_scale(*self, scalar, out);
sm_out = sm_wrap_vec2(out, rb_obj_class(sm_self));
rb_obj_call_init(sm_out, 0, 0);
}
return sm_out;
}
|
#scale!(rhs) ⇒ Object
Calls #scale(rhs, self)
call-seq: scale!(rhs) -> self
155 156 157 |
# File 'lib/snow-math/vec2.rb', line 155 def scale!(rhs) scale rhs, self end |
#set(*args) ⇒ Object
Sets the Vec2’s components.
call-seq:
set(x, y) -> vec2 with components [x, y]
set([x, y]) -> vec2 with components [x, y]
set(vec2) -> copy of vec2
set(vec3) -> vec2 with components [vec3.xy]
set(vec4) -> vec2 with components [vec4.xy]
set(quat) -> vec2 with components [quat.xy]
1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 |
# File 'ext/snow-math/snow-math.c', line 1946
static VALUE sm_vec2_init(int argc, VALUE *argv, VALUE sm_self)
{
vec2_t *self = sm_unwrap_vec2(sm_self, NULL);
size_t arr_index = 0;
rb_check_frozen(sm_self);
switch(argc) {
/* Default value */
case 0: { break; }
/* Copy or by-array */
case 1: {
if (SM_IS_A(argv[0], vec2) ||
SM_IS_A(argv[0], vec3) ||
SM_IS_A(argv[0], vec4) ||
SM_IS_A(argv[0], quat)) {
sm_unwrap_vec2(argv[0], *self);
break;
}
/* Optional offset into array provided */
if (0) {
case 2:
if (!SM_RB_IS_A(argv[0], rb_cArray)) {
self[0][0] = (s_float_t)NUM2DBL(argv[0]);
self[0][1] = (s_float_t)NUM2DBL(argv[1]);
break;
}
arr_index = NUM2SIZET(argv[1]);
}
/* Array of values */
VALUE arrdata = argv[0];
const size_t arr_end = arr_index + 2;
s_float_t *vec_elem = *self;
for (; arr_index < arr_end; ++arr_index, ++vec_elem) {
*vec_elem = (s_float_t)NUM2DBL(rb_ary_entry(arrdata, (long)arr_index));
}
break;
}
default: {
rb_raise(rb_eArgError, "Invalid arguments to initialize/set");
break;
}
} /* switch (argc) */
return sm_self;
}
|
#size ⇒ Object
Returns the length in bytes of the Vec2. When compiled to use doubles as the base type, this is always 16. Otherwise, when compiled to use floats, it’s always 8.
call-seq: size -> fixnum
1457 1458 1459 1460 |
# File 'ext/snow-math/snow-math.c', line 1457
static VALUE sm_vec2_size (VALUE self)
{
return SIZET2NUM(sizeof(vec2_t));
}
|
#store ⇒ Object Also known as: []=
Sets the Vec2’s component at the index to the value.
call-seq: store(index, value) -> value
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 |
# File 'ext/snow-math/snow-math.c', line 1434
static VALUE sm_vec2_store (VALUE sm_self, VALUE sm_index, VALUE sm_value)
{
static const int max_index = sizeof(vec2_t) / sizeof(s_float_t);
vec2_t *self = sm_unwrap_vec2(sm_self, NULL);
int index = NUM2INT(sm_index);
rb_check_frozen(sm_self);
if (index < 0 || index >= max_index) {
rb_raise(rb_eRangeError,
"Index %d is out of bounds, must be from 0 through %d", index, max_index - 1);
}
self[0][index] = (s_float_t)NUM2DBL(sm_value);
return sm_value;
}
|
#subtract(*args) ⇒ Object Also known as: -
Subtracts another vector’s components from this vector’s and returns the result.
call-seq:
subtract(vec2, output = nil) -> output or new vec2
1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 |
# File 'ext/snow-math/snow-math.c', line 1842
static VALUE sm_vec2_subtract(int argc, VALUE *argv, VALUE sm_self)
{
VALUE sm_rhs;
VALUE sm_out;
vec2_t *self;
vec2_t *rhs;
rb_scan_args(argc, argv, "11", &sm_rhs, &sm_out);
self = sm_unwrap_vec2(sm_self, NULL);
if (!SM_IS_A(sm_rhs, vec2) && !SM_IS_A(sm_rhs, vec3) && !SM_IS_A(sm_rhs, vec4) && !SM_IS_A(sm_rhs, quat)) {
rb_raise(rb_eTypeError,
kSM_WANT_TWO_TO_FOUR_FORMAT_LIT,
rb_obj_classname(sm_rhs));
return Qnil;
}
rhs = sm_unwrap_vec2(sm_rhs, NULL);
if (argc == 2) {
if (!RTEST(sm_out)) {
goto SM_LABEL(skip_output);
}{
vec2_t *output;
if (!SM_IS_A(sm_out, vec2) && !SM_IS_A(sm_out, vec3) && !SM_IS_A(sm_out, vec4) && !SM_IS_A(sm_out, quat)) {
rb_raise(rb_eTypeError,
kSM_WANT_TWO_TO_FOUR_FORMAT_LIT,
rb_obj_classname(sm_out));
return Qnil;
}
rb_check_frozen(sm_out);
output = sm_unwrap_vec2(sm_out, NULL);
vec2_subtract(*self, *rhs, *output);
}} else if (argc == 1) {
SM_LABEL(skip_output): {
vec2_t output;
vec2_subtract(*self, *rhs, output);
sm_out = sm_wrap_vec2(output, rb_obj_class(sm_self));
rb_obj_call_init(sm_out, 0, 0);
}} else {
rb_raise(rb_eArgError, "Invalid number of arguments to subtract");
}
return sm_out;
}
|
#subtract!(rhs) ⇒ Object
Calls #subtract(rhs, self)
call-seq: subtract!(rhs) -> self
148 149 150 |
# File 'lib/snow-math/vec2.rb', line 148 def subtract!(rhs) subtract rhs, self end |
#to_s ⇒ Object
2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 |
# File 'ext/snow-math/snow-math.c', line 2008
static VALUE sm_vec2_to_s(VALUE self)
{
const s_float_t *v;
v = (const s_float_t *)*sm_unwrap_vec2(self, NULL);
return rb_sprintf(
"{ "
"%f, %f"
" }",
v[0], v[1]);
}
|
#x ⇒ Object
Returns the X component of the vector.
call-seq: x -> float
65 66 67 |
# File 'lib/snow-math/vec2.rb', line 65 def x self[0] end |
#x=(value) ⇒ Object
Sets the X component of the vector.
call-seq: x = value -> value
72 73 74 |
# File 'lib/snow-math/vec2.rb', line 72 def x=(value) self[0] = value end |
#y ⇒ Object
Returns the Y component of the vector.
call-seq: y -> float
79 80 81 |
# File 'lib/snow-math/vec2.rb', line 79 def y self[1] end |
#y=(value) ⇒ Object
Sets the Y component of the vector.
call-seq: y = value -> value
86 87 88 |
# File 'lib/snow-math/vec2.rb', line 86 def y=(value) self[1] = value end |