Пример #1
0
        public static RectBox GetIntersection(RectBox a, RectBox b)
        {
            float a_x = a.GetX();
            float a_r = a.GetRight();
            float a_y = a.GetY();
            float a_t = a.GetBottom();
            float b_x = b.GetX();
            float b_r = b.GetRight();
            float b_y = b.GetY();
            float b_t = b.GetBottom();
            float i_x = MathUtils.Max(a_x, b_x);
            float i_r = MathUtils.Min(a_r, b_r);
            float i_y = MathUtils.Max(a_y, b_y);
            float i_t = MathUtils.Min(a_t, b_t);

            return(i_x < i_r && i_y < i_t ? new RectBox(i_x, i_y, i_r - i_x, i_t - i_y) : null);
        }
Пример #2
0
        public static RectBox GetIntersection(RectBox a, RectBox b, RectBox result)
        {
            float a_x = a.GetX();
            float a_r = a.GetRight();
            float a_y = a.GetY();
            float a_t = a.GetBottom();
            float b_x = b.GetX();
            float b_r = b.GetRight();
            float b_y = b.GetY();
            float b_t = b.GetBottom();
            float i_x = MathUtils.Max(a_x, b_x);
            float i_r = MathUtils.Min(a_r, b_r);
            float i_y = MathUtils.Max(a_y, b_y);
            float i_t = MathUtils.Min(a_t, b_t);

            if (i_x < i_r && i_y < i_t)
            {
                result.SetBounds(i_x, i_y, i_r - i_x, i_t - i_y);
                return(result);
            }
            return(null);
        }
Пример #3
0
        public bool Intersects(RectBox rect)
        {
            if (rect == null)
            {
                return(false);
            }

            float x1 = start.GetX();
            float y1 = start.GetY();

            float x2 = end.GetX() + start.GetX();
            float y2 = end.GetY() + start.GetY();

            float bx1 = rect.x;
            float by1 = rect.y;
            float bx2 = rect.GetRight();
            float by2 = rect.GetBottom();

            float t = 0;

            if ((x1 >= bx1 && x1 <= bx2 && y1 >= by1 && y1 <= by2) || (x2 >= bx1 && x2 <= bx2 && y2 >= by1 && y2 <= by2))
            {
                return(true);
            }

            if (x1 < bx1 && x2 >= bx1)
            {
                t = y1 + (y2 - y1) * (bx1 - x1) / (x2 - x1);
                if (t > by1 && t <= by2)
                {
                    return(rect.Intersects(this));
                }
            }
            else if (x1 > bx2 && x2 <= bx2)
            {
                t = y1 + (y2 - y1) * (bx2 - x1) / (x2 - x1);
                if (t >= by1 && t <= by2)
                {
                    return(rect.Intersects(this));
                }
            }

            if (y1 < by1 && y2 >= by1)
            {
                t = x1 + (x2 - x1) * (by1 - y1) / (y2 - y1);
                if (t >= bx1 && t <= bx2)
                {
                    return(rect.Intersects(this));
                }
            }
            else if (y1 > by2 && y2 <= by2)
            {
                t = x1 + (x2 - x1) * (by2 - y1) / (y2 - y1);
                if (t >= bx1 && t <= bx2)
                {
                    return(rect.Intersects(this));
                }
            }

            return(false);
        }