I see APIs in Quartz for drawing lines and circles. But all I want to do is to specify the (x,y) cartesian coordinate to color a pixel a particular value. How do I do that?
7 Answers
CGContextFillRect(context, CGRectMake(x,y,1,1));

- 7,613
- 4
- 40
- 56
-
1Swift 5.1 version: `context.fill(CGRect(x: xValue, y: yValue, width: 1, height: 1))` – articga Jan 22 '20 at 14:59
Quartz is not a pixel-oriented API, and its contexts aren’t necessarily pixel buffers. If you want to draw pixmaps, create a bitmap context with CGBitmapContextCreate()
. You provide a buffer, which you can manipulate directly, and can copy to another context by creating a CGImage
from the same buffer using CGImageCreate()
and drawing that.

- 14,532
- 3
- 33
- 47
Draw a very small ellipse/circle and fill it!
CGContextAddEllipseInRect(Context,(CGRectMake (x_dot, y_dot, 3.0, 3.0));
CGContextDrawPath(Context, kCGPathFill);
CGContextStrokePath(Context);
I am using this code to create a small dot (3x3 pixels) in a dotted music note.

- 1,245
- 10
- 8
I got a point (zero length line) to draw after setting the line-caps to kCGLineCapRound. The default line-cap has no length so can't be drawn.
The argument that a point has no size is silly. The lines have no width but we can draw those (by using the "line width" from the draw state). A point should draw in exactly the same way, and with different line caps I believe it does.
Maybe this behavior is new?

- 71
- 1
- 1
-
1“The argument that a point has no size is silly.” No it isn't. A point is just that: A single point. Size is the distance between at least one pair of points. The line caps are in addition to the length of the line you draw; in the case of a round cap, the radius is half the line width, so a zero-length line with round caps at 1 pt line width draws two caps of 1/2 pt radius with nothing in between, thus forming a circle of 1 pt diameter. And no, that behavior is not new: Line caps, and most of the rest of Quartz, came through PDF from PostScript, which dates back to 1985. – Peter Hosey Feb 06 '10 at 04:57
-
5Didn't mean to suggest that a point has size, only that its lack of size is a silly excuse for not being able to draw it. A line (as a mathematical concept) has no width, but we can draw that just fine. Drawing a "zero length line" with round caps renders a point to the screen with line-width diameter. The round caps are essentially offering a "point diameter" setting. But, if the original poster is specifically looking to set the color of one screen pixel, it should be done by setting values directly in a bitmap context and not with the path-based drawing routines. – Chad Feb 11 '10 at 20:39
-
This is what I was looking for! CGContextSetLineWidth(desiredWidth); CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); Then draw a line from the point to itself. – rmp251 Feb 27 '14 at 01:34
You can draw a 1-pixel-length line at the coordinate in question; that should accomplish what you want.

- 85,404
- 22
- 176
- 172
I'm having the same issue - i find the best solution is similar to the last, but at least it doesn't leave something that looks like a "dash"... of course, should ensure x/y are both > 0.
CGContextFillRect(context, CGRectMake(x - 0.5, y - 0.5, 1.0 , 1.0));