C:\> Rostislav Persion's Projects

.:: C# Point Rotation ::.
Rotate a point around origin




This is a very simple C# console application that rotates a point around its origin.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace Rotation1
{

    class Program
    {

        static Point rotate_point(Point origin, Point p1, double angle)
        {
            double angl = Math.Atan2(p1.Y - origin.Y, p1.X - origin.X); double ang2 = angl + (angle * (Math.PI / 180));
            double dist = Math.Sqrt(Math.Pow(p1.X - origin.X, 2) + Math.Pow(p1.Y - origin.Y, 2)); double x2 = (Math.Cos(ang2) * dist) + origin.X; double y2 = (Math.Sin(ang2) * dist) + origin.Y;
            Point p2 = new Point(Convert.ToInt32(x2), Convert.ToInt32(y2)); return p2;
        }
        static void Main(string[] args)
        {
            while (true)
            {
                Console.Write("ORIGIN X: ");
                int origin_x = Convert.ToInt32(Console.ReadLine()); Console.Write("ORIGIN Y: ");
                int origin_y = Convert.ToInt32(Console.ReadLine()); Console.Write("POINT X: ");
                int new_x = Convert.ToInt32(Console.ReadLine());
                Console.Write("POINT Y: ");
                int new_y = Convert.ToInt32(Console.ReadLine());
                Console.Write("DELTA ANGLE: ");
                int new_ang = Convert.ToInt32(Console.ReadLine());
                Point origin = new Point(origin_x, origin_y);
                Point ptl = new Point(new_x, new_y);
                Point pt2 = rotate_point(origin, ptl, new_ang);
                Console.WriteLine("NEW ... X: " + pt2.X + " Y: " + pt2.Y); Console.ReadLine();
            }
        }
    }

}