Search code examples
c#winformsrotationcontrolspoint

C# Rotate 2 controls around a center point at the defaul angle in WinForms


I was trying to rotate 2 windows form button as in the following image:

example image

During their rotation, the distance between them should be 0 and when you click a label, they should "rotate" 90 degree, as like:

if (red is up and black is down)
{
    red will be down and black will be up;
}
else 
{
    red will be up and black will be down;
}

I used this method to return the desired point "location", but i couldn't obtain the desired rotation "effect":

 public static Point Rotate(Point point, Point pivot, double angleDegree)
    {
        double angle = angleDegree * Math.PI / 180;
        double cos = Math.Cos(angle);
        double sin = Math.Sin(angle);
        int dx = point.X - pivot.X;
        int dy = point.Y - pivot.Y;
        double x = cos * dx - sin * dy + pivot.X;
        double y = sin * dx + cos * dy + pivot.X;

        Point rotated = new Point((int)Math.Round(x), (int)Math.Round(y));
        return rotated;
    }

Solution

  • like in my comment it has to look like this:

    private Point calculateCircumferencePoint(double radoffset, Point center, double radius)
            {
                Point res = new Point();
    
                double x = center.X + radius * Math.Cos(radoffset);
                double y = center.Y + radius * Math.Sin(radoffset);
    
                res.X = (int)x;
                res.Y = (int)y;
    
                return res;
            }
    

    here is also a test application: https://github.com/hrkrx/TestAppCircularRotation

    EDIT: for a second button you just need to set the initial offset to Math.PI;

    EDIT2: To rotate the buttons like they cross (like the path of an 8) you need to set the radius to Sin(radoffset) or Cos(radoffset)