Bronze Challenge. What's the best way


#1

Hi all,

With regards to the Bronze challenge, most of the solutions posted in the forum are based on random colors. Can anyone recommend a good way to make the colored circles uniformed, ie. red, green, blue, red, green, blue, red, green, etc, etc)?

I’ve tried the folowing, but am getting an error saying “terminate called throwing an exception” on the currentColor = [presetComponentColors objectAtIndex:i]; line:

int countOfColors = [presetComponentColors count]; int i = 0; UIColor *currentColor; for (float currentRadius = maxRadius; currentRadius > 0; currentRadius -= 20) { while (i <= countOfColors) { currentColor = [presetComponentColors objectAtIndex:i]; [self setCircleColor:currentColor]; [[self circleColor] setStroke]; CGContextAddArc(ctx, center.x, center.y, currentRadius, 0.0, M_PI * 2.0, YES); CGContextStrokePath(ctx); i++; } }

Any help/advice would be fanastic :slight_smile:


#2

take a look at my bronze solution below. arc4random is your friend :slight_smile:

– oops just read what you said at the beginning. nevermind then if you dont want to do random :slight_smile:


#3

Looking at your loop structure, you never reset i to 0 so that it can reiterate through the array again and the end of the while loop will increment the i value past your last array index. Without testing this myself, I would guess you are trying to access your array beyond the available index.


#4

There is a coding error.

Compare this to yours (note the while statement’s test expression):

    int countOfColors = [presetComponentColors count];
    int i = 0;
    UIColor *currentColor;
    for (float currentRadius = maxRadius; currentRadius > 0; currentRadius -= 20) {
        while (i < countOfColors) {
            currentColor = [presetComponentColors objectAtIndex:i];
            [self setCircleColor:currentColor];
            [[self circleColor] setStroke];
            CGContextAddArc(ctx, center.x, center.y, currentRadius, 0.0, M_PI * 2.0, YES);
            CGContextStrokePath(ctx);
            i++;
        }
    }

Arrays are indexed using zero-based indexing; that is, first element is at index 0, second at 1, and last elements is at N - 1, where N is the size of the array.

Also consider replacing the wile statement with a for statement, which enables you to increment the index variable right next to the test expression.

for (int i = 0; i < countOfColors; ++i) {
...
}

#5

Hello all, I use hue and saturation which makes nice raibow.

    float h = 0.0;
    float s = 1.0;
    for(float currentRadius = maxRadius; currentRadius > 0; currentRadius -= 20)
    {
        // Add a path to context
        CGContextAddArc(ctx, center.x, center.y, currentRadius, 0.0, M_PI * 2.0, YES);
        
        // Color
        UIColor *c = [[UIColor alloc] initWithHue:h saturation:s brightness:0.5 alpha:1.0];
        [c setStroke];
        h += 0.1;
        if(h > 1.0) s -= 0.2;
        
        // Perform drawing instruction; removes path
        CGContextStrokePath(ctx);
    }