Re: turning off anti-aliasing
Re: turning off anti-aliasing
- Subject: Re: turning off anti-aliasing
- From: Rob Ross <email@hidden>
- Date: Wed, 5 Jul 2006 19:36:01 -0700
On Jul 5, 2006, at 6:21 AM, Scott Thompson wrote:
On Jul 5, 2006, at 1:21 AM, Rob Ross wrote:
am drawing some simple path shapes (circles, squares, sine wave,
etc), and I wanted to draw a cartesian coordinate grid behind
them. But my grid lines were coming out "fuzzy". I was stroking
them as 1-unit wide, but as I resized the Window containing the
view, they would "smear" and every other pixel growth of the
window, vertically or horizontally, I would see the grid lines
change from one to two pixels in width. After doing some research
I thought the problem was due to antialiasing. (Another mystery to
me is why, although the scale factor is 1,1, when I change the
size of my window by a single pixel, the new window size as
reported by bounds has increased by .5 of a pixel.)
This is probably the case. In my book I call it the half-pixel
line problem.
I actually have your book. But I'm just starting chapter 5. I guess
if I held out another week and read chapter 6 I would have been in
better shape!
When Quartz sets up a context on a bitmap (like the screen) it
aligns user space so that the whole number coordinate values
correspond to the lower, left edges of pixels. If you draw a one
point wide line, then, it actually overlaps over two pixels (half a
pixel on either side of the boundary). One way to "fix" this is to
offset your coordinate system by 0.5, 0.5 before drawing. While
this technique works today, it may not work as well when we have
scaleable UI and high density monitors. You'll have to decide
whether or not it's a valid tradeoff for your application.
How are you drawing your grid lines?
I am stroking them. They're just 1-unit wide, and I vary the alpha
from 50% for the x/y axis lines, to 25% for the major tick lines, and
10% for the minor tick lines. If you want to just draw a straight
line, is it better to stroke them, or fill a rectangle that encloses
the same space (i.e. 1 unit?).
I tried turning off antialiasing before drawing the grid via
[nsGraphicsContextInstance setShouldAntialias:NO] but that didn't
seem to do anything at all. What is this purpose of this method?
It should disable antialiasing as part of the current graphics state.
I wrote the following drawRect into a custom view:
- (void)drawRect:(NSRect)rect {
NSRect leftRect, rightRect;
NSDivideRect([self bounds], &leftRect, &rightRect, [self
bounds].size.width / 2.0, NSMinXEdge);
[[NSColor redColor] set];
leftRect = NSInsetRect(leftRect, 5, 5);
rightRect = NSInsetRect(rightRect, 5, 5);
[[NSGraphicsContext currentContext] saveGraphicsState];
[[NSGraphicsContext currentContext] setShouldAntialias: NO];
NSBezierPath *oval1Path = [NSBezierPath bezierPathWithOvalInRect:
leftRect];
[oval1Path stroke];
[[NSGraphicsContext currentContext] restoreGraphicsState];
NSBezierPath *oval2Path = [NSBezierPath bezierPathWithOvalInRect:
rightRect];
[oval2Path stroke];
}
And it does what I expect, the left oval is not antialiased, the
right one is. I can't imagine why it doesn't work for you. You
might need to be very careful about your saves and restores.
Ok, I tried this code and I see that it works. And then I went back
to my own code and commented out the
CGContextSetAllowsAntialiasing(context,flag);
call, while leaving the [[NSGraphicsContext currentContext]
setShouldAntialias: NO];
and now my own code seems to work. So perhaps I wasn't calling it
when I thought I was.
This particular problem seems to be solved. Thanks for your help!
Rob Ross
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden