I want to create a single pixel-size dot. I assumed the way to do so was to draw a line using NSBezierPath with the starting point being equal to the end point (see code below) but doing so makes a completely empty window.
func drawPoint() {
let path = NSBezierPath()
NSColor.blue.set()
let fromPoint = NSMakePoint(CGFloat(100) , CGFloat(100))
let toPoint = NSMakePoint(CGFloat(100) , CGFloat(100))
path.move(to: fromPoint)
path.line(to: toPoint)
path.lineWidth = 1.0
path.stroke()
path.fill()
}
However, if I change the toPoint coordinates from 100,100 to 101,101 (or any set of coordinates other than 100,100), a blue shape does show up. Does anyone know why this is the case?
Another issue I found is if you made toPoint's coordinates equivalent to (for instance) 101,101, then it would create a square with a length of two pixels, but also add a another pixel-length layer of blur (see image below, zoomed in for detail).
What I wish to do, however, is just put a small single blue square the size of one pixel, with no blur. Does anyone know how I can do this?

You have a few problems:
Are you sure? What if you're on a Retina display? What if you're drawing to a PDF that will be printed on a 600 dpi laser printer?
That is a way to draw a
lineWidth-sized dot, if you set thelineCapproperty to.roundor.square. The defaultlineCapis.butt, which results in an empty stroke. If you setlineCapto.roundor.square, you'll get a non-empty stroke.You need to understand how the coordinate grid relates to pixels:
On a non-Retina screen, integer coordinates are by default at the edges of pixels, not at the centers of pixels. A stroke along integer coordinates straddles the boundary between pixels, so (with
lineWidth = 1.0) you get multiple partially-filled pixels. To fill a single pixel, you need to use coordinates that end in.5(the center of the pixel).On a Retina screen, integer and half-integer coordinates are by default at the edges of pixels, not at the centers of pixels. A stroke along integer or half-integer coordinates straddles the boundary between pixels. With
lineWidth = 1.0, pixels on both sides are completely filled. If you want to fill only a single pixel, you need to use coordinates that end in.25or.75and alineWidthof0.5.In an unscaled PDF context, a
lineWidthof1.0nominally corresponds to 1/72 of an inch, and the number of filled pixels depends on the output device.This can only give you “a single pixel-size dot” on a non-Retina screen (or if you scaled the graphics context). You need to adjust it if you truly want a single pixel dot on a Retina screen. But you're better off sticking to points rather than pixels.
All that said, here's how you can create and stroke an
NSBezierPathto fill one pixel:Result:
However, you might prefer to simply fill a rectangle directly:
Result: