Class: TG::Geometry::Rect

Inherits:
Object
  • Object
show all
Defined in:
ext/tg_geometry/tg_geometry_ext.c

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1207

static VALUE rb_tg_geometry_rect_initialize(int argc, VALUE *argv, VALUE self) {
    VALUE min_x_value;
    VALUE min_y_value;
    VALUE max_x_value;
    VALUE max_y_value;
    tg_rect_wrapper_t *rect_data;
    double min_x;
    double min_y;
    double max_x;
    double max_y;

    rb_check_arity(argc, 4, 4);
    rb_scan_args(argc, argv, "40", &min_x_value, &min_y_value, &max_x_value, &max_y_value);

    min_x = NUM2DBL(min_x_value);
    min_y = NUM2DBL(min_y_value);
    max_x = NUM2DBL(max_x_value);
    max_y = NUM2DBL(max_y_value);

    validate_rect_coordinates(min_x, min_y, max_x, max_y);

    TypedData_Get_Struct(self, tg_rect_wrapper_t, &tg_rect_type, rect_data);
    rect_data->min_x = min_x;
    rect_data->min_y = min_y;
    rect_data->max_x = max_x;
    rect_data->max_y = max_y;
    rect_data->initialized = true;

    rb_obj_freeze(self);
    return self;
}

Instance Method Details

#centerObject



1259
1260
1261
1262
1263
1264
1265
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1259

static VALUE rb_tg_geometry_rect_center(VALUE self) {
    tg_rect_wrapper_t *rect_data = get_rect_wrapper(self);
    double center_x = rect_data->min_x + ((rect_data->max_x - rect_data->min_x) / 2.0);
    double center_y = rect_data->min_y + ((rect_data->max_y - rect_data->min_y) / 2.0);

    return rb_ary_new_from_args(2, rb_float_new(center_x), rb_float_new(center_y));
}

#contains_point?(x_value, y_value) ⇒ Boolean

Returns:

  • (Boolean)


1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1274

static VALUE rb_tg_geometry_rect_contains_point_p(VALUE self, VALUE x_value, VALUE y_value) {
    tg_rect_wrapper_t *rect_data = get_rect_wrapper(self);
    double x = NUM2DBL(x_value);
    double y = NUM2DBL(y_value);

    check_finite_double(x, "x");
    check_finite_double(y, "y");

    return rect_contains_point_data(rect_data, x, y) ? Qtrue : Qfalse;
}

#expand_to_include(other) ⇒ Object



1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1285

static VALUE rb_tg_geometry_rect_expand_to_include(VALUE self, VALUE other) {
    tg_rect_wrapper_t *rect_data = get_rect_wrapper(self);
    tg_rect_wrapper_t *other_data = get_rect_wrapper(other);
    double min_x = rect_data->min_x < other_data->min_x ? rect_data->min_x : other_data->min_x;
    double min_y = rect_data->min_y < other_data->min_y ? rect_data->min_y : other_data->min_y;
    double max_x = rect_data->max_x > other_data->max_x ? rect_data->max_x : other_data->max_x;
    double max_y = rect_data->max_y > other_data->max_y ? rect_data->max_y : other_data->max_y;

    return rect_build(min_x, min_y, max_x, max_y);
}

#expand_to_include_point(x_value, y_value) ⇒ Object



1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1296

static VALUE rb_tg_geometry_rect_expand_to_include_point(VALUE self, VALUE x_value, VALUE y_value) {
    tg_rect_wrapper_t *rect_data = get_rect_wrapper(self);
    double x = NUM2DBL(x_value);
    double y = NUM2DBL(y_value);
    double min_x;
    double min_y;
    double max_x;
    double max_y;

    check_finite_double(x, "x");
    check_finite_double(y, "y");

    min_x = rect_data->min_x < x ? rect_data->min_x : x;
    min_y = rect_data->min_y < y ? rect_data->min_y : y;
    max_x = rect_data->max_x > x ? rect_data->max_x : x;
    max_y = rect_data->max_y > y ? rect_data->max_y : y;

    return rect_build(min_x, min_y, max_x, max_y);
}

#intersects?(other) ⇒ Boolean

Returns:

  • (Boolean)


1267
1268
1269
1270
1271
1272
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1267

static VALUE rb_tg_geometry_rect_intersects_p(VALUE self, VALUE other) {
    tg_rect_wrapper_t *rect_data = get_rect_wrapper(self);
    tg_rect_wrapper_t *other_data = get_rect_wrapper(other);

    return rect_intersects_rect_data(rect_data, other_data) ? Qtrue : Qfalse;
}

#max_xObject



1249
1250
1251
1252
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1249

static VALUE rb_tg_geometry_rect_max_x(VALUE self) {
    tg_rect_wrapper_t *rect_data = get_rect_wrapper(self);
    return rb_float_new(rect_data->max_x);
}

#max_yObject



1254
1255
1256
1257
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1254

static VALUE rb_tg_geometry_rect_max_y(VALUE self) {
    tg_rect_wrapper_t *rect_data = get_rect_wrapper(self);
    return rb_float_new(rect_data->max_y);
}

#min_xObject



1239
1240
1241
1242
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1239

static VALUE rb_tg_geometry_rect_min_x(VALUE self) {
    tg_rect_wrapper_t *rect_data = get_rect_wrapper(self);
    return rb_float_new(rect_data->min_x);
}

#min_yObject



1244
1245
1246
1247
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1244

static VALUE rb_tg_geometry_rect_min_y(VALUE self) {
    tg_rect_wrapper_t *rect_data = get_rect_wrapper(self);
    return rb_float_new(rect_data->min_y);
}