Quartz 2D Questions

Discussion in 'iOS Programming' started by Buckeyes1995, Sep 29, 2011.

  1. Buckeyes1995 macrumors member

    Joined:
    Mar 4, 2011
    #1
    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?
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    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.
     
  3. idelovski macrumors regular

    Joined:
    Sep 11, 2008
    #3
    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);
    ...
    
     
  4. Buckeyes1995 thread starter macrumors member

    Joined:
    Mar 4, 2011
    #4
    Thanks! I completely missed the discussion about subpathing. That will allow me to draw both brackets as one drawing.

    Yup, understand.

    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?
     
  5. North Bronson macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #5
    Core Graphics Layer Drawing

    A stylish solution would be to use CGLayers. As opposed to drawing the bracket twice, you draw the bracket once (into a CGLayer) and the context will draw the layer twice (which is supposed to be more efficient).
     
  6. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #6
    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.

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

Share This Page