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
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'ext/rect.c', line 48
static
VALUE ray_init_rect(int argc, VALUE *argv, VALUE self) {
VALUE x, y, w, h;
rb_scan_args(argc, argv, "22", &x, &y, &w, &h);
say_rect *rect;
Data_Get_Struct(self, say_rect, rect);
rect->x = NUM2DBL(x);
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.
70 71 72 73 |
# File 'lib/ray/rect.rb', line 70 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.
55 56 57 |
# File 'lib/ray/rect.rb', line 55 def bottom_left Ray::Vector2[x, y + h] end |
#bottom_right ⇒ Ray::Vector2
Returns bottom right corner.
60 61 62 |
# File 'lib/ray/rect.rb', line 60 def bottom_right Ray::Vector2[x + w, y + h] end |
#center ⇒ Ray::Vector2
65 66 67 |
# File 'lib/ray/rect.rb', line 65 def center Ray::Vector2[x + w / 2, y + h / 2] end |
#collide?(rect) ⇒ true, false
Returns True if the receiver collides with the rect.
28 29 30 31 32 33 34 35 |
# File 'lib/ray/rect.rb', line 28 def collide?(rect) rect = rect.to_rect rect.x < x + width && x < rect.x + rect.width && rect.y < y + height && y < rect.y + rect.height end |
#contain?(p) ⇒ true, false
Returns True if the receiver contians this point.
38 39 40 41 42 |
# File 'lib/ray/rect.rb', line 38 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
75 76 77 |
# File 'lib/ray/rect.rb', line 75 def eql?(obj) self.class == obj.class && self == obj end |
#hash ⇒ Object
79 80 81 |
# File 'lib/ray/rect.rb', line 79 def hash [x, y, w, h].hash end |
#height ⇒ Float Also known as: h
Returns height of the rect.
109 110 111 112 113 114 115 |
# File 'ext/rect.c', line 109
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=
181 182 183 184 185 186 187 188 189 190 191 |
# File 'ext/rect.c', line 181
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
71 72 73 74 75 76 77 78 79 |
# File 'ext/rect.c', line 71
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 |
# File 'lib/ray/rect.rb', line 23 def outside?(rect) !collide?(rect) end |
#pos ⇒ Ray::Vector2
Returns The position of the rect.
88 89 90 |
# File 'lib/ray/rect.rb', line 88 def pos Vector2[x, y] end |
#size ⇒ Ray::Vector2
Returns The size of the rect.
93 94 95 |
# File 'lib/ray/rect.rb', line 93 def size Vector2[w, h] end |
#to_rect ⇒ Object
83 84 85 |
# File 'lib/ray/rect.rb', line 83 def to_rect self end |
#top_left ⇒ Ray::Vector2
Returns top left corner.
45 46 47 |
# File 'lib/ray/rect.rb', line 45 def top_left Ray::Vector2[x, y] end |
#top_right ⇒ Ray::Vector2
Returns top right corner.
50 51 52 |
# File 'lib/ray/rect.rb', line 50 def top_right Ray::Vector2[x + w, y] end |
#width ⇒ Float Also known as: w
Returns width of the rect.
100 101 102 103 104 105 106 |
# File 'ext/rect.c', line 100
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=
162 163 164 165 166 167 168 169 170 171 172 |
# File 'ext/rect.c', line 162
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.
82 83 84 85 86 87 88 |
# File 'ext/rect.c', line 82
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
124 125 126 127 128 129 130 131 132 133 134 |
# File 'ext/rect.c', line 124
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.
91 92 93 94 95 96 97 |
# File 'ext/rect.c', line 91
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
143 144 145 146 147 148 149 150 151 152 153 |
# File 'ext/rect.c', line 143
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;
}
|