0

So in this piece of code i'm writing I have these shapes defined by an ArrayList of Point objects, these points represent cells on a grid. Each shape has an x and a y, which is the centre of the shape, in an absolute coordinate system - bottom left corner would be 0, 0 etc. Each of the shapes should be able to rotate around the centre. I have defined these shapes in their "north facing" orientation using coordinates relative to their centre. How would I be able to retrieve a list of absolute coordinates for each cell given the parameters x and y for the centre of the shape for each of the four directions N, E, S, W.

ArrayList<Point> adjustedCells = new ArrayList<Point>();

for(Point point : cells) {
    Point adjustedPoint;
    switch (getOrientation()) {
    case NORTH:
        adjustedPoint = new Point(x + point.x, y + point.y);
        adjustedCells.add(adjustedPoint);
    case SOUTH:
        adjustedPoint = new Point(x - point.x, y - point.y);
        adjustedCells.add(adjustedPoint);
    case EAST:
        adjustedPoint = new Point(x - point.y, y + point.x);
        adjustedCells.add(adjustedPoint);
    case WEST:
        adjustedPoint = new Point(x + point.y, y - point.x);
        adjustedCells.add(adjustedPoint);
    }
}

This is what I tried based on other code I've found but it's clearly not giving the correct coordinates. This piece is just inside a method with the parameters x and y which are the centre x and y of the shape in absolute.

Hamish
  • 37
  • 6
  • In your question, add the resulting coordinates you get from your code for a simple example shape, and add the inputs. – tkruse May 10 '20 at 13:25

1 Answers1

1

I just see a couple of issues with your code, one related to geometry and the other to how the switch statement works in Java.

Firstly the switch statement. You need to break after each case to stop "falling through" to the following case. For more detail on this take a look at Why do we need break after case statements?

Secondly the geometry. This is almost correct, you just have the EAST and WEST cases swapped. Your relative cell locations are vectors. The coordinates of a (NORTH) vector (x,y) rotated 90 degrees clockwise (EAST) are (y, -x). For a counter clockwise rotation (WEST) they're (-y, x). Repeat either of these and you get the coordinates for a 180 degree rotation (SOUTH) (-x, -y), which you already had.

for(Point point : cells) {
  Point adjustedPoint;
  switch (getOrientation()) {
  case NORTH:
      adjustedPoint = new Point(x + point.x, y + point.y);
      adjustedCells.add(adjustedPoint);
      break;
  case SOUTH:
      adjustedPoint = new Point(x - point.x, y - point.y);
      adjustedCells.add(adjustedPoint);
      break;
  case EAST:
      adjustedPoint = new Point(x + point.y, y - point.x);
      adjustedCells.add(adjustedPoint);
      break;
  case WEST:
      adjustedPoint = new Point(x - point.y, y + point.x);
      adjustedCells.add(adjustedPoint);
      break;
  }     
}
RaffleBuffle
  • 5,396
  • 1
  • 9
  • 16
  • Brilliant, I knew it was something simple like that. Could for the life of me see it though. – Hamish May 10 '20 at 14:44