• Immutable Page
  • Info
  • Attachments

CompRect

A CompRect describes a simple two dimensional rectangle on screen. Various information can be retrieved about it when it's geometry has been set. The implementation actually serves as a wrapper around an X11 Region, but only it's extents can be set and read.

Class Definition

/**
 * A 2D rectangle, which is likely in screen space. It's data is
 * isolated and can only be mutated with set() methods.
 */
class CompRect {

    public:
        CompRect ();
        CompRect (int x, int y, int width, int height);
        CompRect (const CompRect&);
        CompRect (const XRectangle);

        int x () const;
        int y () const;
        CompPoint pos () const;

        int width () const;
        int height () const;

        int x1 () const;
        int y1 () const;
        int x2 () const;
        int y2 () const;

        int left   () const;
        int right  () const;
        int top    () const;
        int bottom () const;

        int centerX () const;
        int centerY () const;
        CompPoint center () const;

        int area () const;
        
        /**
         * Returns an X region handle for the CompRect
         */
        const Region region () const;

        void setGeometry (int x, int y,
                          int width, int height);

        void setX      (int);
        void setY      (int);
        void setWidth  (int);
        void setHeight (int);

        void setPos (const CompPoint&);
        void setSize (const CompSize&);

        /** 
         * Sets the left edge position
         *
         * Setting an edge past it's opposite edge will result in both edges
         * being set to the new value
         */
        void setLeft    (int);
        /**
         * Sets the top edge position
         *
         * Setting an edge past it's opposite edge will result in both edges
         * being set to the new value
         */
        void setTop     (int);
        /** 
         * Sets the right edge position
         *
         * Setting an edge past it's opposite edge will result in both edges
         * being set to the new value
         */
        void setRight   (int);
        /** 
         * Sets the bottom edge position
         *
         * Setting an edge past it's opposite edge will result in both edges
         * being set to the new value
         */
        void setBottom  (int);

        bool contains (const CompPoint &) const;
        bool contains (const CompRect &) const;
        bool intersects (const CompRect &) const;
        bool isEmpty () const;

        bool operator== (const CompRect &) const;
        bool operator!= (const CompRect &) const;

        CompRect operator& (const CompRect &) const;
        CompRect& operator&= (const CompRect &);
        CompRect& operator= (const CompRect &);

        typedef std::vector<CompRect> vector;
        typedef std::vector<CompRect *> ptrVector;
        typedef std::list<CompRect *> ptrList;

    private:
        REGION       mRegion;
};

Positional information

The ::x (), ::y(), ::x1 (), ::x2 (), ::y1 (), ::y2 ()) methods describe basic positional information about the CompRect itself - you can also get the position in CompPoint form with the ::pos () method.

Internal information

The ::width () and ::height () methods will tell you the width and height information based on the extents of the CompRect. There is are also methods to get the center of the rect, ::centerX (), ::centerY (), and ::center ().

Extents

You can also set and retrieve the leftmost, rightmost, topmost and bottommost edges with the the getters ::left (), ::right (), ::top (), ::bottom () and setters ::setLeft (), ::setRight (), ::setTop (), ::setBottom (). Note that if you set any edge further from it's opposite edge, that will result in both edges taking the new value. This means that the effective width or height of the rect will be zero.

Overlapping rectangles

You can also get the bounding box between two overlapping rectangles, which is the maximum extents each rectangle has using the operators operator&, operator&=. However, considering that the structure represents only a single rectangle, the result will be the CompRect which exactly bounds the product of the union between two rects.

Development/zero-nine/CoreClasses/CompRect (last edited 2010-07-27 03:08:37 by 124-169-107-122)