1SkRect 2====== 3 4*Rectangles* 5 6<!--Updated Mar 4, 2011--> 7 8SkRect is basic to many drawing and measuring operations. It can be 9drawn using canvas.drawRect(), but it is also used to return the 10bounds of objects like paths and text characters. It is specified 11using SkScalar values. 12 13SkIRect is the integer counter part to SkRect, but is specified using 1432bit 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 28SkRect has the usual getters, to return width(), height(), centerX(), 29etc. It also has methods to compute unions and intersections between 30rectangles. 31 32Converting between SkRect and SkIRect is asymetric. Short of overflow 33issues when SkScalar is an int, converting from SkIRect to SkRect is 34straight forward: 35 36<!--?prettify lang=cc?--> 37 38 SkRect::set(const SkIRect&); 39 40However, convert from SkRect to SkIRect needs to know how to go from 41fractional 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 49In Skia, rectangle coordinates describe the boundary of what is drawn, 50such that an empty rectangle encloses zero pixels: 51 52bool 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 64Thus, to draw a single pixel (assuming no matrix on the canvas), the 65rectangle should be initialized as follows: 66 67<!--?prettify lang=cc?--> 68 69 SkRect r = SkRect::MakeXYWH(x, y, SkIntToScalar(1), SkIntToScalar(1)); 70 71The same conventions hold for the integer counterpart: SkIRect. This 72also dovetails with SkRegion, which has the same model for set 73membership, and which uses SkIRect. 74