17

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?

hippietrail
  • 15,848
  • 18
  • 99
  • 158

7 Answers7

21
CGContextFillRect(context, CGRectMake(x,y,1,1));
Mat
  • 7,613
  • 4
  • 40
  • 56
16

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.

Jens Ayton
  • 14,532
  • 3
  • 33
  • 47
8

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.

Paulo
  • 1,245
  • 10
  • 8
7

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?

Chad
  • 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
  • 5
    Didn'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
4

You can draw a 1-pixel-length line at the coordinate in question; that should accomplish what you want.

Ben Gottlieb
  • 85,404
  • 22
  • 176
  • 172
0

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));

0

Swift 3 :

 UIRectFill(CGRect(x: x, y: y, width: 1, height: 1))

UIRectFill()

hippietrail
  • 15,848
  • 18
  • 99
  • 158
Antoine Weber
  • 1,741
  • 1
  • 14
  • 14