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.



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

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)



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'ext/polygon.c', line 127

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

  say_drawable_set_shader_proc(poly->drawable, ray_drawable_shader_proc);
  say_drawable_set_other_data(poly->drawable, (void*)ret);
  rb_iv_set(ret, "@shader_attributes", Qnil);

  return ret;
}

.ellipse(center, rx, ry, color = Ray::Color.white) ⇒ 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_width = 0, outline_color = Ray::Color.white)



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'ext/polygon.c', line 165

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

  VALUE rb = Data_Wrap_Struct(self, NULL, say_polygon_free, poly);

  say_drawable_set_shader_proc(poly->drawable, ray_drawable_shader_proc);
  say_drawable_set_other_data(poly->drawable, (void*)rb);
  rb_iv_set(rb, "@shader_attributes", Qnil);

  return rb;
}

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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'ext/polygon.c', line 61

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));
  VALUE rb = Data_Wrap_Struct(self, NULL, say_polygon_free, line);

  say_drawable_set_shader_proc(line->drawable, ray_drawable_shader_proc);
  say_drawable_set_other_data(line->drawable, (void*)rb);
  rb_iv_set(rb, "@shader_attributes", Qnil);

  return rb;
}

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'ext/polygon.c', line 91

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

  say_drawable_set_shader_proc(poly->drawable, ray_drawable_shader_proc);
  say_drawable_set_other_data(poly->drawable, (void*)ret);
  rb_iv_set(ret, "@shader_attributes", Qnil);

  return ret;
}

Instance Method Details

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

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

Returns:



81
82
83
# File 'lib/ray/polygon.rb', line 81

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



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'ext/polygon.c', line 207

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:



402
403
404
405
406
# File 'ext/polygon.c', line 402

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:



312
313
314
315
316
317
318
319
320
# File 'ext/polygon.c', line 312

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:



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

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)



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

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



264
265
266
267
# File 'ext/polygon.c', line 264

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

#initialize_copy(other) ⇒ Object



29
30
31
32
33
34
# File 'ext/polygon.c', line 29

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

#outline=(val) ⇒ Object

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

Parameters:



413
414
415
416
417
# File 'ext/polygon.c', line 413

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



327
328
329
330
331
332
333
334
335
# File 'ext/polygon.c', line 327

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



276
277
278
279
# File 'ext/polygon.c', line 276

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.



286
287
288
289
290
# File 'ext/polygon.c', line 286

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.



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

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



270
271
272
273
# File 'ext/polygon.c', line 270

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:



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

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

#pretty_print(q, other_attr = []) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/ray/polygon.rb', line 85

def pretty_print(q, other_attr = [])
  attr = [
          "filled?", "outlined?", "outline_width",
          "points"
         ]

  super q, attr + other_attr
end

#resize(n) ⇒ Object

Changes the amount of points in the polygon.

Parameters:

  • n (Integer)

    New size



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

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



362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'ext/polygon.c', line 362

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



382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'ext/polygon.c', line 382

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



342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'ext/polygon.c', line 342

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



194
195
196
197
# File 'ext/polygon.c', line 194

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