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 an image, 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.
-
#bottom_left ⇒ Ray::Vector2
Bottom left corner.
-
#bottom_right ⇒ Ray::Vector2
Bottom right corner.
- #center ⇒ Ray::Vector2
-
#collide?(rect) ⇒ true, false
True if the receiver collides with the rect.
-
#contain?(p) ⇒ true, false
True if the receiver contians this point.
- #eql?(obj) ⇒ Boolean
- #hash ⇒ Object
-
#height ⇒ Float
(also: #h)
Height of the rect.
-
#h=(val) ⇒ void
(also: #h=)
Sets the height of the rect.
- #initialize(*args) ⇒ Object constructor
- #initialize_copy(other) ⇒ Object
-
#inside?(rect) ⇒ true, false
True if the receiver is inside the rect.
- #inspect ⇒ Object (also: #to_s)
-
#outside?(rect) ⇒ true, false
True if the receiver is outside the rect.
-
#pos ⇒ Ray::Vector2
The position of the rect.
-
#size ⇒ Ray::Vector2
The size of the rect.
- #to_rect ⇒ Object
-
#top_left ⇒ Ray::Vector2
Top left corner.
-
#top_right ⇒ Ray::Vector2
Top right corner.
-
#width ⇒ Float
(also: #w)
Width of the rect.
-
#w=(val) ⇒ void
(also: #w=)
Changes the with of the rect.
-
#x ⇒ Float
X position of the rect.
-
#x=(val) ⇒ void
Sets the x position of the rect.
-
#y ⇒ Float
Y position of the rect.
-
#y=(val) ⇒ void
Changes the y position of the rect.
Constructor Details
#initialize(x, y) ⇒ Object #initialize(x, y, w, h) ⇒ Object #initialize(hash) ⇒ Object
59 60 61 62 63 64 65 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 |
# File 'ext/rect.c', line 59
static
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 option :y");
}
if (!NIL_P(w) && NIL_P(h)) {
rb_raise(rb_eArgError, "missing option :height");
}
}
say_rect *rect;
Data_Get_Struct(self, say_rect, rect);
rect->x = NUM2DBL(x_or_hash);
rect->y = NUM2DBL(y);
if (!NIL_P(w)) {
rect->w = NUM2DBL(w);
rect->h = NUM2DBL(h);
}
else {
rect->w = 0;
rect->h = 0;
}
return Qnil;
}
|
Instance Method Details
#==(rect) ⇒ true, false
Returns True if the two rects are equal.
69 70 71 72 |
# File 'lib/ray/rect.rb', line 69 def ==(rect) return false unless rect.is_a? Rect x == rect.x && y == rect.y && w == rect.w && h == rect.h end |
#bottom_left ⇒ Ray::Vector2
Returns bottom left corner.
54 55 56 |
# File 'lib/ray/rect.rb', line 54 def bottom_left Ray::Vector2[x, y + h] end |
#bottom_right ⇒ Ray::Vector2
Returns bottom right corner.
59 60 61 |
# File 'lib/ray/rect.rb', line 59 def bottom_right Ray::Vector2[x + w, y + h] end |
#center ⇒ Ray::Vector2
64 65 66 |
# File 'lib/ray/rect.rb', line 64 def center Ray::Vector2[x + w / 2, y + h / 2] end |
#collide?(rect) ⇒ true, false
Returns True if the receiver collides with the rect.
32 33 34 |
# File 'lib/ray/rect.rb', line 32 def collide?(rect) !outside?(rect) end |
#contain?(p) ⇒ true, false
Returns True if the receiver contians this point.
37 38 39 40 41 |
# File 'lib/ray/rect.rb', line 37 def contain?(p) p = p.to_vector2 (p.x >= x) && (p.y >= y) && (p.x < x + w) && (p.y < y + h) end |
#eql?(obj) ⇒ Boolean
74 75 76 |
# File 'lib/ray/rect.rb', line 74 def eql?(obj) self.class == obj.class && self == obj end |
#hash ⇒ Object
78 79 80 |
# File 'lib/ray/rect.rb', line 78 def hash [x, y, w, h].hash end |
#height ⇒ Float Also known as: h
Returns height of the rect.
147 148 149 150 151 152 153 |
# File 'ext/rect.c', line 147
static
VALUE ray_rect_h(VALUE self) {
say_rect *rect;
Data_Get_Struct(self, say_rect, rect);
return rb_float_new(rect->h);
}
|
#h=(val) ⇒ void Also known as: h=
219 220 221 222 223 224 225 226 227 228 229 |
# File 'ext/rect.c', line 219
static
VALUE ray_rect_set_h(VALUE self, VALUE val) {
rb_check_frozen(self);
say_rect *rect;
Data_Get_Struct(self, say_rect, rect);
rect->h = NUM2DBL(val);
return val;
}
|
#initialize_copy(other) ⇒ Object
109 110 111 112 113 114 115 116 117 |
# File 'ext/rect.c', line 109
static
VALUE ray_init_rect_copy(VALUE self, VALUE other) {
say_rect *rect = NULL, *source = NULL;
Data_Get_Struct(self, say_rect, rect);
Data_Get_Struct(other, say_rect, source);
*rect = *source;
return self;
}
|
#inside?(rect) ⇒ true, false
Returns True if the receiver is inside the rect. (false if they just collide).
15 16 17 18 19 20 |
# File 'lib/ray/rect.rb', line 15 def inside?(rect) rect = rect.to_rect (x >= rect.x) && (y >= rect.y) && (x + w) <= (rect.x + rect.w) && (y + h) <= (rect.y + rect.h) end |
#inspect ⇒ Object Also known as: to_s
7 8 9 |
# File 'lib/ray/rect.rb', line 7 def inspect "(#{pos}, #{size})" end |
#outside?(rect) ⇒ true, false
Returns True if the receiver is outside the rect.
23 24 25 26 27 28 29 |
# File 'lib/ray/rect.rb', line 23 def outside?(rect) rect = rect.to_rect !rect.contain?(top_left) && !rect.contain?(bottom_left) && !rect.contain?(top_right) && !rect.contain?(bottom_right) end |
#pos ⇒ Ray::Vector2
Returns The position of the rect.
87 88 89 |
# File 'lib/ray/rect.rb', line 87 def pos Vector2[x, y] end |
#size ⇒ Ray::Vector2
Returns The size of the rect.
92 93 94 |
# File 'lib/ray/rect.rb', line 92 def size Vector2[w, h] end |
#to_rect ⇒ Object
82 83 84 |
# File 'lib/ray/rect.rb', line 82 def to_rect self end |
#top_left ⇒ Ray::Vector2
Returns top left corner.
44 45 46 |
# File 'lib/ray/rect.rb', line 44 def top_left Ray::Vector2[x, y] end |
#top_right ⇒ Ray::Vector2
Returns top right corner.
49 50 51 |
# File 'lib/ray/rect.rb', line 49 def top_right Ray::Vector2[x + w, y] end |
#width ⇒ Float Also known as: w
Returns width of the rect.
138 139 140 141 142 143 144 |
# File 'ext/rect.c', line 138
static
VALUE ray_rect_w(VALUE self) {
say_rect *rect;
Data_Get_Struct(self, say_rect, rect);
return rb_float_new(rect->w);
}
|
#w=(val) ⇒ void Also known as: w=
200 201 202 203 204 205 206 207 208 209 210 |
# File 'ext/rect.c', line 200
static
VALUE ray_rect_set_w(VALUE self, VALUE val) {
rb_check_frozen(self);
say_rect *rect;
Data_Get_Struct(self, say_rect, rect);
rect->w = NUM2DBL(val);
return val;
}
|
#x ⇒ Float
Returns X position of the rect.
120 121 122 123 124 125 126 |
# File 'ext/rect.c', line 120
static
VALUE ray_rect_x(VALUE self) {
say_rect *rect;
Data_Get_Struct(self, say_rect, rect);
return rb_float_new(rect->x);
}
|
#x=(val) ⇒ void
162 163 164 165 166 167 168 169 170 171 172 |
# File 'ext/rect.c', line 162
static
VALUE ray_rect_set_x(VALUE self, VALUE val) {
rb_check_frozen(self);
say_rect *rect;
Data_Get_Struct(self, say_rect, rect);
rect->x = NUM2DBL(val);
return val;
}
|
#y ⇒ Float
Returns Y position of the rect.
129 130 131 132 133 134 135 |
# File 'ext/rect.c', line 129
static
VALUE ray_rect_y(VALUE self) {
say_rect *rect;
Data_Get_Struct(self, say_rect, rect);
return rb_float_new(rect->y);
}
|
#y=(val) ⇒ void
181 182 183 184 185 186 187 188 189 190 191 |
# File 'ext/rect.c', line 181
static
VALUE ray_rect_set_y(VALUE self, VALUE val) {
rb_check_frozen(self);
say_rect *rect;
Data_Get_Struct(self, say_rect, rect);
rect->y = NUM2DBL(val);
return val;
}
|