1
votes

I am having trouble plotting a new CGPoint when given an origin, distance and angle. The task is pretty simple: I have a line with three edit handles attached to it - one on each end and one in the center. When the the end handles are dragged, the line is moved relative to the handle being dragged. That functionality is working properly. When the center handle is dragged, the two endpoint handles should maintain their relationship to one another as shown in the image below. So when dragging the center handle the two other handles should move with it.

enter image description here

Here is my current code to plot the points:

func pointFromPoint(origin:CGPoint, distance:Double, degrees:Double) -> CGPoint {
    var endPoint = CGPoint()
    endPoint.x = CGFloat(distance * cos(degrees) + Double(origin.x))
    endPoint.y = CGFloat(distance * sin(degrees) + Double(origin.y))
    return endPoint
}

When using this function, the new CGPoint locations seems to fall in random locations. Can anyone spot anything wrong in my math? Thanks!

1
The trig function need radians, not degrees, I think.paulvs
You know the x and y differences that the center one moved based on the x and y differences of the start and stop touch motions. why not just use those as your sole values? Move all three points by this much x and this much y...Putz1103
@paulvs Yes. I think you should put that as an answer.jlasierra

1 Answers

0
votes

Most trigonometry functions need radians, not degrees.

Also, as @Putz1103 pointed out, you can probably use the delta x and delta y of the UITouch of the movement instead of calculating the new position of the ends based on the center point's movement.