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



1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1848

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



1900
1901
1902
1903
1904
1905
1906
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1900

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)


1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1915

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



1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1926

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



1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1937

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)


1908
1909
1910
1911
1912
1913
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1908

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



1890
1891
1892
1893
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1890

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



1895
1896
1897
1898
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1895

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



1880
1881
1882
1883
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1880

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



1885
1886
1887
1888
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1885

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