PDA

View Full Version : Storing line points in an array :(




hodgey87
Oct 21, 2010, 11:31 AM
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:

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



dejo
Oct 21, 2010, 11:42 AM
You should probably try looping through and drawing each of the lines.

hodgey87
Oct 21, 2010, 11:51 AM
You should probably try looping through and drawing each of the lines.

Ok would that be a for statement like so?


for(mdelegate.pointarray)
{

}

dejo
Oct 21, 2010, 11:57 AM
for(mdelegate.pointarray)
{

}

A for loop is probably the best way to do it but that code is not syntactically correct.

hodgey87
Oct 21, 2010, 12:13 PM
for(mdelegate.pointarray)
{

}

A for loop is probably the best way to do it but that code is not syntactically correct.

Yeh this is the area i get stuck in all these arrays

for (firstpointx = firstpointy ; secondpointx = secondpointy; )
{
mdelegate.pointarray = [NSMutableArray alloc];
}

i saw an example like this

dejo
Oct 21, 2010, 12:21 PM
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.