Silver Challenge


#1

First I created a new property in WCODrawView.m (BNRDrawView.m for those who stay consistent with the book.)

@property (nonatomic, weak) UIColor *lineColor;

Then I added two new methods to WCODrawView.m

WCODrawView.h

[code]- (int)angleFromStartToEndingPoints:(WCOLine *)line
startPoint:(CGPoint)begin
endPoint:(CGPoint)end {
return (((atan2((line.end.x - line.begin.x) , (line.end.y - line.begin.y)))*180)/M_PI);
}

  • (UIColor *)selectLineColorFromAngleValue:(int)angleValue {
    UIColor *lc = nil;
    if (angleValue >= 0 && angleValue <= 90) {
    lc = [UIColor redColor];
    } else if (angleValue >= 91 && angleValue <= 180) {
    lc = [UIColor blueColor];
    } else if (angleValue < 0 && angleValue >= -90) {
    lc = [UIColor greenColor];
    } else if (angleValue < -90 && angleValue >= -179) {
    lc = [UIColor yellowColor];
    } else {
    lc = [UIColor blackColor];
    }
    return lc;
    }
    [/code]

Then I called those new methods from the strokeLine method within WCODrawView.m

- (void)strokeLine:(WCOLine *)line
{
    UIBezierPath *bp = [UIBezierPath bezierPath];
    bp.lineWidth = 10;
    bp.lineCapStyle = kCGLineCapRound;
    
    [bp moveToPoint:line.begin];
    [bp addLineToPoint:line.end];
    self.lineColor = [self selectLineColorFromAngleValue:[self angleFromStartToEndingPoints:line
                                                                                 startPoint:line.begin
                                                                                   endPoint:line.end]];
    [self.lineColor setStroke];
    [bp stroke];
}

Now I get 4 different colors depending upon the final angle of the line.


#2

Hey jimmypete:

I had the same concept – just draw four different colors based upon the angle, but I think my solution is a bit simpler so I pass it along in case you’re interested:

I just modified drawRect to add the following lines of code:

[code] for (CPELine *line in self.finishedLines)
{
if (line.begin.x > line.end.x && line.begin.y > line.end.y) {
[[UIColor greenColor] set];
} else if (line.begin.x > line.end.x && line.begin.y < line.end.y){
[[UIColor redColor] set];
} else if (line.begin.x < line.end.x && line.begin.y > line.end.y){
[[UIColor blueColor] set];
} else [[UIColor orangeColor] set];

    [self strokeLine:line];
}

[/code]