Main Page Class Hierarchy Compound List File List Compound Members File Members

# rectangle.h

Go to the documentation of this file.

    00001
    00002
    00003 //
    00004 //      RECTANGLE.H                                     (c)     YoY'01                                          WEB: www.aestesis.org
    00005 //
    00008 #ifndef                                                 _RECTANGLE_H_
    00009 #define                                                 _RECTANGLE_H_
    00012 #include                                                <stdio.h>
    00013 #include                                                "types.h"
    00016
    00017 //      If you use directly the variables in the class API, it's at your own risks. Prefer the methods...
    00018
    00021
    00022 class Arectangle
    00023 {
    00024 public:
    00025                                                                 Arectangle                                              () { x=0; y=0; w=0; h=0; }
    00026                                                                 Arectangle                                              (Arectangle &r);
    00027                                                                 Arectangle                                              (int x, int y, int w, int h);
    00028                                                                 Arectangle                                              (int x, int y);
    00029
    00030         void                                            intersect                                               (Arectangle &r);
    00031         void                                            add                                                             (Arectangle &r);
    00032         void                                            add                                                             (int x, int y);
    00033         void                                            translat                                                (int x, int y);
    00034         bool                                            contains                                                (int x, int y);
    00035
    00036         // private
    00037
    00038         int                                                     x;
    00039         int                                                     y;
    00040         int                                                     w;
    00041         int                                                     h;
    00042 };
    00043
    00046
    00047 __inline Arectangle::Arectangle(Arectangle &r)
    00048 {
    00049         x=r.x;
    00050         y=r.y;
    00051         w=r.w;
    00052         h=r.h;
    00053 }
    00054
    00055 __inline Arectangle::Arectangle(int x, int y, int w, int h)
    00056 {
    00057         this->x=x;
    00058         this->y=y;
    00059         this->w=w;
    00060         this->h=h;
    00061 }
    00062
    00063 __inline Arectangle::Arectangle(int x, int y)
    00064 {
    00065         this->x=x;
    00066         this->y=y;
    00067         this->w=1;
    00068         this->h=1;
    00069 }
    00070
    00071 __inline void Arectangle::intersect(Arectangle &r)
    00072 {
    00073         int xx=maxi(r.x, x);
    00074         int yy=max(r.y, y);
    00075         w=maxi(mini(r.x+r.w, x+w)-xx, 0);
    00076         h=maxi(mini(r.y+r.h, y+h)-yy, 0);
    00077         x=xx;
    00078         y=yy;
    00079 }
    00080
    00081 __inline void Arectangle::translat(int x, int y)
    00082 {
    00083         this->x+=x;
    00084         this->y+=y;
    00085 }
    00086
    00087 __inline void Arectangle::add(Arectangle &r)
    00088 {
    00089         int xx=mini(r.x, x);
    00090         int yy=mini(r.y, y);
    00091         w=maxi(r.x+r.w, x+w)-xx;
    00092         h=maxi(r.y+r.h, y+h)-yy;
    00093         x=xx;
    00094         y=yy;
    00095 }
    00096
    00097 __inline void Arectangle::add(int rx, int ry)
    00098 {
    00099         int xx=mini(rx, x);
    00100         int yy=mini(ry, y);
    00101         w=maxi(rx+1, x+w)-xx;
    00102         h=maxi(ry+1, y+h)-yy;
    00103         x=xx;
    00104         y=yy;
    00105 }
    00106
    00107 __inline bool Arectangle::contains(int x, int y)
    00108 {
    00109         return (x>=this->x)&&(x<(this->x+w))&&(y>=this->y)&&(y<(this->y+h));
    00110 }
    00111
    00114 #endif                                                  //_RECTANGLE_H_