Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to rotate a point around another point

I am writing a game. I need to know how to rotate point a around point b by a given number of degrees. I am writing this in java and it is going to be part of my class, Point.

like image 408
user2277362 Avatar asked Dec 15 '22 00:12

user2277362


1 Answers

double x1 = point.x - center.x;
double y1 = point.y - center.y;

double x2 = x1 * Math.cos(angle) - y1 * Math.sin(angle));
double y2 = x1 * Math.sin(angle) + y1 * Math.cos(angle));

point.x = x2 + center.x;
point.y = y2 + center.y;

This approach uses rotation matrices. "point" is your point a, "center" is your point b.

like image 196
SDLeffler Avatar answered Dec 26 '22 14:12

SDLeffler