Tiberian Technologies Scripts Reference Revision: 9000
Loading...
Searching...
No Matches
rect.h
1/* Renegade Scripts.dll
2 Copyright 2013 Tiberian Technologies
3
4 This file is part of the Renegade scripts.dll
5 The Renegade scripts.dll is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 2, or (at your option) any later
8 version. See the file COPYING for more details.
9 In addition, an exemption is given to allow Run Time Dynamic Linking of this code with any closed source module that does not contain code covered by this licence.
10 Only the source code to the module(s) containing the licenced code has to be released.
11*/
12#ifndef TT_INCLUDE__RECT_H
13#define TT_INCLUDE__RECT_H
14#include "Vector2.h"
15class RectClass {
16public:
17 float Left;
18 float Top;
19 float Right;
20 float Bottom;
21 RectClass( const RectClass & r ) { Left = r.Left; Top = r.Top; Right = r.Right; Bottom = r.Bottom; }
22 RectClass( const Vector2 & top_left, const Vector2 & bottom_right ) { Left = top_left.X; Top = top_left.Y; Right = bottom_right.X; Bottom = bottom_right.Y; }
23 RectClass& operator = (const RectClass & r) { Left = r.Left; Top = r.Top; Right = r.Right; Bottom = r.Bottom; return *this; }
24 void Set(float left, float top, float right, float bottom) { Left = left; Top = top; Right = right; Bottom = bottom; }
25 void Set( const Vector2 & top_left, const Vector2 & bottom_right ) { Left = top_left.X; Top = top_left.Y; Right = bottom_right.X; Bottom = bottom_right.Y; }
26 void Set(const RectClass & r) { Left = r.Left; Top = r.Top; Right = r.Right; Bottom = r.Bottom; }
27 Vector2 Upper_Right( void ) const { return Vector2( Right, Top ); }
28 Vector2 Lower_Left( void ) const { return Vector2( Left, Bottom ); }
29 float Width(void) const { return Right - Left; }
30 float Height(void) const { return Bottom - Top; }
31 Vector2 Center( void ) const { return Vector2( (Left + Right)/2, (Top + Bottom)/2 ); }
32 Vector2 Extent( void ) const { return Vector2( (Right - Left)/2, (Bottom - Top)/2 ); }
33 RectClass & operator *= (float k) { return Scale( k ); }
34 RectClass & operator /= (float k) { return Scale( 1/k ); }
35 RectClass & Scale_Relative_Center( float k ) { Vector2 center = Center(); *this-=center; Left*=k; Top*=k; Right*=k; Bottom*=k; *this+=center; return *this; }
36 RectClass & Scale( float k ) { Left*=k; Top*=k; Right*=k; Bottom*=k; return *this; }
37 RectClass & Scale( const Vector2 &k ) { Left*=k.X; Top*=k.Y; Right*=k.X; Bottom*=k.Y; return *this; }
38 RectClass & Inverse_Scale( const Vector2 &k ) { Left/=k.X; Top/=k.Y; Right/=k.X; Bottom/=k.Y; return *this; }
39 RectClass & operator += ( const Vector2 & o ) { Left+=o.X; Top+=o.Y; Right+=o.X; Bottom+=o.Y; return *this; }
40 RectClass & operator -= ( const Vector2 & o ) { Left-=o.X; Top-=o.Y; Right-=o.X; Bottom-=o.Y; return *this; }
41 void Inflate( const Vector2 & o ) { Left-=o.X; Top-=o.Y; Right+=o.X; Bottom+=o.Y; }
42 RectClass & operator += ( const RectClass & r ) { Left=min(Left,r.Left); Top=min(Top,r.Top); Right=max(Right,r.Right); Bottom=max(Bottom,r.Bottom); return *this; }
43 RectClass(float NewLeft,float NewTop,float NewRight,float NewBottom)
44 {
45 Left = NewLeft;
46 Top = NewTop;
47 Right = NewRight;
48 Bottom = NewBottom;
49 }
50 RectClass()
51 {
52 }
53 Vector2 Upper_Left() const
54 {
55 return Vector2(Left,Top);
56 }
57 Vector2 Lower_Right() const
58 {
59 return Vector2(Right,Bottom);
60 }
61 bool operator == ( const RectClass &rval ) const { return (rval.Left == Left) && (rval.Right == Right) && (rval.Top == Top) && (rval.Bottom == Bottom); }
62 bool operator != ( const RectClass &rval ) const { return (rval.Left != Left) || (rval.Right != Right) || (rval.Top != Top) || (rval.Bottom != Bottom); }
63 bool Contains ( const Vector2 &pos ) const { return (pos.X >= Left) && (pos.X <= Right) && (pos.Y >= Top) && (pos.Y <= Bottom); }
64 void Snap_To_Units( const Vector2 & u ) { Left = (int)(Left / u.X + 0.5f) * u.X; Right = (int)(Right / u.X + 0.5f) * u.X; Top = (int)(Top / u.Y + 0.5f) * u.Y; Bottom = (int)(Bottom / u.Y + 0.5f) * u.Y; }
65
66 Vector2 getSize() const { return Vector2(Right - Left, Bottom - Top); }
67};
68
69#endif