# Silver Solution

#1

My method used vectors to relate the vector created by the self.view’s width boundary and by the vector created by a user’s touchMoved UIResponder. Simply using the dot product of two vectors, I was able to assign a stroke color based on the angle between the two vectors.

BNRDrawView.m

``````struct vector
{
double x;
double y;
};

double dotProduct(struct vector u, struct vector v)
{
return u.x * v.x + u.y * v.y;
}

double magnitude(struct vector u)
{
return sqrt((pow(u.x,2)+pow(u.y,2))*1.0);
}
struct vector createVectorWithPoints(CGPoint first, CGPoint second)
{
struct vector newVector;
newVector.x = second.x - first.x;
newVector.y = second.y - first.y;
return newVector;
}

- (double)angleBetweenBoundsWidthAndVector:(struct vector)first
{
double angle = 0.0;
CGPoint beginPoint;
CGPoint endPoint;
beginPoint.x = 0;
beginPoint.y = self.bounds.size.height;
endPoint.x = self.bounds.size.width;
endPoint.y = self.bounds.size.height;

struct vector u = first;
struct vector v = createVectorWithPoints(beginPoint, endPoint);

angle = acos(dotProduct(u, v)/(magnitude(u)*magnitude(v)))*180/M_PI;
return angle;
}

- (UIColor *)getColorForLine:(BNRLine *)line
{
struct vector v = createVectorWithPoints(line.begin, line.end);
double angle = (int)[self angleBetweenBoundsWidthAndVector:v];

NSLog(@"Angle = %f\n", angle);
if(angle >= 0.0 && angle < 30.0) return [UIColor blueColor];
if(angle >= 30.0 && angle < 60.0) return [UIColor greenColor];
if(angle >= 60.0 && angle < 90.0) return [UIColor orangeColor];
if(angle >= 90.0 && angle < 120.0) return [UIColor purpleColor];
if(angle >= 120.0 && angle < 150.0) return [UIColor yellowColor];
if(angle >= 150.0 && angle < 180.0) return [UIColor brownColor];
else return [UIColor blackColor];
}

- (void)drawRect:(CGRect)rect
{
[[UIColor blackColor] set];

for(BNRLine *line in self.finishedLines)
{
UIColor *newColor = [self getColorForLine:line];
[newColor set];
[self strokeLine:line];
}

[[UIColor redColor] set];

for(NSValue *key in self.linesInProgress)
{
[self strokeLine:self.linesInProgress[key]];
}
}
``````