[cairo] a about draw line

Zack Weinberg zweinberg at mozilla.com
Sun Sep 20 10:45:31 PDT 2009

张玲春 <zlc53 at 163.com> wrote:
> I use cairo to draw a horizontal line, regardless of how much the use
> of cairo_line_width set, I found it to be occupied by 2 pixels high
> census drawn. I would like to draw a precise line, may I ask how to do

The coordinate grid lies between the pixels, and stroke lines are
centered on their coordinates.  Thus, a horizontal line stroked at an
integer y-coordinate [for instance, (0,10) -- (10,10)] adds color to
*two* rows of pixels, no matter how narrow it is: the row directly
above its grid line, and the row directly below.

So if you want to stroke exactly one row of pixels, set the line width
to 1, then add (or subtract) 0.5 to the y-coordinate of both the start
and end points.  Similarly, for a single-pixel vertical line, do this
to the x-coordinates.

This gotcha does not apply when you are filling rather than stroking:
filling the box (0,10) -- (10,10) -- (10,11) -- (0,11) -- closepath is
another way to draw a single pixel horizontal line.


More information about the cairo mailing list