Home | History | Annotate | Download | only in api
      1 SkRect
      2 ======
      3 
      4 *Rectangles*
      5 
      6 <!--Updated Mar 4, 2011-->
      7 
      8 SkRect is basic to many drawing and measuring operations. It can be
      9 drawn using canvas.drawRect(), but it is also used to return the
     10 bounds of objects like paths and text characters. It is specified
     11 using SkScalar values.
     12 
     13 SkIRect is the integer counter part to SkRect, but is specified using
     14 32bit integers.
     15 
     16 <!--?prettify lang=cc?-->
     17 
     18     struct SkRect {
     19        SkScalar fLeft;
     20        SkScalar fTop;
     21        SkScalar fRight;
     22        SkScalar fBottom;
     23        // methods
     24     };
     25 
     26     SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
     27 
     28 SkRect has the usual getters, to return width(), height(), centerX(),
     29 etc. It also has methods to compute unions and intersections between
     30 rectangles.
     31 
     32 Converting between SkRect and SkIRect is asymetric. Short of overflow
     33 issues when SkScalar is an int, converting from SkIRect to SkRect is
     34 straight forward:
     35 
     36 <!--?prettify lang=cc?-->
     37 
     38     SkRect::set(const SkIRect&);
     39 
     40 However, convert from SkRect to SkIRect needs to know how to go from
     41 fractional values to integers.
     42 
     43 <!--?prettify lang=cc?-->
     44 
     45     SkRect::round(SkIRect*) const;     // Round each coordinate.
     46     SkRect::roundOut(SkIRect*) const;  // Apply floor to left/top,
     47                                        // and ceil to right/bottom.
     48 
     49 In Skia, rectangle coordinates describe the boundary of what is drawn,
     50 such that an empty rectangle encloses zero pixels:
     51 
     52 bool SkRect::isEmpty() const { return fLeft >= fRight || fTop >= fBottom; }
     53 
     54 <!--?prettify lang=cc?-->
     55 
     56     SkScalar SkRect::width() const { return fRight - fLeft; }
     57 
     58     SkScalar SkRect::height() const { return fBottom - fTop; }
     59 
     60     bool SkRect::contains(SkScalar x, SkScalar y) const {
     61         return fLeft <= x && x < fRight && fTop <= y && y < fBottom;
     62     }
     63 
     64 Thus, to draw a single pixel (assuming no matrix on the canvas), the
     65 rectangle should be initialized as follows:
     66 
     67 <!--?prettify lang=cc?-->
     68 
     69     SkRect r = SkRect::MakeXYWH(x, y, SkIntToScalar(1), SkIntToScalar(1));
     70 
     71 The same conventions hold for the integer counterpart: SkIRect. This
     72 also dovetails with SkRegion, which has the same model for set
     73 membership, and which uses SkIRect.
     74