I have created a polygon (6 points). Lets call this one, outside polygon. inside the outside polygon I created smaller polygons. I want to flip all of it vertically.

I know the points of the outside polygon and I have an ArrayList for the inner polygons. I was able to flip the outside polygon. but how do I flipped the inner polygons keeping their relative positions in the new one? I know the center of the outside polygon and the flipped version.

Imagine that you are playing cards (the cards are shaped as polygons of 6 points) with and you want to flip the 4 of diamond? how do I do it? Thanks in advance.

Recommended Answers

All 6 Replies

What do you mean by flipping? Do you mean rotating the polygon or mirroring it? If you know the center of the outter polygon, you could use the point for transform process for the inner polygon. Then rotate/mirror it the same way you did with the outter polygon. Does that make sense to you?

Assuming the inner polygons have their coordinates defined in the same frame as the outer one (as ooposed to being relative to the outer polygon) then all you need to do is to apply the same absolute transform you used on the outer polygon's coordinates

When I first read the OP, my thought is similar to James -- both polygons share the same center point. But then I am not sure when I reread the OP post again. That's why I suggested to use the center point as origin point for the inner polygon in order to transform points before rotation.

/*
  The polygons below are sharing the same center point. Then the same
  rotation/mirror method can directly be applied to the inner polygon.
    +---------+
    |  +---+  |
    |  |   |  |
    |  +---+  |
    +---------+

  The polygons below are not sharing the same center point. The inner polygon
  must be rotating around the center point of the outter polygon and may need
  to transform before rotating/mirroring the inner polygon.
    +------------+
    |  +---+     |
    |  |   |     |
    |  +---+     |
    |            |
    +------------+

*/

Just consider some trivial cases where one (or more) point of the inner poly s co-located with one point of the outer poly. It's obvious that the same transform has to be applied to all the points of all the polys. (By "transform" I mean the fixed values that you add and multiply each point by; it's a subset of AffineTransform)

correction: I needed to flip horizontal.

I flipped the outer polygon (triangle shape), and I was able to move the inner polygons. but the distance is incorrect. this is a picture of what I have done,
(https://docs.google.com/drawings/d/1cPYJqxTWVu5gSHFQyHxHWSTysNzxJvNuJIwsgCQInfc/edit) https://docs.google.com/drawings/d/1cPYJqxTWVu5gSHFQyHxHWSTysNzxJvNuJIwsgCQInfc/edit

I tried this:

 for (Polygon p : polygonList) {

                    Polygon tempP = new Polygon(p.xpoints, p.ypoints, p.npoints);

                    firstPointinPolygon = new Point(p.xpoints[0], p.ypoints[0]);
                    // find frist point in the polygon 

                    float adjacent = (float) firstPointinPolygon.getX() - 400;
                    float opposite = (float) firstPointinPolygon.getY() - 400;

                    float hypotenuse = (float) Math.sqrt(opposite * opposite + adjacent * adjacent);

                    float cosine = adjacent / hypotenuse;
                    float sine = opposite / hypotenuse;

                    float endX = 400 * cosine;
                    float endY = 400 * sine;

                    float endXDelta =400-endX;
                    float endYDelta=400-endY;

                    Polygon pM = move(tempP, endX, endY);


                    polygonListMirror.add(pM);

                    tempP = new Polygon();
                }

public Polygon move(Polygon p, double xMove, double yMove) {

 // Change the values of the points for the Polygon
    for (int i = 0; i < p.xpoints.length; i++) {

        p.xpoints[i] += xMove;
        p.ypoints[i] += yMove;

    }
    return p;
}

But did not get the result, I expected. What am I doing wrong? The end result should be like the picture in this link:
(https://docs.google.com/drawings/d/1vYdWkCelWW1_NUypNhtmckBYfEMzCf6bMVtoB-AyPkw/edit) https://docs.google.com/drawings/d/1vYdWkCelWW1_NUypNhtmckBYfEMzCf6bMVtoB-AyPkw/edit

If you are mirroring on the vertical axis only and want to use distance to move/copy polygons inside, you need to use the shortest distance of each point of inner polygon from the center line of the outter polygon (mirroring axis). Subtract the distance from the x value of the center line location (y value is the same) and you will get the correct mirror point locations of inner polygons.

/*  A polygon is located away from the center line of mirroring.
                     |
                     |        .
                     |       / \
                     |      |  .'
                     |       ''
                     |
                     x

    Compute the shortest distance of each point of the polygon against the center line.
                     |
                     |---a----.     x+a
                     |---b---/ \    x+b
                     |---c--|--.'   x+c
                     |---d---''     x+d
                     |
                     x

    Then compute mirror polygon points using the distance.
                     |
     x-a    .----a---|---a----.     x+a
     x-b   / \---b---|---b---/ \    x+b
     x-c  '.--|--c---|---c--|--.'   x+c
     x-d    ''---d---|---d---''     x+d
                     |
                     x
*/
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.