// Transform rotated rectangle while dragging bottom-right handle // The top-left point will not move! // For a rectangle with top-left at x, y // A = top-left point (A' after rotation) // C = bottom-right point (C' after rotation) public void AdjustRectangle(Rectangle rectangle, Point bottomRight, float angle) { // Establish the center of the rectangle cx, cy var center = new PointF( rectangle.X + rectangle.Width / 2, rectangle.Y + rectangle.Height / 2 ); // Get rotated top-left point var rotatedA = rotate(rectangle.Location, center, angle); // Get the new center by finding the midpoint between A' and C'. var newCenter = new PointF( (rotatedA.X+ bottomRight.X) / 2, (rotatedA.Y + bottomRight.Y) / 2 ); // Get new top-left and new bottom-right points var newTopLeft = rotate(rotatedA,newCenter,-angle ); var newBottomRight = rotate(bottomRight,newCenter,-angle ); // Rotate point A(x,y) around center cx, cy by angle // return A' (rotated top-left) PointF rotate(PointF p, PointF c, double a) { return new PointF().Set( (p.X - c.X) * Math.Cos(a) - (p.Y - c.Y) * Math.Sin(a) + c.X, (p.X - c.X) * Math.Sin(a) + (p.Y - c.Y) * Math.Cos(a) + c.Y ); } // Apply new transform on rotated Rectangle rectangle.Location = newTopLeft.ToPoint(); rectangle.Size = new SizeF( newBottomRight.X - newTopLeft.X, newBottomRight.Y - newTopLeft.Y ).ToSize(); }