Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Buckeyes1995

macrumors member
Original poster
Mar 4, 2011
95
11
Hi all-

I'm beginning work on my app, and, at the moment, am doing a lot of prototyping. What I'm trying to do is draw a 3x3 Matrix on the screen. To draw the brackets, I'm using Quartz 2D. Being a complete newbie to Quartz 2D, I was wondering how I'd go about drawing the left and right brackets together? I've read up on the Paths principle and had no problem drawing the left side bracket.. my question is, is it possible to split the path? I'd like my matrix-brackets to be one single drawing. Everything I read indicates lines are just appended to existing points with no mechanism to break up a path.

If not, if I draw the right side bracket independently, I assume it becomes its own drawing component and therefore any operations I perform on the left bracket (for example rotating it) I'd have to do the same on the right side?

I suppose I could also draw a rectangle and apply a mask to get the bracket effect. Ultimately I want to be able to write text (numbers) into the matrix elements.

Does my question make sense?
 
You're using the CGMutablePathRef type right? Then you can create subpaths. The end-point of one subpath is not connected to the beginning of the next subpath. You begin a subpath by calling CGPathMoveToPoint.

But what operations are you expecting to perform on a path? You can't transform a path. That's because a path is really a set of drawing instructions using a co-ordinate system local to the path.

It is only when the path is drawn/stoked/cleared/etc that the path is applied using the current graphics state of the CGContext. This graphics state includes the current transformation matrix (CTM). The co-ordinates of the path will be run through the CTM to determine the final output.

So if you have a bracket, all you'd need to do is apply a new CTM and re-draw the bracket. The new CTM would translate and rotate the context co-ordinates so that the bracket appears on the right.
 
You can't transform a path.

Not sure what you try to say, but the second parameter to CGPathMoveToPoint and other path functions is a pointer to the CGAffineTransform. That should do the trick.

From CGPath.h

Code:
CG_EXTERN void CGPathMoveToPoint(CGMutablePathRef path,
    const CGAffineTransform *m, CGFloat x, CGFloat y)
    CG_AVAILABLE_STARTING(__MAC_10_2, __IPHONE_2_0);
CG_EXTERN void CGPathAddLineToPoint(CGMutablePathRef path,
    const CGAffineTransform *m, CGFloat x, CGFloat y)
    CG_AVAILABLE_STARTING(__MAC_10_2, __IPHONE_2_0);
...
 
You're using the CGMutablePathRef type right? Then you can create subpaths. The end-point of one subpath is not connected to the beginning of the next subpath. You begin a subpath by calling CGPathMoveToPoint.

Thanks! I completely missed the discussion about subpathing. That will allow me to draw both brackets as one drawing.

But what operations are you expecting to perform on a path? You can't transform a path. That's because a path is really a set of drawing instructions using a co-ordinate system local to the path.

It is only when the path is drawn/stoked/cleared/etc that the path is applied using the current graphics state of the CGContext. This graphics state includes the current transformation matrix (CTM). The co-ordinates of the path will be run through the CTM to determine the final output.

Yup, understand.

So if you have a bracket, all you'd need to do is apply a new CTM and re-draw the bracket. The new CTM would translate and rotate the context co-ordinates so that the bracket appears on the right.

If I do that, won't that be performing the translation or rotation on the original bracket? Do I need to make a copy of the left bracket first?
 
Not sure what you try to say, but the second parameter to CGPathMoveToPoint and other path functions is a pointer to the CGAffineTransform.

The affine transform here will be applied when the path is constructed. If you construct a path and reused over multiple drawRect: calls, this won't work if you want to transform the path on each drawRect: call.

If I do that, won't that be performing the translation or rotation on the original bracket? Do I need to make a copy of the left bracket first?

No. The transformation is applied when the path is drawn, the path itself is not modified. Once a path is draw, the pixels of the line on the screen are not associated with the path. A path is not like a view in this regard.

If your familiar with Photoshop and Illustrator, paths are more like drawing in Photoshop than it is like drawing in Illustrator. You can't go back and select the path once it's draw and do stuff with it. It's just a bunch of pixels on the canvas. (Don't take this analogy too far, I know Photoshop does have vector shapes and advanced tools that can isolate the pixels that make up a line; it's the contrast with Illustrator that's important).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.