Skip navigation.
 
ml1 pixel wide lines
FROM : Joe Esch
DATE : Thu Nov 04 17:31:14 2004

Before you flame me, let me say that I've looked in the archives and
found that questions about drawing 1 pixel wide lines using
NSBezierPath or Quartz have been asked many times.  The reason that I'm
submitting yet another question is that I am still having problems and
was hoping that since this has been asked so many times that maybe
Apple has done something to solve the problem in a nice way.

First off, let me say that I am only interested in horizontal or
vertical lines, so answers like "1 pixel wide lines don't make any
sense for diagonal lines" are not that interesting.

Here is what I am currently doing:

    // Turn off antialiasing
    NSGraphicsContext* gc =  [NSGraphicsContext currentContext];
    [gc setShouldAntialias:NO];

    // Move to the center of the pixels
    p1.x = floorf(p1.x) + 0.5;
    p1.y = floorf(p1.y) + 0.5;
    p2.x = floorf(p2.x) + 0.5;
    p2.y = floorf(p2.y) + 0.5;

    NSBezierPath* path = [NSBezierPath bezierPath];
    [path setLineWidth:0.0];
    [path moveToPoint:p1];
    [path lineToPoint:p2];
    [path stroke];

I think that these are all the steps that are suggested for drawing 1
pixel wide lines.  In fact, this seems to work if I do not scale the
view.  The problem is that I am putting the view inside of a scaling
view (modeled after the TextEdit sample).  I change the clip view
bounds which results in the view getting scaled.  I assume that this
results in the positions moving off the center of the pixels.  The
result is that as I zoom in and out on the view the lines get wider or
narrower and it is definitely not what I want.

My questions are:

1. Has Apple realized that this is a useful thing to want to do and
provided a good way to do it yet?

2. If not, how do I do it?  Am I going to have to somehow invert the
view transform and figure out how to adjust the point coordinates so
that the final transformed points end up in the center of a pixel? 
This seems like an awful lot of work for something that should be easy.
  For some reason, I don't see the problem with scrolling.  I assume
that the scrolling code must adjust the scroll amount to only scroll by
even pixel amounts.

BTW, the lines that I want to draw 1 pizel wide are things like a grid,
  paper margins. selection rectangles and other kinds of drawing aids. 
Actual lines that I am drawing I do want to scale and those work great.

Related mailsAuthorDate
ml1 pixel wide lines Joe Esch Nov 4, 17:31
mlRe: 1 pixel wide lines stephane sudre Nov 4, 17:41
mlRe: 1 pixel wide lines Greg Titus Nov 4, 18:16
mlRe: 1 pixel wide lines Nat! Nov 4, 22:33
mlRe: 1 pixel wide lines Scott Ribe Nov 6, 23:30