Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7843395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:32:21+00:00 2026-06-02T16:32:21+00:00

I am trying to draw and fill simple paths in Quartz 2d for iPad.

  • 0

I am trying to draw and fill simple paths in Quartz 2d for iPad.

The following method from the class SPTCutPattern defines the paths that I want to draw:

- (void) initDummyCutPattern {
NSArray *piece1Path = [[NSArray alloc] initWithObjects:
                       [NSValue valueWithCGPoint:CGPointMake(0, 0)],
                       [NSValue valueWithCGPoint:CGPointMake(3, 0)],
                       [NSValue valueWithCGPoint:CGPointMake(3, 1)],
                       [NSValue valueWithCGPoint:CGPointMake(2, 1)],
                       [NSValue valueWithCGPoint:CGPointMake(2, 2)],
                       [NSValue valueWithCGPoint:CGPointMake(0, 2)],
                       nil];
SPTPuzzlePiece *Piece1 = [[SPTPuzzlePiece alloc] initWithWidthInGrid:3 andHeightInGrid:2 andLeftInGrid:0 andTopInGrid:0 andBuilt:YES andPathInGrid:piece1Path];

NSArray *piece2Path = [[NSArray alloc] initWithObjects:
                       [NSValue valueWithCGPoint:CGPointMake(3, 0)],
                       [NSValue valueWithCGPoint:CGPointMake(5, 0)],
                       [NSValue valueWithCGPoint:CGPointMake(5, 5)],
                       [NSValue valueWithCGPoint:CGPointMake(4, 5)],
                       [NSValue valueWithCGPoint:CGPointMake(4, 4)],
                       [NSValue valueWithCGPoint:CGPointMake(2, 4)],
                       [NSValue valueWithCGPoint:CGPointMake(2, 3)],
                       [NSValue valueWithCGPoint:CGPointMake(3, 3)],
                       [NSValue valueWithCGPoint:CGPointMake(3, 2)],
                       [NSValue valueWithCGPoint:CGPointMake(2, 2)],
                       [NSValue valueWithCGPoint:CGPointMake(2, 1)],
                       [NSValue valueWithCGPoint:CGPointMake(3, 1)],
                       nil];
SPTPuzzlePiece *Piece2 = [[SPTPuzzlePiece alloc] initWithWidthInGrid:3 andHeightInGrid:5 andLeftInGrid:2 andTopInGrid:0 andBuilt:YES andPathInGrid:piece2Path];

NSArray *piece3Path = [[NSArray alloc] initWithObjects:
                       [NSValue valueWithCGPoint:CGPointMake(0, 2)],
                       [NSValue valueWithCGPoint:CGPointMake(3, 2)],
                       [NSValue valueWithCGPoint:CGPointMake(3, 3)],
                       [NSValue valueWithCGPoint:CGPointMake(2, 3)],
                       [NSValue valueWithCGPoint:CGPointMake(2, 4)],
                       [NSValue valueWithCGPoint:CGPointMake(4, 4)],
                       [NSValue valueWithCGPoint:CGPointMake(4, 5)],
                       [NSValue valueWithCGPoint:CGPointMake(0, 5)],
                       nil];

SPTPuzzlePiece *Piece3 = [[SPTPuzzlePiece alloc] initWithWidthInGrid:4 andHeightInGrid:3 andLeftInGrid:0 andTopInGrid:2 andBuilt:YES andPathInGrid:piece3Path];

self.pieces = [[NSArray alloc] initWithObjects:Piece1, Piece2, Piece3, nil];
}

And this method from the class SPTPuzzlePieceView which is supposed to draw the paths:

- (void)drawRect:(CGRect)rect
{
int widthUnit = self.frame.size.width / self.puzzlePiece.widthInGrid;
int heightUnit = self.frame.size.height / self.puzzlePiece.heightInGrid;
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);

CGContextMoveToPoint(context, [[puzzlePiece.pathInGrid objectAtIndex:0] CGPointValue].x*widthUnit, 
                     [[puzzlePiece.pathInGrid objectAtIndex:0] CGPointValue].y*heightUnit);

for (NSValue *point in puzzlePiece.pathInGrid) {
    CGContextAddLineToPoint(context, [point CGPointValue].x*widthUnit, [point CGPointValue].y*heightUnit);
}

CGContextFillPath(context);
}

I am getting these three objects, which are absolutely NOT what I expect and want:

screenshot

The objects are supposed to be more (or less) like this:

shapes I wanted

I am sorry that these methods are totally taken out of context, so I hope you can see what I am trying to do. The drawRect method gets called to draw each of the piece objects I defined in the first method in turn.

I cannot figure out why it is not drawing the shapes I want. Help me please.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T16:32:22+00:00Added an answer on June 2, 2026 at 4:32 pm

    Your second and third shapes are being initialised with widthInGrid and heightInGrid values smaller than their points. This results in drawing outside the view bounds.

    Three other things:

    1. Consider using UIBezierPath for creating and drawing your path, instead of CGContext... calls. It’s harder to make mistakes with. If at any point the user has to touch these shapes and drag them around, its containsPoint: method will be really handy.
    2. The Objective-C naming convention for variables is that they start with lower-case, to distinguish them from class names. So, you would have SPTPuzzlePiece *piece1 instead of *Piece1. Words within the variable name do use upper-case.
    3. In drawRect, use the view’s own bounds to get the width and height, not its frame. These will be the same if you haven’t distorted your view, but it’s good practice to follow. The bounds always define the coordinate space you’re drawing into.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to draw closed path and fill it with some collor. Here the
I am trying to draw text on a live wallpaper that has a background
I'm am doing a simple coding attempt trying to draw on a SurfaceView created
I'm trying to draw a circle in c using opengl, that's smaller than the
Greetings! I'm trying to draw a series of circles in a CALayer that resides
I'm trying to find out how to draw a path, and then fill it
I am currently trying to draw a polygon and then fill it with a
I'm trying to draw an NSTextFieldCell subclass that looks like the rounded event item
I'm trying to pass context from one class to another.. pls tell me wat
I'm trying to create a simple chart that allows zoom capability and the examples

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.