NSBezierPath 笔划未正确缩放

问题描述

我有一个简单的 NSView 子类,它在指定位置绘制圆形节点。

为了在我的视图中渲染节点,我将图形上下文的原点转换到视图框架的中心并对其进行缩放,使其在限制维度上从 -1.25 跨越到 1.25(节点坐标都在范围 -1...1)。然后我使用ovalIn: 构造函数为每个节点创建一个NSBezierPath。最后,我用黄色填充路径并用黑色描边。

但是...虽然黄色填充看起来不错,但黑色轮廓没有正确缩放!

我错过了什么?

代码如下:

  override func draw(_ dirtyRect: NSRect)
  {
    let nodeRadius   = CGFloat(0.05)
    let unscaledSpan = CGFloat(2.5)
    
    super.draw(dirtyRect)
    
    NSColor.white.set()
    self.frame.fill()
    
    guard let graph = graph else { return }
    
    let scale = min(bounds.width/unscaledSpan,bounds.height/unscaledSpan)
    
    NSGraphicsContext.current?.saveGraphicsstate()
    defer { NSGraphicsContext.current?.restoreGraphicsstate() }
    
    let xform = NSAffineTransform()
    xform.translateX( by: 0.5*bounds.width,yBy: 0.5*bounds.height)
    xform.scale(by: scale)
    xform.concat()
    
    for v in graph.vertices
    {
      let r = NSRect(x: v.x-nodeRadius,y: v.y-nodeRadius,width: 2.0*nodeRadius,height: 2.0*nodeRadius)
      let p = NSBezierPath(ovalIn: r)
      NSColor.yellow.set()
      p.fill()
      NSColor.black.set()
      p.stroke()
    }
  }

这就是我所看到的(显示有两种不同的窗口大小

enter image description here

enter image description here

显然,翻译对于填充和描边都工作正常。 但是,中风的缩放是关闭的。

感谢您的任何/所有提示/建议。

解决方法

哦...我没有考虑缩放对线宽的影响。

进行了以下编辑,一切正常:

  ...
  let p = NSBezierPath(ovalIn: r)
  p.lineWidth = CGFloat(0.01)
  NSColor.yellow.set()
  p.fill()
  NSColor.black.set()
  p.stroke()
  ...