views:

2451

answers:

6

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?

+3  A: 

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

Ben Gottlieb
A: 

I should mention that I have tried drawing a zero length line (x1,y1) == (x2,y2). That did not work I do not want to draw a 1 pixel line. that messes up what I am trying to do

thanks

a zero length line is exactly that: zero. You're drawing between infinitesimal points on the screen, so you have to have SOME dimension. You could try turning off anti-aliasing in your context. How does a 1-pixel line screw things up?
Ben Gottlieb
+4  A: 

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.

Ahruman
+3  A: 

CGContextFillRect(context, CGRectMake(x,y,1,1));

A: 

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

+3  A: 

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
“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
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