Hi. I have a Vector of Vectors that I need to rotate 90 degrees clockwise and 90 degrees counter-clockwise.

I got it to rotate Clockwise, but whatever I try, I can't get it to rotate counter clock-wise. The 2D Vector is not always going to be a square. It will mostly be a rectangle.

Can anyone help with the counter-clockwise rotation? everything that I've found only applies to Object[][]'s that are square...

Here is what I have to make it rotate CW successfully:

// tiles = Vector<Vector<MapTile>>();
public void rotateCW(){
        int d1 = tiles.size();
        int d2 = tiles.firstElement().size();
        fieldHeight = d1;
        fieldWidth = d2;

        Vector v = new Vector<Vector<MapTile>>(d2);
        for(int i=0;i<d2;i++){
            Vector e = new Vector<MapTile>(d1);
            for(int j=0;j<d1;j++){
                e.add(tiles.get(d1-1-j).get(i));
            }
            v.add(e);
        }
        tiles = v;
        rotation++;
        if(rotation==4)rotation = 0;
    }

Recommended Answers

All 3 Replies

Here's a really easy solution (although it's probably not what are hoping for!)

public void rotateCCW() {
   rotateCW(); rotateCW(); rotateCW(); 
}

:-)

@James: Hahaha, yeah, I'd thought about that =P, but the objects in the Vectors take up (what I believe to be) a nice chunk of space, and moving them that many times in takes more time than I'd like...

I'm making a game, and I want the view of the field to visibly rotate when the method is called (Like in Final Fantasy Tactics for the PS1). But that's just a graphics thing. This is the data part, and I could do it that way, but it takes more time.... hmmm.....

Well, as it turns out, that code made it rotate counter-clockwise, my formula for drawing was flip-flopped, and made it look like it was rotating incorrectly (which it was). Here are the formulas for rotating both ways: (I figured the other one out ;) )

public Vector<Vector<E>> rotateCW(Vector<Vector<E>> original){
    int oldWidth = original.size();
    int oldHeight = original.firstElement().size();

    Vector rotated = new Vector<Vector<E>>(oldHeight);
    for(int i=0;i<oldHeight;i++){
        Vector e = new Vector<E>();
        for(int j=0;j<oldWidth;j++){
            e.add(0,original.get(oldWidth-1 - j).get(i));
        }
        rotated.add(e);
    }
    return rotated;
}

public Vector<Vector<E>> rotateCCW(Vector<Vector<E>> original){
    Vector rotated = new Vector<Vector<E>>(orininal.get(0).size());

    for(int i=0;i<orginal.get(0).size();i++){
        Vector col = new Vector<E>(original.size());
        for(int j=original.size()-1;j>=0;j--){
            col.add(original.get(j).get(i));
        }
        rotated.add(col);
    }
    return rotated;
}

Yes, I know that there are no comments, sorry >.< If you look through it enough, it should be clear-ish. If anyone has questions, please PM me.

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.