Class: Ray::Polygon

Inherits:
Drawable
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ray/polygon.rb,
ext/polygon.c

Defined Under Namespace

Classes: Point

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size = nil, &block) ⇒ Polygon

Returns a new instance of Polygon.

Examples:

polygon = Ray::Polygon.new

other_polygon = Ray::Polygon.new(10) do |point|
  point.pos   = [point.id, point.id]
  point.color = Ray::Color.new(point.id * 10, 0, 0)
end

Parameters:

  • size (Integer, nil) (defaults to: nil)

    Size of the polygon. If non nil, size points will be added to the polygon and then yielded.



59
60
61
62
63
64
# File 'lib/ray/polygon.rb', line 59

def initialize(size = nil, &block)
  if size
    resize size
    each(&block) if block
  end
end

Class Method Details

.circle(center, radius, color = Ray::Color.white, outline_width = 0) ⇒ Object

@param [Ray::Vector2] center The center of the circle

@param [Float] radius The radius of the circle
@param [Ray::Color] color The color of the circle

@param outline_width (see rectangle)
@param outline_color (see rectangle)

@return [Ray::Polygon] A circle

outline_color = Ray::Color.white)



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'ext/polygon.c', line 109

static
VALUE ray_polygon_circle(int argc, VALUE *argv, VALUE self) {
  VALUE rb_center = Qnil, rb_radius = Qnil, rb_color = Qnil,
    rb_outline_width = Qnil, rb_outline = Qnil;

  rb_scan_args(argc, argv, "23", &rb_center, &rb_radius, &rb_color,
               &rb_outline_width, &rb_outline);

  say_color color = NIL_P(rb_color) ? say_make_color(255, 255, 255, 255) :
    ray_rb2col(rb_color);

  say_polygon *poly = say_polygon_circle(ray_convert_to_vector2(rb_center),
                                         NUM2DBL(rb_radius),
                                         color);
  VALUE ret = Data_Wrap_Struct(self, NULL, say_polygon_free, poly);

  ray_polygon_make_outline(poly, rb_outline_width, rb_outline);

  return ret;
}

.ellipse(center, rx, ry, color = Ray::Color.white, outline_width = 0) ⇒ Object

@param [Ray::Vector2] center The center of the ellipse

@param [Float] rx Horizontal radius
@param [Float] ry Vertical radius
@param [Ray::Color] color The color of the ellipse

@param outline_width (see rectangle)
@param outline_color (see rectangle)

@return [Ray::Polygon] An ellipse

outline_color = Ray::Color.white)



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'ext/polygon.c', line 143

static
VALUE ray_polygon_ellipse(int argc, VALUE *argv, VALUE self) {
  VALUE rb_center = Qnil, rb_rx = Qnil, rb_ry = Qnil, rb_color = Qnil,
    rb_outline_width = Qnil, rb_outline = Qnil;

  rb_scan_args(argc, argv, "33", &rb_center, &rb_rx, &rb_ry, &rb_color,
               &rb_outline_width, &rb_outline);

  say_color color = NIL_P(rb_color) ? say_make_color(255, 255, 255, 255) :
    ray_rb2col(rb_color);

  say_polygon *poly = say_polygon_ellipse(ray_convert_to_vector2(rb_center),
                                          NUM2DBL(rb_rx), NUM2DBL(rb_ry),
                                          color);

  ray_polygon_make_outline(poly, rb_outline_width, rb_outline);

  return Data_Wrap_Struct(self, NULL, say_polygon_free, poly);
}

.line(first, last, width = 1, color = Ray::Color.white) ⇒ Ray::Polygon

Returns A line.

Parameters:

  • first (Vector2, #to_vector2)

    First point of the line

  • last (Vector2, #to_vector2)

    Last point of the line

  • width (Float) (defaults to: 1)

    Width of the line

  • color (Ray::Color) (defaults to: Ray::Color.white)

    Color of the line

Returns:



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'ext/polygon.c', line 54

static
VALUE ray_polygon_line(int argc, VALUE *argv, VALUE self) {
  VALUE first = Qnil, last = Qnil, width = Qnil, color = Qnil;
  rb_scan_args(argc, argv, "22", &first, &last, &width, &color);

  say_polygon *line = say_polygon_line(ray_convert_to_vector2(first),
                                       ray_convert_to_vector2(last),
                                       NIL_P(width) ? 1 : NUM2DBL(width),
                                       NIL_P(color) ?
                                       say_make_color(255, 255, 255, 255) :
                                       ray_rb2col(color));
  return Data_Wrap_Struct(self, NULL, say_polygon_free, line);
}

.rectangle(rect, color = Ray::Color.white, outline_width = 0) ⇒ Object

@param [Ray::Rect, #to_rect] rect Rectangle occupied by the polygon

@param [Ray::Color] color Color of the rectangle
@param [Float] outline_width Width of the outline
@param [Ray::Color] outline_color Color of the outline

@return [Ray::Polygon] A rectangle

outline_color = Ray::Color.white)



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'ext/polygon.c', line 78

static
VALUE ray_polygon_rectangle(int argc, VALUE *argv, VALUE self) {
  VALUE rb_rect = Qnil, rb_color = Qnil, outline_width = Qnil, outline = Qnil;
  rb_scan_args(argc, argv, "13", &rb_rect, &rb_color, &outline_width, &outline);

  say_rect rect   = ray_convert_to_rect(rb_rect);
  say_color color = NIL_P(rb_color) ? say_make_color(255, 255, 255, 255) :
    ray_rb2col(rb_color);

  say_polygon *poly = say_polygon_rectangle(say_make_vector2(rect.x, rect.y),
                                            say_make_vector2(rect.w, rect.h),
                                            color);
  VALUE ret = Data_Wrap_Struct(self, NULL, say_polygon_free, poly);

  ray_polygon_make_outline(poly, outline_width, outline);

  return ret;
}

Instance Method Details

#[](id) ⇒ Ray::Polygon::Point

Returns idth point of the polygon (id should be less than size).

Returns:



73
74
75
# File 'lib/ray/polygon.rb', line 73

def [](id)
  Point.new(self, id)
end

#add_point(pos, col = Ray::Color.white, outline = Ray::Color.white) ⇒ Object

Adds a point to the polygon

Parameters:

  • pos (Ray::Vector2)

    Position of the point

  • col (Ray::Color) (defaults to: Ray::Color.white)

    Color of the polygon at that point

  • outline (Ray::Color) (defaults to: Ray::Color.white)

    Color of the outline at that point



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'ext/polygon.c', line 179

static
VALUE ray_polygon_add_point(int argc, VALUE *argv, VALUE self) {
  say_polygon *poly = ray_rb2polygon(self);

  VALUE rb_pos = Qnil, rb_col = Qnil, rb_outline_col = Qnil;
  rb_scan_args(argc, argv, "12", &rb_pos, &rb_col, &rb_outline_col);

  say_vector2 pos = ray_convert_to_vector2(rb_pos);
  say_color col = NIL_P(rb_col) ? say_make_color(255, 255, 255, 255) :
    ray_rb2col(rb_col);
  say_color outline_col = NIL_P(rb_col) ? say_make_color(255, 255, 255, 255) :
    ray_rb2col(rb_outline_col);

  size_t index = say_polygon_get_size(poly);

  say_polygon_resize(poly, say_polygon_get_size(poly) + 1);
  say_polygon_set_pos_for(poly, index, pos);
  say_polygon_set_color_for(poly, index, col);
  say_polygon_set_outline_for(poly, index, outline_col);

  return self;
}

#color=(val) ⇒ Object

Sets a color to all the points of the shape.

Parameters:



374
375
376
377
378
# File 'ext/polygon.c', line 374

static
VALUE ray_polygon_set_color(VALUE self, VALUE val) {
  say_polygon_set_color(ray_rb2polygon(self), ray_rb2col(val));
  return val;
}

#color_of(id) ⇒ Ray::Color?

Returns Color of the idth point.

Parameters:

  • id (Integer)

    Id of the point

Returns:



284
285
286
287
288
289
290
291
292
# File 'ext/polygon.c', line 284

static
VALUE ray_polygon_color_of(VALUE self, VALUE rb_id) {
  say_polygon *poly = ray_rb2polygon(self);
  size_t id = NUM2ULONG(rb_id);

  if (id >= say_polygon_get_size(poly))
    return Qnil;
  return ray_col2rb(say_polygon_get_color_for(poly, id));
}

#each {|point| ... } ⇒ Object

Yield Parameters:



67
68
69
# File 'lib/ray/polygon.rb', line 67

def each
  0.upto(size - 1) { |n| yield self[n] }
end

#filled=(val) ⇒ Object

Enables or disable filling.

Parameters:

  • val (true, false)

    True to enable filling (default)



218
219
220
221
222
# File 'ext/polygon.c', line 218

static
VALUE ray_polygon_set_filled(VALUE self, VALUE val) {
  say_polygon_set_filled(ray_rb2polygon(self), RTEST(val));
  return val;
}

#filled?true, false

Returns True if filling is enabled.

Returns:

  • (true, false)

    True if filling is enabled



236
237
238
239
# File 'ext/polygon.c', line 236

static
VALUE ray_polygon_filled(VALUE self) {
  return say_polygon_filled(ray_rb2polygon(self)) ? Qtrue : Qfalse;
}

#initialize_copy(other) ⇒ Object



23
24
25
26
27
# File 'ext/polygon.c', line 23

static
VALUE ray_polygon_init_copy(VALUE self, VALUE other) {
  say_polygon_copy(ray_rb2polygon(self), ray_rb2polygon(other));
  return self;
}

#outline=(val) ⇒ Object

Sets an outline color to all the points of the shape.

Parameters:



385
386
387
388
389
# File 'ext/polygon.c', line 385

static
VALUE ray_polygon_set_outline(VALUE self, VALUE val) {
  say_polygon_set_outline_color(ray_rb2polygon(self), ray_rb2col(val));
  return val;
}

#outline_of(id) ⇒ Ray::Color?

Returns Color of the outline at the idth point.

Parameters:

  • id (Integer)

    Id of the point

Returns:

  • (Ray::Color, nil)

    Color of the outline at the idth point



299
300
301
302
303
304
305
306
307
# File 'ext/polygon.c', line 299

static
VALUE ray_polygon_outline_of(VALUE self, VALUE rb_id) {
  say_polygon *poly = ray_rb2polygon(self);
  size_t id = NUM2ULONG(rb_id);

  if (id >= say_polygon_get_size(poly))
    return Qnil;
  return ray_col2rb(say_polygon_get_outline_for(poly, id));
}

#outline_widthFloat

Returns The width of the outline.

Returns:

  • (Float)

    The width of the outline



248
249
250
251
# File 'ext/polygon.c', line 248

static
VALUE ray_polygon_outline_width(VALUE self) {
  return rb_float_new(say_polygon_get_outline(ray_rb2polygon(self)));
}

#outline_width=(val) ⇒ Object

Sets the width of the outline.

Parameters:

  • val (Float)

    New width.



258
259
260
261
262
# File 'ext/polygon.c', line 258

static
VALUE ray_polygon_set_outline_width(VALUE self, VALUE val) {
  say_polygon_set_outline(ray_rb2polygon(self), NUM2DBL(val));
  return val;
}

#outlined=(val) ⇒ Object

Enables or disable outline.

Parameters:

  • val (true, false)

    True to enable outline.



229
230
231
232
233
# File 'ext/polygon.c', line 229

static
VALUE ray_polygon_set_outlined(VALUE self, VALUE val) {
  say_polygon_set_outlined(ray_rb2polygon(self), RTEST(val));
  return val;
}

#outlined?true, false

Returns True if outline is enabled.

Returns:

  • (true, false)

    True if outline is enabled



242
243
244
245
# File 'ext/polygon.c', line 242

static
VALUE ray_polygon_outlined(VALUE self) {
  return say_polygon_outlined(ray_rb2polygon(self)) ? Qtrue : Qfalse;
}

#pos_of(id) ⇒ Ray::Vector2?

Returns Position of the idth point.

Parameters:

  • id (Integer)

    Id of the point

Returns:



269
270
271
272
273
274
275
276
277
# File 'ext/polygon.c', line 269

static
VALUE ray_polygon_pos_of(VALUE self, VALUE rb_id) {
  say_polygon *poly = ray_rb2polygon(self);
  size_t id = NUM2ULONG(rb_id);

  if (id >= say_polygon_get_size(poly))
    return Qnil;
  return ray_vector2_to_rb(say_polygon_get_pos_for(poly, id));
}

#resize(n) ⇒ Object

Changes the amount of points in the polygon.

Parameters:

  • n (Integer)

    New size



207
208
209
210
211
# File 'ext/polygon.c', line 207

static
VALUE ray_polygon_resize(VALUE self, VALUE size) {
  say_polygon_resize(ray_rb2polygon(self), NUM2ULONG(size));
  return self;
}

#set_color_of(id, val) ⇒ Object

Parameters:

  • id (Integer)

    Id of the point

  • val (Ray::Color)

    Color of the point



334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'ext/polygon.c', line 334

static
VALUE ray_polygon_set_color_of(VALUE self, VALUE rb_id, VALUE val) {
  say_polygon *poly = ray_rb2polygon(self);
  size_t id = NUM2ULONG(rb_id);

  if (id >= say_polygon_get_size(poly)) {
    rb_raise(rb_eArgError, "trying to change point %ld, when there are %ld points",
             id, say_polygon_get_size(poly));
  }

  say_polygon_set_color_for(poly, id, ray_rb2col(val));

  return val;
}

#set_outline_of(id, val) ⇒ Object

Parameters:

  • id (Integer)

    Id of the point

  • val (Ray::Color)

    Color of the outline of the idth point



354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'ext/polygon.c', line 354

static
VALUE ray_polygon_set_outline_of(VALUE self, VALUE rb_id, VALUE val) {
  say_polygon *poly = ray_rb2polygon(self);
  size_t id = NUM2ULONG(rb_id);

  if (id >= say_polygon_get_size(poly)) {
    rb_raise(rb_eArgError, "trying to change point %ld, when there are %ld points",
             id, say_polygon_get_size(poly));
  }

  say_polygon_set_outline_for(poly, id, ray_rb2col(val));

  return val;
}

#set_pos_of(id, val) ⇒ Object

Parameters:

  • id (Integer)

    Id of the point

  • val (Ray::Vector2)

    Position of the point



314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'ext/polygon.c', line 314

static
VALUE ray_polygon_set_pos_of(VALUE self, VALUE rb_id, VALUE val) {
  say_polygon *poly = ray_rb2polygon(self);
  size_t id = NUM2ULONG(rb_id);

  if (id >= say_polygon_get_size(poly)) {
    rb_raise(rb_eArgError, "trying to change point %ld, when there are %ld points",
             id, say_polygon_get_size(poly));
  }

  say_polygon_set_pos_for(poly, id, ray_convert_to_vector2(val));

  return val;
}

#sizeInteger

Returns The amount of points in the polygon.

Returns:

  • (Integer)

    The amount of points in the polygon



166
167
168
169
# File 'ext/polygon.c', line 166

static
VALUE ray_polygon_size(VALUE self) {
  return INT2FIX(say_polygon_get_size(ray_rb2polygon(self)));
}