How do I draw a point using Core Graphics?
Asked Answered
C

7

18

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?

Cleland answered 23/11, 2008 at 21:55 Comment(0)
R
22
CGContextFillRect(context, CGRectMake(x,y,1,1));
Refresh answered 26/11, 2008 at 22:58 Comment(1)
Swift 5.1 version: context.fill(CGRect(x: xValue, y: yValue, width: 1, height: 1))Palfrey
K
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.

Kujawa answered 24/11, 2008 at 8:59 Comment(0)
M
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.

Makepeace answered 28/11, 2011 at 0:47 Comment(0)
F
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?

Farland answered 6/2, 2010 at 1:1 Comment(3)
“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.Mapes
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.Farland
This is what I was looking for! CGContextSetLineWidth(desiredWidth); CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); Then draw a line from the point to itself.Traps
F
4

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

Faceoff answered 23/11, 2008 at 22:27 Comment(0)
M
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));

Mazurek answered 29/12, 2008 at 23:5 Comment(0)
N
0

Swift 3 :

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

UIRectFill()

Nansen answered 29/4, 2017 at 9:19 Comment(1)
This is for UIKit. Core Graphics is at a lower level than UIKit.Jinx

© 2022 - 2024 — McMap. All rights reserved.