* Copyright 2001-2004, Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Stefano Ceccherini, burton666@libero.it
*/
#ifndef __CLIPPING_H
#define __CLIPPING_H
#include <Region.h>
#include <SupportDefs.h>
basically you can do almost everything you do with
BRects, just that clipping_rects can only have integer
coordinates (a thing that makes these perfect for drawing
calculations).
*/
static inline clipping_rect
union_rect(const clipping_rect &r1, const clipping_rect &r2)
{
clipping_rect rect;
rect.left = min_c(r1.left, r2.left);
rect.top = min_c(r1.top, r2.top);
rect.right = max_c(r1.right, r2.right);
rect.bottom = max_c(r1.bottom, r2.bottom);
return rect;
}
static inline clipping_rect
sect_rect(const clipping_rect &r1, const clipping_rect &r2)
{
clipping_rect rect;
rect.left = max_c(r1.left, r2.left);
rect.top = max_c(r1.top, r2.top);
rect.right = min_c(r1.right, r2.right);
rect.bottom = min_c(r1.bottom, r2.bottom);
return rect;
}
static inline void
offset_rect(clipping_rect &rect, int32 x, int32 y)
{
rect.left += x;
rect.top += y;
rect.right += x;
rect.bottom += y;
}
static inline void
scale_rect(clipping_rect& rect, float x, float y)
{
rect.left = (int)(rect.left * x);
rect.top = (int)(rect.top * y);
rect.right = (int)((rect.right + 1) * x) - 1;
rect.bottom = (int)((rect.bottom + 1) * y) - 1;
}
static inline BRect
to_BRect(const clipping_rect &rect)
{
return BRect((float)rect.left, (float)rect.top,
(float)rect.right, (float)rect.bottom);
}
static inline clipping_rect
to_clipping_rect(const BRect &rect)
{
clipping_rect clipRect;
clipRect.left = (int32)rect.left;
clipRect.top = (int32)rect.top;
clipRect.right = (int32)rect.right;
clipRect.bottom = (int32)rect.bottom;
return clipRect;
}
static inline bool
point_in(const clipping_rect &rect, int32 px, int32 py)
{
if (px >= rect.left && px <= rect.right
&& py >= rect.top && py <= rect.bottom)
return true;
return false;
}
static inline bool
point_in(const clipping_rect &rect, const BPoint &pt)
{
if (pt.x >= rect.left && pt.x <= rect.right
&& pt.y >= rect.top && pt.y <= rect.bottom)
return true;
return false;
}
static inline bool
rect_contains(const clipping_rect &rect, const clipping_rect &testRect)
{
return rect.top <= testRect.top && rect.bottom >= testRect.bottom
&& rect.left <= testRect.left && rect.right >= testRect.right;
}
static inline bool
valid_rect(const clipping_rect &rect)
{
if (rect.left <= rect.right && rect.top <= rect.bottom)
return true;
return false;
}
static inline bool
rects_intersect(const clipping_rect &rectA, const clipping_rect &rectB)
{
if (!valid_rect(rectA) || !valid_rect(rectB))
return false;
return !(rectA.left > rectB.right || rectA.top > rectB.bottom
|| rectA.right < rectB.left || rectA.bottom < rectB.top);
}
static inline int32
rect_width(const clipping_rect &rect)
{
return rect.right - rect.left;
}
static inline int32
rect_height(const clipping_rect &rect)
{
return rect.bottom - rect.top;
}
#endif