Storing lines in an array...

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

  1. hodgey87

    hodgey87 New Member

    Joined:
    Oct 21, 2007
    Messages:
    10
    Likes Received:
    0
    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. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    CGPoint* pointArray;

    EDIT: To clarify, just use basic C. Create an array of CGPoints. Although I don't feel this is the best solution to fit the line drawing, it the answer you asked for.

Share This Page