In Files

Parent

Imlib2::Polygon

Public Class Methods

new(*args) click to toggle source

Return a new Imlib2::Polygon

Examples:

poly = Imlib2::Polygon.new

points = [[10, 10], [20, 30], [15, 8], [6, 3], [12, 2]]
poly = Imlib2::Polygon.new *points
VALUE poly_new(int argc, VALUE *argv, VALUE klass) {
  ImlibPolygon *poly;
  VALUE p_o;

  poly = malloc(sizeof(ImlibPolygon*));
  *poly = imlib_polygon_new();

  p_o = Data_Wrap_Struct(klass, 0, poly_free, poly);
  rb_obj_call_init(p_o, argc, argv);

  return p_o;
}

Public Instance Methods

add_point(*args) click to toggle source

Add a point to the polygon

Example:

poly.add_point 123, 456
static VALUE poly_add_point(int argc, VALUE *argv, VALUE self) {
  ImlibPolygon *poly;
  int x, y;

  switch (argc) {
    case 1:
      switch (TYPE(argv[0])) {
        case T_HASH:
          x = NUM2INT(rb_hash_aref(argv[0], rb_str_new2("x")));
          y = NUM2INT(rb_hash_aref(argv[0], rb_str_new2("y")));
          break;
        case T_ARRAY:
          x = NUM2INT(rb_ary_entry(argv[0], 0));
          y = NUM2INT(rb_ary_entry(argv[0], 1));
          break;
        default:
          rb_raise(rb_eTypeError, "Invalid argument count (not 2 or 3)");
      }
      break;
    case 2:
      x = NUM2INT(argv[0]);
      y = NUM2INT(argv[1]);
      break;
    default:
      rb_raise(rb_eTypeError, "Invalid argument count (not 2 or 3)");
  }
  
  Data_Get_Struct(self, ImlibPolygon, poly);
  imlib_polygon_add_point(*poly, x, y);
    
  return self;
}
bounds() click to toggle source

Return the bounding rectangle of the given polygon.

Example:

bounds = poly.bounds
%q(x y w h).each_index { |i, v| puts v << ' = ' << bounds[i] }
static VALUE poly_bounds(VALUE self) {
  ImlibPolygon *poly;
  VALUE ary;
  int i, r[4] = { 0, 0, 0, 0 };

  Data_Get_Struct(self, ImlibPolygon, poly);
  imlib_polygon_get_bounds(*poly, &r[0], &r[1], &r[2], &r[3]);

  ary = rb_ary_new();
  for (i = 0; i < 4; i++)
    rb_ary_push(ary, INT2FIX(r[i]));

  return ary;
}
contains?(*args) click to toggle source

Does the given point lie within the polygon?

Example:

if poly.contains? 12, 5
  puts 'yes'
else
  puts 'no'
end
static VALUE poly_contains(int argc, char *argv, VALUE self) {
  ImlibPolygon *poly;
  int x, y;

  switch (argc) {
    case 1:
      switch (TYPE(argv[0])) {
        case T_HASH:
          x = NUM2INT(rb_hash_aref(argv[0], rb_str_new2("x")));
          y = NUM2INT(rb_hash_aref(argv[0], rb_str_new2("y")));
          break;
        case T_ARRAY:
          x = NUM2INT(rb_ary_entry(argv[0], 0));
          y = NUM2INT(rb_ary_entry(argv[0], 1));
          break;
        default:
          rb_raise(rb_eTypeError, "Invalid argument type (not array or hash)");
      }
      break;
    case 2:
      x = NUM2INT(argv[0]);
      y = NUM2INT(argv[1]);
      break;
    default:
      rb_raise(rb_eTypeError, "Invalid argument count (not 2 or 3)");
  }
  
  Data_Get_Struct(self, ImlibPolygon, poly);
  return imlib_polygon_contains_point(*poly, x, y) ? Qtrue : Qfalse;
}
contains_point?(*args) click to toggle source

Does the given point lie within the polygon?

Example:

if poly.contains? 12, 5
  puts 'yes'
else
  puts 'no'
end
static VALUE poly_contains(int argc, char *argv, VALUE self) {
  ImlibPolygon *poly;
  int x, y;

  switch (argc) {
    case 1:
      switch (TYPE(argv[0])) {
        case T_HASH:
          x = NUM2INT(rb_hash_aref(argv[0], rb_str_new2("x")));
          y = NUM2INT(rb_hash_aref(argv[0], rb_str_new2("y")));
          break;
        case T_ARRAY:
          x = NUM2INT(rb_ary_entry(argv[0], 0));
          y = NUM2INT(rb_ary_entry(argv[0], 1));
          break;
        default:
          rb_raise(rb_eTypeError, "Invalid argument type (not array or hash)");
      }
      break;
    case 2:
      x = NUM2INT(argv[0]);
      y = NUM2INT(argv[1]);
      break;
    default:
      rb_raise(rb_eTypeError, "Invalid argument count (not 2 or 3)");
  }
  
  Data_Get_Struct(self, ImlibPolygon, poly);
  return imlib_polygon_contains_point(*poly, x, y) ? Qtrue : Qfalse;
}
get_bounds() click to toggle source

Return the bounding rectangle of the given polygon.

Example:

bounds = poly.bounds
%q(x y w h).each_index { |i, v| puts v << ' = ' << bounds[i] }
static VALUE poly_bounds(VALUE self) {
  ImlibPolygon *poly;
  VALUE ary;
  int i, r[4] = { 0, 0, 0, 0 };

  Data_Get_Struct(self, ImlibPolygon, poly);
  imlib_polygon_get_bounds(*poly, &r[0], &r[1], &r[2], &r[3]);

  ary = rb_ary_new();
  for (i = 0; i < 4; i++)
    rb_ary_push(ary, INT2FIX(r[i]));

  return ary;
}

[Validate]

Generated with the Darkfish Rdoc Generator 2.