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

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.



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.

Returns:

  • (true, false)

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

Returns bottom left corner.

Returns:



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

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

#bottom_rightRay::Vector2

Returns bottom right corner.

Returns:



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

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

#centerRay::Vector2

Returns:



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.

Returns:

  • (true, false)

    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.

Returns:

  • (true, false)

    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

Returns:

  • (Boolean)


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

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

#hashObject



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

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

#heightFloat Also known as: h

Returns height of the rect.

Returns:

  • (Float)

    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=

This method returns an undefined value.

Sets the height of the rect

Parameters:

  • val (Float)

    The new height 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_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).

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
# File 'lib/ray/rect.rb', line 23

def outside?(rect)
  !collide?(rect)
end

#posRay::Vector2

Returns The position of the rect.

Returns:



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

def pos
  Vector2[x, y]
end

#sizeRay::Vector2

Returns The size of the rect.

Returns:



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

def size
  Vector2[w, h]
end

#to_rectObject



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

def to_rect
  self
end

#top_leftRay::Vector2

Returns top left corner.

Returns:



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

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

#top_rightRay::Vector2

Returns top right corner.

Returns:



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

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



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=

This method returns an undefined value.

Changes the with of the rect

Parameters:

  • val (Float)

    The new width 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_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



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

This method returns an undefined value.

Sets the x position of the rect

Parameters:

  • val (Float)

    The new x position of the rect



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

#yFloat

Returns Y position of the rect.

Returns:

  • (Float)

    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

This method returns an undefined value.

Changes the y position of the rect.

Parameters:

  • val (Float)

    The new y position of the rect.



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