J2me - Rotate an array of 2d points in increments of 45 degrees

I am working on a bagel cell phone game that works on a grid. Attacks / objects in this game act by targeting tiles according to the template relative to the user. Patrun usually refers to the direction the user is aiming, for example, in a screenshot of a template

Point2d[] damageTiles = new Point2d[4];

damageTiles[0] = new Point2d(0, -1);
damageTiles[1] = new Point2d(0, -2);
damageTiles[2] = new Point2d(1, -2);
damageTiles[3] = new Point2d(-1, -2);

shows relative to a temporary enemy (yellow box) pointing up.

I currently have simple code to rotate an array of templates 90 degrees, as shown here . My question is: is there an easy way to rotate an array of 2d points by 45 degrees, allowing my attacks / objects to shoot diagonally, preferably without the use of floating point math, since it tends to work slowly on many phones (or so I hear). This is probably a trivial question for anyone familiar with graphical programming, but I was struck by the case of the code block.

My current rotation code is shown below. Now I understand that the best way to do this is to take an angle instead of a “direction” and rotate the points by that angle (deflecting angles that are not a multiple of 45, of course).

private Point2d[] rotateList(Point2d[] points, int direction) {
    for (int i = 0; i < points.length; i++) {

        if (direction == ROTATE_LEFT) {
            int temp = points[i].x;
            points[i].x = points[i].y;
            points[i].y = -1 * temp;
        }
        else if (direction == ROTATE_RIGHT) {
            int temp = points[i].x;
            points[i].x = -1 * points[i].y;
            points[i].y = temp;
        }

    }

    return points;
}
+3
3

- , , 45 - 90 45.

original:
.....
.###.
..#..
..o..

alternative A:
.....
.##..
.##..
...o.

alternative B:
..#..
.#...
#.#..
...o.

.

45- "", 90 , 45- , 90- .

+2

, 2D ( x, y, ), (. wikipedia)

[x']   [cos(theta)  -sin(theta)] [x]
[y'] = [sin(theta)   cos(theta)] [y]

theta = 45 ,

 x' = 0.7071*x - 0.7071*y
 y' = 0.7071*x + 0.7071*y

( 0.7071 sqrt (2)/2, 4 )

0
source

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


All Articles