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 an image, 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 (Float)
    • :y (Float)
    • (0) (Float)

      :width

    • (0) (Float)

      :height required if width is set

    • (0) (Float)

      :w alias for :width

    • (0) (Float)

      :h alias for :height



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.

Returns:

  • (true, false)

    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_leftRay::Vector2

Returns bottom left corner.

Returns:



54
55
56
# File 'lib/ray/rect.rb', line 54

def bottom_left
  Ray::Vector2[x, y + h]
end

#bottom_rightRay::Vector2

Returns bottom right corner.

Returns:



59
60
61
# File 'lib/ray/rect.rb', line 59

def bottom_right
  Ray::Vector2[x + w, y + h]
end

#centerRay::Vector2

Returns:



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.

Returns:

  • (true, false)

    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.

Returns:

  • (true, false)

    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

Returns:

  • (Boolean)


74
75
76
# File 'lib/ray/rect.rb', line 74

def eql?(obj)
  self.class == obj.class && self == obj
end

#hashObject



78
79
80
# File 'lib/ray/rect.rb', line 78

def hash
  [x, y, w, h].hash
end

#heightFloat Also known as: h

Returns height of the rect.

Returns:

  • (Float)

    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=

This method returns an undefined value.

Sets the height of the rect

Parameters:

  • val (Float)

    The new height of the rect.



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

Returns:

  • (true, false)

    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

#inspectObject 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.

Returns:

  • (true, false)

    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

#posRay::Vector2

Returns The position of the rect.

Returns:



87
88
89
# File 'lib/ray/rect.rb', line 87

def pos
  Vector2[x, y]
end

#sizeRay::Vector2

Returns The size of the rect.

Returns:



92
93
94
# File 'lib/ray/rect.rb', line 92

def size
  Vector2[w, h]
end

#to_rectObject



82
83
84
# File 'lib/ray/rect.rb', line 82

def to_rect
  self
end

#top_leftRay::Vector2

Returns top left corner.

Returns:



44
45
46
# File 'lib/ray/rect.rb', line 44

def top_left
  Ray::Vector2[x, y]
end

#top_rightRay::Vector2

Returns top right corner.

Returns:



49
50
51
# File 'lib/ray/rect.rb', line 49

def top_right
  Ray::Vector2[x + w, y]
end

#widthFloat Also known as: w

Returns width of the rect.

Returns:

  • (Float)

    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=

This method returns an undefined value.

Changes the with of the rect

Parameters:

  • val (Float)

    The new width of the rect.



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

#xFloat

Returns X position of the rect.

Returns:

  • (Float)

    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

This method returns an undefined value.

Sets the x position of the rect

Parameters:

  • val (Float)

    The new x position of the rect



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

#yFloat

Returns Y position of the rect.

Returns:

  • (Float)

    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

This method returns an undefined value.

Changes the y position of the rect.

Parameters:

  • val (Float)

    The new y position of the rect.



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