Boost logo

Boost :

From: Anis Benylloul (benyelloul_at_[hidden])
Date: 2005-06-20 04:03:31


Hi !

It is very common for graphical libraries to declare some rectangle and
point classes.
In fact, all of them do that: SDL has SDL_Rect, X has XRectangle and XPoint,
Qt has QRect and QPoint, Window$ has POINT and RECT (and POINTF and RECTF),
rect, Rect, Rectangle, point, Point2D, Point3D, point<float> ..... etc ....

And you know what ? they all are incompatible, both interface and
implementation. Time to have a look at this, heu ?

After few months of work here is what I have:

// Code Start
int main()
{
    // point_xy is an implementation type it contains
        // "some implementation" of the point abtraction
    // Other implementaions include point_xyz<>, for 3D points
    typedef geom::point_xy<double> point_xy;

    // point<> is a wrapper class, in which you "plug" implementation types
    // to obtain a working ``point''
    typedef geom::point<point_xy> point; // Plug, then save the result
in ``point''

    point pt=point_xy(3.4, 3.0),
        pt2=point_xy(0.2, 1.4);

    // Cool property access
    pt.x()++;
    pt.y()=pt2.rho();
    pt.theta()+=3*pt2.y();

    // Handy operator overloads
    pt+=pt2*4;

    // std::complex<> is also a possible implementation
    geom::point< std::complex<double> > pt_cplx=std::complex<double>(4, 6);

    pt2=pt_cplx-pt; // Mixed-implementation computations

    // point::impl() extracts the implementation out of its wrapper
    std::complex<double>& impl=pt_cplx.impl();

    
    // Now for Boxes. Boxes are some generalization of rectanlges that
are not always
    // ``flat''

    typedef geom::box_xywh<int> box_xywh; // Implementation type ..
    typedef geom::box<box_xywh> box; // .. being plugged in a wrapper

    box bx, bx2;

    bx.xmin()=bx2.x2();
    bx.width()=bx2.height();
    bx.y2()=bx2.height();

    // Accessing corners
    bx.corner< geom::x1, geom::y1>()=pt; // Displace
    bx.corner< geom::xmin, geom::ymax >()=bx2.corner<geom::x2, geom::y1,
geom::z1>();

    // Accessing the center
    bx.center()=bx2.center()+pt;
    bx2.x1()=bx.center().x();

    // .....
}
// Code End

So, wadda you say ?

-- Anis Benyelloul


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk