// Copyright (C) 2002-2006 Nikolaus Gebhardt // This file is part of the "Irrlicht Engine". // For conditions of distribution and use, see copyright notice in irrlicht.h #pragma once #include "Position2D.h" #include "Dimension2D.h" namespace Irrlicht { namespace Core { /// /// Rectangle class. /// Mostly used by 2D GUI elements and for 2D drawing methods. /// It has 2 positions instead of position and dimension /// and a fast method for collision detection with other rectangles and points. /// public __value class Rect { public: Rect() : UpperLeftCorner(0,0), LowerRightCorner(0,0) { } Rect(int x, int y, int x2, int y2) : UpperLeftCorner(x,y), LowerRightCorner(x2,y2) {}; Rect(Position2D upperLeft, Position2D lowerRight) : UpperLeftCorner(upperLeft), LowerRightCorner(lowerRight) { } Rect(Position2D pos, Dimension2D size) : UpperLeftCorner(pos), LowerRightCorner(pos.X + size.Width, pos.Y + size.Height) { } /// /// Returns if a 2d point is within this rectangle. /// /// Position to test if it lies within this rectangle. /// Returns true if the position is within the rectangle, false if not. bool IsPointInside(Position2D pos) { return UpperLeftCorner.X <= pos.X && UpperLeftCorner.Y <= pos.Y && LowerRightCorner.X >= pos.X && LowerRightCorner.Y >= pos.Y; } /// /// Returns if the rectangle collides with an other rectangle. /// bool IsRectCollided(Rect other) { return (LowerRightCorner.Y > other.UpperLeftCorner.Y && UpperLeftCorner.Y < other.LowerRightCorner.Y && LowerRightCorner.X > other.UpperLeftCorner.X && UpperLeftCorner.X < other.LowerRightCorner.X); } /// /// Clips this rectangle with another one. /// void ClipAgainst(Rect other) { if (other.LowerRightCorner.X < LowerRightCorner.X) LowerRightCorner.X = other.LowerRightCorner.X; if (other.LowerRightCorner.Y < LowerRightCorner.Y) LowerRightCorner.Y = other.LowerRightCorner.Y; if (other.UpperLeftCorner.X > UpperLeftCorner.X) UpperLeftCorner.X = other.UpperLeftCorner.X; if (other.UpperLeftCorner.Y > UpperLeftCorner.Y) UpperLeftCorner.Y = other.UpperLeftCorner.Y; } /// /// Returns width of rectangle. /// __property int get_Width() { return LowerRightCorner.X - UpperLeftCorner.X; } /// /// Sets width of rectangle. /// __property void set_Width(int w) { LowerRightCorner.X = UpperLeftCorner.X + w; } /// /// Returns height of rectangle. /// __property int get_Height() { return LowerRightCorner.Y - UpperLeftCorner.Y; } /// /// Sets height of rectangle. /// __property void set_Height(int h) { LowerRightCorner.Y = UpperLeftCorner.Y + h; } /// /// If the lower right corner of the rect is smaller then the upper left, /// the points are swapped. /// void Repair() { if (LowerRightCorner.X < UpperLeftCorner.X) { System::Int32 t = LowerRightCorner.X; LowerRightCorner.X = UpperLeftCorner.X; UpperLeftCorner.X = t; } if (LowerRightCorner.Y < UpperLeftCorner.Y) { System::Int32 t = LowerRightCorner.Y; LowerRightCorner.Y = UpperLeftCorner.Y; UpperLeftCorner.Y = t; } } /// /// Returns if the rect is valid to draw. It could be invalid, if /// The UpperLeftCorner is lower or more right than the LowerRightCorner, /// or if the area described by the rect is 0. /// bool IsValid() { return ((LowerRightCorner.X - UpperLeftCorner.X) * (LowerRightCorner.Y - UpperLeftCorner.Y) >= 0); } /// /// Returns the center of the rectangle /// Position2D GetCenter() { return Position2D((UpperLeftCorner.X + LowerRightCorner.X) / 2, (UpperLeftCorner.Y + LowerRightCorner.Y) / 2); } Position2D UpperLeftCorner; Position2D LowerRightCorner; }; } }