Class: Ray::Rect
- Inherits:
-
Object
- Object
- Ray::Rect
- Defined in:
- ext/rect.c,
lib/ray/rect.rb,
ext/rect.c
Overview
Rects are used to represent a part of a surface, using two attributes to represent its position, and two others to represent its size.
Instance Method Summary collapse
-
#==(rect) ⇒ true, false
True if the two rects are equal.
-
#collide?(rect) ⇒ true, false
True if the receiver collides with the rect.
-
#contain?(p_x, p_y) ⇒ true, false
True if the receiver contians this point.
-
#height ⇒ Integer
(also: #h)
Size of the rect.
-
#h=(val) ⇒ Object
(also: #h=)
Sets the size of the rect.
- #initialize(*args) ⇒ Object constructor
-
#inside?(rect) ⇒ true, false
True if the receiver is inside the rect.
- #inspect ⇒ Object
-
#outside?(rect) ⇒ true, false
True if the receiver is outside the rect.
- #to_rect ⇒ Object
-
#width ⇒ Integer
(also: #w)
Size of the rect.
-
#w=(val) ⇒ Object
(also: #w=)
Sets the size of the rect.
-
#x ⇒ Integer
Position of the rect.
-
#x=(val) ⇒ Object
Sets the position of the rect.
-
#y ⇒ Integer
Position of the rect.
-
#y=(val) ⇒ Object
Sets the position of the rect.
Constructor Details
#initialize(x, y) ⇒ Object #initialize(x, y, w, h) ⇒ Object #initialize(hash) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'ext/rect.c', line 66
VALUE ray_init_rect(int argc, VALUE *argv, VALUE self) {
VALUE x_or_hash, y, w, h;
rb_scan_args(argc, argv, "13", &x_or_hash, &y, &w, &h);
int type = TYPE(x_or_hash);
if (type == T_HASH) {
if (!NIL_P(y)) {
rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)",
argc);
}
VALUE hash = x_or_hash;
x_or_hash = rb_hash_aref(hash, RAY_SYM("x"));
y = rb_hash_aref(hash, RAY_SYM("y"));
w = rb_hash_aref(hash, RAY_SYM("width"));
h = rb_hash_aref(hash, RAY_SYM("height"));
if (NIL_P(w)) w = rb_hash_aref(hash, RAY_SYM("w"));
if (NIL_P(h)) h = rb_hash_aref(hash, RAY_SYM("h"));
if (NIL_P(y)) {
rb_raise(rb_eArgError, "Missing argument 'y'");
}
if (!NIL_P(w) && NIL_P(h)) {
rb_raise(rb_eArgError, "missing argument 'height'");
}
}
else {
if (NIL_P(y)) {
rb_raise(rb_eArgError, "wrong number of arguments (1 for 2)");
}
if (!NIL_P(w) && NIL_P(h)) {
rb_raise(rb_eArgError, "wrong number of arguments (3 for 4)");
}
}
ray_rect *rect;
Data_Get_Struct(self, ray_rect, rect);
rect->x = NUM2INT(x_or_hash);
rect->y = NUM2INT(y);
if (!NIL_P(w)) {
rect->w = NUM2INT(w);
rect->h = NUM2INT(h);
}
else {
rect->w = 0;
rect->h = 0;
}
return Qnil;
}
|
Instance Method Details
#==(rect) ⇒ true, false
Returns True if the two rects are equal.
35 36 37 38 |
# File 'lib/ray/rect.rb', line 35 def ==(rect) return false unless rect.is_a? Rect x == rect.x && y == rect.y && w == rect.w && h == rect.h end |
#collide?(rect) ⇒ true, false
Returns True if the receiver collides with the rect.
24 25 26 |
# File 'lib/ray/rect.rb', line 24 def collide?(rect) !outside?(rect) end |
#contain?(p_x, p_y) ⇒ true, false
Returns True if the receiver contians this point.
29 30 31 32 |
# File 'lib/ray/rect.rb', line 29 def contain?(p_x, p_y) (p_x >= x) && (p_y >= y) && (p_x < x + w) && (p_y < y + h) end |
#height ⇒ Integer Also known as: h
Returns size of the rect.
149 150 151 152 153 154 |
# File 'ext/rect.c', line 149
VALUE ray_rect_h(VALUE self) {
ray_rect *rect;
Data_Get_Struct(self, ray_rect, rect);
return INT2FIX(rect->h);
}
|
#h=(val) ⇒ Object Also known as: h=
199 200 201 202 203 204 205 206 |
# File 'ext/rect.c', line 199
VALUE ray_rect_set_h(VALUE self, VALUE val) {
ray_rect *rect;
Data_Get_Struct(self, ray_rect, rect);
rect->h = NUM2INT(val);
return val;
}
|
#inside?(rect) ⇒ true, false
Returns True if the receiver is inside the rect. (false if they just collide).
9 10 11 12 13 |
# File 'lib/ray/rect.rb', line 9 def inside?(rect) (x >= rect.x) && (y >= rect.y) && (x + w) <= (rect.x + rect.w) && (y + h) <= (rect.y + rect.h) end |
#inspect ⇒ Object
3 4 5 |
# File 'lib/ray/rect.rb', line 3 def inspect "#<#{self.class} {{#{x}, #{y}}, {#{w}, #{h}}}>" end |
#outside?(rect) ⇒ true, false
Returns True if the receiver is outside the rect.
16 17 18 19 20 21 |
# File 'lib/ray/rect.rb', line 16 def outside?(rect) !rect.contain?(x, y) && !rect.contain?(x, y + h) && !rect.contain?(x + w, y) && !rect.contain?(x + w, y + h) end |
#to_rect ⇒ Object
40 41 42 |
# File 'lib/ray/rect.rb', line 40 def to_rect self end |
#width ⇒ Integer Also known as: w
Returns size of the rect.
141 142 143 144 145 146 |
# File 'ext/rect.c', line 141
VALUE ray_rect_w(VALUE self) {
ray_rect *rect;
Data_Get_Struct(self, ray_rect, rect);
return INT2FIX(rect->w);
}
|
#w=(val) ⇒ Object Also known as: w=
186 187 188 189 190 191 192 193 |
# File 'ext/rect.c', line 186
VALUE ray_rect_set_w(VALUE self, VALUE val) {
ray_rect *rect;
Data_Get_Struct(self, ray_rect, rect);
rect->w = NUM2INT(val);
return val;
}
|
#x ⇒ Integer
Returns position of the rect.
125 126 127 128 129 130 |
# File 'ext/rect.c', line 125
VALUE ray_rect_x(VALUE self) {
ray_rect *rect;
Data_Get_Struct(self, ray_rect, rect);
return INT2FIX(rect->x);
}
|
#x=(val) ⇒ Object
160 161 162 163 164 165 166 167 |
# File 'ext/rect.c', line 160
VALUE ray_rect_set_x(VALUE self, VALUE val) {
ray_rect *rect;
Data_Get_Struct(self, ray_rect, rect);
rect->x = NUM2INT(val);
return val;
}
|
#y ⇒ Integer
Returns position of the rect.
133 134 135 136 137 138 |
# File 'ext/rect.c', line 133
VALUE ray_rect_y(VALUE self) {
ray_rect *rect;
Data_Get_Struct(self, ray_rect, rect);
return INT2FIX(rect->y);
}
|
#y=(val) ⇒ Object
173 174 175 176 177 178 179 180 |
# File 'ext/rect.c', line 173
VALUE ray_rect_set_y(VALUE self, VALUE val) {
ray_rect *rect;
Data_Get_Struct(self, ray_rect, rect);
rect->y = NUM2INT(val);
return val;
}
|