#pragma region CPL License /* Nuclex Native Framework Copyright (C) 2002-2015 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #pragma endregion // CPL License #ifndef NUCLEX_GEOMETRY_AREAS_ROTATEDRECTANGLE2_H #define NUCLEX_GEOMETRY_AREAS_ROTATEDRECTANGLE2_H #include "../Point2.h" #include "../Vector2.h" #include "../Math.h" #include namespace Nuclex { namespace Geometry { namespace Areas { // ------------------------------------------------------------------------------------------- // /// 2D rectangle that has been rotated by an arbitrary angle template struct RotatedRectangle2 { /// Initializes a new rotated rectangle /// Coordinates of the rectangle's center /// Extents of the rectangle /// Angle by which the rectangle has been rotated public: RotatedRectangle2( const Point2 ¢er, const Vector2 extents, TScalar angle ) : Center(center), Extents(extents), Angle(angle) {} /// Returns a point that lies in the center of the rectangle /// The center of the rectangle public: Point2 GetCenter() const { return this->Center; } /// Calculates the area of the rectangle /// The area of the rectangle public: TScalar GetArea() const { return (this->Extents.X * 2) * (this->Extents.Y * 2); } /// Calculates the perimeter of the rectangle /// The rectangle's perimeter public: TScalar GetPerimeter() const { TScalar horizontal = (this->Extents.X * 4); TScalar vertical = (this->Extents.Y * 4); return horizontal + vertical; } /// Offsets the entire rectangle by the specified amount /// Amount the rectangle will be offset on the X axis /// Amount the rectangle will be offset on the Y axis public: void ShiftBy(TScalar amountX, TScalar amountY) { this->Center.X += amountX; this->Center.Y += amountY; } /// Offsets the entire rectangle by the specified amount /// Amount the rectangle will be offset public: void ShiftBy(const Vector2 &amount) { this->Center += amount; } /// Moves the rectangle to the specified location /// X coordinate the rectangle's center will end up at /// Y coordinate the rectangle's center will end up at public: void PlaceAt(TScalar x, TScalar y) { this->Center.X = x; this->Center.Y = y; } /// Moves the rectangle to the specified location /// Position the rectangle's center will end up at public: void PlaceAt(const Point2 &position) { this->Center = position; } /// Triggers an assertion if the rectangle is flipped on any axis public: void EnforceNotFlipped() const { using namespace std; assert((this->Extents.X >= 0) && "Rectangle must not be flipped on the X axis"); assert((this->Extents.Y >= 0) && "Rectangle must not be flipped on the Y axis"); } /// Coordinates of the rectangle's center public: Point2 Center; /// Extents of the rectangle public: Vector2 Extents; /// Angle, in radians, by which the rectangle has been rotated public: TScalar Angle; }; // ------------------------------------------------------------------------------------------- // }}} // namespace Nuclex::Geometry::Areas #endif // NUCLEX_GEOMETRY_AREAS_ROTATEDRECTANGLE2_H