Get coordinates after rotation

I have a usercontrol that contains a rectangle and 2 ellipses on the left and right edges of the rectangle. I am interested to know the coordinates of the user control after the translation has occurred and the rendertransform has rotated.

The user control is contained in the canvas.

EDIT: After searching the Internet for a while, I was able to find the answer to my question here http://forums.silverlight.net/forums/p/136759/305241.aspx , so I thought I posted a link for other people having this problem.

I put Thomas Petricek's message as an answer because it was the closest to the solution.

+3
source share
4 answers

, ( ):

public Point RotatePoint(float angle, Point pt) { 
   var a = angle * System.Math.PI / 180.0;
   float cosa = Math.Cos(a), sina = Math.Sin(a);
   return new Point(pt.X * cosa - pt.Y * sina, pt.X * sina + pt.Y * cosa);
}

, . , , . sin cos . . ( ) .

+7

[4,6] 36 . ?

:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            // Origin of the point
            Point myPoint = new Point(4, 6);
            // Degrees to rotate the point
            float degree = 36.0F;

            PointF newPoint = RotatePoint(degree, myPoint);

            System.Console.WriteLine(newPoint.ToString());
            System.Console.ReadLine();
        }

        public static PointF RotatePoint(float angle, Point pt)
        {
            var a = angle * System.Math.PI / 180.0;
            float cosa = (float)Math.Cos(a);
            float sina = (float)Math.Sin(a);
            PointF newPoint = new PointF((pt.X * cosa - pt.Y * sina), (pt.X * sina + pt.Y * cosa));
            return newPoint;
        }
    }
}
+2

Use the Transform method of the RotateTransform object - give it a point with the coordinates you want to convert, and it will rotate it.

0
source

Extension methods for those who want to revolve around a non-zero center:

public static class VectorExtentions
{
    public static Point Rotate(this Point pt, double angle, Point center)
    {
        Vector v = new Vector(pt.X - center.X, pt.Y - center.Y).Rotate(angle);
        return new Point(v.X + center.X, v.Y + center.Y);
    }

    public static Vector Rotate(this Vector v, double degrees)
    {
        return v.RotateRadians(degrees * Math.PI / 180);
    }

    public static Vector RotateRadians(this Vector v, double radians)
    {
        double ca = Math.Cos(radians);
        double sa = Math.Sin(radians);
        return new Vector(ca * v.X - sa * v.Y, sa * v.X + ca * v.Y);
    }
}
0
source

Source: https://habr.com/ru/post/1796025/


All Articles