Storing line points in an array :(

Discussion in 'iOS Programming' started by hodgey87, Oct 21, 2010.

  1. Guest

    Joined:
    Mar 20, 2009
    #1
    Hi Guys,

    Just wondered if anyone could help, currently developing a join the dots type app but have a little trouble. I need to be able to keep all the lines that are drawn on the screen.

    Currently i can draw 1 line and when i draw the next the previous line disappears. I know i need to store these points in an array and call them but im not sure how to go about doing this.

    If someone could help id appreciate it.

    Heres the code for the drawing:

    Code:
    if([mdelegate.pointcollectionarray count]==4)
    	{
    		CGContextRef ctx = UIGraphicsGetCurrentContext();
    		CGContextSetStrokeColorWithColor(ctx, [UIColor blackColor].CGColor);
    		CGContextSetLineWidth(ctx, 6.0);
    		
    		float firstpointx= [[mdelegate.pointcollectionarray objectAtIndex:0] floatValue];
    		float firstpointy= [[mdelegate.pointcollectionarray objectAtIndex:1]floatValue];
    		float secondpointx= [[mdelegate.pointcollectionarray objectAtIndex:2]floatValue];
    		float secondpointy= [[mdelegate.pointcollectionarray objectAtIndex:3]floatValue];
    		
    		CGContextMoveToPoint(ctx,firstpointx,firstpointy);///move to your first dot
    		
    		CGContextAddLineToPoint(ctx, secondpointx, secondpointy);//add line from first dot to second dot
    		
    		CGContextSetLineCap(ctx, kCGLineCapRound);
    		CGContextStrokePath(ctx);
    		CGContextSetLineJoin(ctx, kCGLineJoinRound);
    		[mdelegate.pointcollectionarray removeAllObjects];
    		
    	}
    
    
    -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
    	if(!mdelegate){
    		mdelegate = (Dot2DotAppDelegate*)[[UIApplication sharedApplication]delegate];}
    	
    	if(!mdelegate.pointarray){
    		mdelegate.pointcollectionarray=[[NSMutableArray alloc] init]; }
    	
    	//NSLog(@"touched");
    	CGPoint curPoint = [[touches anyObject] locationInView:self];
    	[mdelegate.pointcollectionarray addObject:[NSNumber numberWithFloat:curPoint.x]];
    	[mdelegate.pointcollectionarray addObject:[NSNumber numberWithFloat:curPoint.y]];
    	
    	[self setNeedsDisplay]; // calls drawRectMethod
    }
    
    Many thanks
     
  2. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    You should probably try looping through and drawing each of the lines.
     
  3. thread starter Guest

    Joined:
    Mar 20, 2009
    #3
    Ok would that be a for statement like so?

    Code:
    	for(mdelegate.pointarray)
    	{
    		
    	}
    
     
  4. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Code:
    for(mdelegate.pointarray)
    {
    		
    }
    A for loop is probably the best way to do it but that code is not syntactically correct.
     
  5. thread starter Guest

    Joined:
    Mar 20, 2009
    #5
    Yeh this is the area i get stuck in all these arrays

    Code:
    for (firstpointx = firstpointy ; secondpointx = secondpointy; )
    		{
    			mdelegate.pointarray = [NSMutableArray alloc];
    		}
    i saw an example like this
     
  6. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Looping is a basic fundamental of any programming. I suggest you step back from the real coding and go learn those before you continue with this project.
     

Share This Page