`move`

.
*
* @param x Handle's new x position
* @param y Handle's new y position
*
* @return True if the handle is far enough from the position that
* rotation would occur as the handle is moved
*/
synchronized public boolean reset (double x, double y)
{
this.hx = x;
this.hy = y;
this.handleSet = true;
this.azimuth = 0.0;
double diffx = this.hx - this.x;
double diffy = this.hy - this.y;
this.handleDistance =
Math.sqrt (diffx * diffx + diffy * diffy);
return this.handleDistance > this.nonRotateRadius;
}
/**
* Move the handle, and move and rotate the position accordingly.
* The handle's position must have been set before (so the typical
* pattern is to call `reset`

, then call
* `move`

zero or more times, and then repeat this
* cycle multiple times.
*
* @param newx Handle's new X-position
* @param newy Handle's new Y-position
*/
synchronized public void move (double newx, double newy)
{
// Handle must have been set already.
if (! this.handleSet) return;
// Get vector to the handle's original position
double olddiffx = this.hx - this.x;
double olddiffy = this.hy - this.y;
// Get vector to the handle's new position
double newdiffx = newx - this.x;
double newdiffy = newy - this.y;
// If the handle was far enough away ...
if (this.handleDistance > this.nonRotateRadius)
{
// Rotate the piece as well as move it. Turn the position
// by the angle between the handle's original bearing, and
// its new bearing.
double oldangle = Math.atan2 (olddiffy, olddiffx);
double newangle = Math.atan2 (newdiffy, newdiffx);
double anglediff = newangle - oldangle;
this.azimuth += anglediff;
this.azimuth %= 2.0 * Math.PI;
// And the position should be the same distance away from
// the handle's new position, along the new angle.
this.x = newx - this.handleDistance * Math.cos(newangle);
this.y = newy - this.handleDistance * Math.sin(newangle);
}
else
{
// Simply translate the position by the vector the handle
// moved.
this.x += newx - this.hx;
this.y += newy - this.hy;
}
// Update the handle to its new position, ready for another
// call to move(...).
this.hx = newx;
this.hy = newy;
}
}