剪辑区域之间的差距

时间:2012-10-10 17:49:33

标签: objective-c cocoa core-graphics

我画了很多充满NSGradient的梯形。我希望他们能够无缝地加入,但实际情况并非如此。在这种情况下,我更喜欢使用剪辑而不是使用渐变显式填充NSBezierPath。

Gap between two clipped regions

以下是有罪的代码:

-(void)drawRect:(NSRect)dirtyRect {
    [[NSColor blackColor]set];
    NSRectFill(self.bounds);

    NSPoint v0 =  NSMakePoint(100,100);
    NSPoint v1 =  NSMakePoint(200,100);
    NSPoint v2 =  NSMakePoint(100,200);
    NSPoint v3 =  NSMakePoint(200,200);
    NSPoint v4 = NSMakePoint(150, 150);

    NSBezierPath * triangle0 = [NSBezierPath bezierPath];
    [triangle0 moveToPoint:v0];
    [triangle0 lineToPoint:v1];
    [triangle0 lineToPoint:v2];
    [triangle0 closePath];

    NSBezierPath * triangle1 = [NSBezierPath bezierPath];
    [triangle1 moveToPoint:v1];
    [triangle1 lineToPoint:v2];
    [triangle1 lineToPoint:v3];
    [triangle1 closePath];

    NSGradient * gradient = [[NSGradient alloc] initWithColorsAndLocations:[NSColor whiteColor],0.0,[NSColor orangeColor],1.0,nil];

    [NSGraphicsContext saveGraphicsState];
    [triangle0 addClip];
    [gradient drawFromPoint:v0 toPoint:v4 options:NSGradientDrawsAfterEndingLocation|NSGradientDrawsBeforeStartingLocation];
     //   [gradient drawFromPoint:v0 toPoint:v4 options:0];

    [NSGraphicsContext restoreGraphicsState];

    [NSGraphicsContext saveGraphicsState];
    [triangle1 addClip];
    [gradient drawFromPoint:v3 toPoint:v4 options:NSGradientDrawsAfterEndingLocation|NSGradientDrawsBeforeStartingLocation];

    //[gradient drawFromPoint:v3 toPoint:v4 options:0];

    [NSGraphicsContext restoreGraphicsState];

我该怎么做才能避免两个三角形之间的差距?

编辑: 这是尝试通过明确填写NSBezierPath来解决这个问题。

-(void)drawRect:(NSRect)dirtyRect {
[[NSColor blackColor]set];
NSRectFill([self bounds]);

CGContextRef c = [[NSGraphicsContext currentContext] graphicsPort];
CGContextTranslateCTM(c, 0.5, 0.5);

NSPoint v0 =  NSMakePoint(100,100);
NSPoint v1 =  NSMakePoint(200,100);
NSPoint v2 =  NSMakePoint(100,200);
NSPoint v3 =  NSMakePoint(200,200);

NSBezierPath * triangle0 = [NSBezierPath bezierPath];
[triangle0 moveToPoint:v0];
[triangle0 lineToPoint:v1];
[triangle0 lineToPoint:v2];
[triangle0 closePath];

NSBezierPath * triangle1 = [NSBezierPath bezierPath];
[triangle1 moveToPoint:v1];
[triangle1 lineToPoint:v2];
[triangle1 lineToPoint:v3];
[triangle1 closePath];

[[NSColor whiteColor]set];

NSGradient * gradient = [[NSGradient alloc] initWithColorsAndLocations:[NSColor whiteColor],0.0,[NSColor orangeColor],1.0,nil];
[gradient drawInBezierPath:triangle0 angle:45];
[gradient drawInBezierPath:triangle1 angle:45+180];

NSBezierPath  * seam = [ NSBezierPath bezierPath]; [seam moveToPoint:v1]; [seam lineToPoint:v2];
[[NSColor orangeColor]set]; [seam stroke];

NSGradient * gradient2 = [[NSGradient alloc] initWithColorsAndLocations:[NSColor whiteColor],0.0,[NSColor orangeColor],0.5,[NSColor whiteColor],1.0,nil];
NSBezierPath * squarePath = [NSBezierPath bezierPath];
[squarePath moveToPoint:v0];
[squarePath lineToPoint:v1];
[squarePath lineToPoint:v3];
[squarePath lineToPoint:v2];
[squarePath closePath];
CGContextTranslateCTM(c, 200, 0);
[gradient2 drawInBezierPath:squarePath angle:45];
}

A seam between the two paths

在左侧,两个三角形填充相同的NSGradient。 在右边,一个充满NSGradient的广场,我想要达到的目标。 接缝是试图隐藏两个NSBezierPath之间的黑线,但它并没有真正起作用。在这种情况下,它几乎不可见,但如果我绘制了大量这样的三角形,它将是可见的。

编辑Rengers回答:

以下是没有抗锯齿的外观:

No antialiasing, but the problem has moved

从技术上讲,这是一个很好的答案,但现在的问题是,毫不奇怪,多边形的边缘不再是抗锯齿的。知道如何消除裁剪区域之间的间隙,而不会失去边缘上的抗锯齿效果吗?

1 个答案:

答案 0 :(得分:2)

这可能是因为默认情况下启用了抗锯齿功能。您可以使用以下命令禁用它:

[[NSGraphicsContext currentContext] setShouldAntialias:NO];