gpt4 book ai didi

objective-c - 裁剪区域之间的间隙

转载 作者:太空狗 更新时间:2023-10-30 03:54:09 27 4
gpt4 key购买 nike

我正在绘制很多填充了 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

从技术上讲,这是一个很好的答案,但现在的问题是,不出所料,多边形的边缘不再抗锯齿。知道如何在不丢失边缘抗锯齿的情况下消除裁剪区域之间的间隙吗?

最佳答案

这可能是因为默认启用了抗锯齿功能。您可以使用以下方法禁用它:

[[NSGraphicsContext currentContext] setShouldAntialias:NO];

关于objective-c - 裁剪区域之间的间隙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12825343/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com