Class: Ray::Rect

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(x, y) ⇒ Object #initialize(x, y, w, h) ⇒ Object #initialize(hash) ⇒ Object

Overloads:

  • #initialize(x, y) ⇒ Object

    Creates a new rect with size set to 0, 0.

  • #initialize(x, y, w, h) ⇒ Object

    Creates a new rect with the specified size.

  • #initialize(hash) ⇒ Object

    Creates a new rect according to the keys specified in hash.

    Options Hash (hash):

    • :x (Integer)
    • :y (Integer)
    • :width (Integer, optional)
    • :height (Integer, optional)

      required if width is set



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.

Returns:

  • (true, false)

    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.

Returns:

  • (true, false)

    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.

Returns:

  • (true, false)

    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

#heightInteger Also known as: h

Returns size of the rect.

Returns:

  • (Integer)

    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=

Sets the size of the rect



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).

Returns:

  • (true, false)

    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

#inspectObject



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.

Returns:

  • (true, false)

    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_rectObject



40
41
42
# File 'lib/ray/rect.rb', line 40

def to_rect
  self
end

#widthInteger Also known as: w

Returns size of the rect.

Returns:

  • (Integer)

    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=

Sets the size of the rect



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;
}

#xInteger

Returns position of the rect.

Returns:

  • (Integer)

    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

Sets the position of the rect



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;
}

#yInteger

Returns position of the rect.

Returns:

  • (Integer)

    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

Sets the position of the rect



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;
}