I have a circular graph with a radial cursor on it, and the cursor moves to where you touch on the iPhone screen, but I need the cursor to remain the same length, even if the touch occurs close to the origin of the graph or outside the graph. I have no idea how to do this. Any help would be greatly appreciated
views:
48answers:
2Determine the angle between where you want the line to start and the end point, then draw a line of whatever length you like starting from the start point and going only as far as you want the length? Or did I misunderstand the question?
arctangent( (y2-y1)/(x2-x1) )
I'm not familiar with these methods, but CGContextAddLineToPoint
should work with @eruciform's technique if you find the appropriate point based on the angle.
angle = arctan((y2 - y1) / (x2 - x1))
x = cos(angle) * length
y = sin(angle) * length
And draw the line from (x1, y1) to (x, y).
Alternatively, you could just use the fact that the line you're drawing is proportional to the one formed by the original point, and do some Pythagorean stuff:
original length = sqrt((y2 - y1)^2 + (x2 - x1)^2)
ratio = desired length / original length
x = ((x2 - x1) * ratio) + x1 y = ((y2 - y1) * ratio) + y1
Again, draw the line from (x1, y1) to (x, y).