How to fill color in white spaces using touch event?

enter image description here

want to fill in all the gaps with different colors using touch events

Right now I can fill in the circles that select colors from the picker, but how to fill in the indented part with different colors ...

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ UIColor *cl=[UIColor clearColor]; UITouch *tuch=[touches anyObject]; if ([clr isEqualToString:@"Red"]) { cl=[UIColor redColor]; } else if ([clr isEqualToString:@"Blue"]) { cl=[UIColor blueColor] ; } else if ([clr isEqualToString:@"Green"]) { cl=[UIColor greenColor]; } CGPoint p = [tuch locationInView:self]; float xsq1=px -50; xsq1=xsq1*xsq1; float ysq1=py-110; ysq1=ysq1*ysq1; float h1 = ABS(sqrt(xsq1 + ysq1)); float xsq2=px -100; xsq2=xsq2*xsq2; float ysq2=py-110; ysq2=ysq2*ysq2; float h2 = ABS(sqrt(xsq2 + ysq2)); float xsq3=px -50; xsq3=xsq3*xsq3; float ysq3=py-190; ysq3=ysq3*ysq3; float h3 = ABS(sqrt(xsq3 + ysq3)); if (h1<=40) { NSLog(@"touches inside of first circle"); CGContextSetFillColorWithColor(context, cl.CGColor); CGRect cir1 = CGRectMake(10,266,80,80); CGContextFillEllipseInRect(context, cir1); [self setNeedsDisplayInRect:cir1]; } else if (h2<=40) { NSLog(@"touches inside of second circle"); CGContextSetFillColorWithColor(context, cl.CGColor); CGRect cir2 = CGRectMake(60,266,80,80); CGContextFillEllipseInRect(context, cir2); [self setNeedsDisplayInRect:cir2]; } } 
+8
iphone ios4
source share
1 answer

There are two tasks here: the first is to determine which area has been affected, the second is to fill in this area. Both require that you calculate the intersection points of the circles of the above image using trigonometry and know your position.

A simple solution for detecting a touch area will be to check if the touch is in any of the circles, it is easy to calculate by calculating the distance to the point of contact from the center of the circles, if it is less than the radius it is inside the circle. If it is inside more than one circle, you know that it belongs to this intersection area. This is if there are no circles inside, but the x component is between the center of the left circle and the right circle, it should be in the area between all circles. Otherwise, the touch point should be outside all circles.

To fill in the various sections of the above image, you can create paths containing regions that you need to fill in and fill in with CGContextFillPath . Something like that:

 // draw a path to contain the fill region CGContextBeginPath(ctx); CGContextMoveToPoint(ctx, startx, starty); CGContextAddArcToPoint(ctx, ...); // lots of other CGContextAddArcToPoint or AddLineToPoint method calls here to define the clip region // close the clip path CGContextClosePath(ctx); // now you can fill the region CGContextFillPath(ctx); 

You can repeat this as many ways as possible. You can calculate path arcs for use with circle intersection points and radii.

+1
source share

All Articles