swing - Java Tetris - Using transpose to rotate a piece -
i'm building tetris in java fun side project , working on rotations.
i hard coding each rotation proving quite tedious.
that being said, going try matrix rotations using linear algebra, on mathematics.se recommended try transposing. given description, tried drawing out. did drawings correct?
from that, i'm not trying code transpose, lost in code.
public void gettranspose() { tile[][] gridtranspose = transpose(); system.out.println(); system.out.println("b`:"); (int j = 0; j < gridtranspose.length; j++) { (int = 0; < gridtranspose[j].length-1; i++) { system.out.print("(" + + ", " + j + ") "); } system.out.println(); } } public tile[][] transpose() { tile gridt[][]; gridt = new tile[width][height]; system.out.println("b:"); for(int = 0; < grid.length; i++) { for(int j = 0; j < grid[i].length-1; j++) { system.out.print("(" + + ", " + j + ") "); gridt[j][i] = grid[i][j]; } system.out.println(); } return gridt; }
this outputs seemingly correct transposed graph?
b:
(0, 0) (0, 1) (0, 2) (0, 3) (0, 4) (0, 5) (0, 6) (0, 7) (0, 8) (1, 0) (1, 1) (1, 2) (1, 3) (1, 4) (1, 5) (1, 6) (1, 7) (1, 8) (2, 0) (2, 1) (2, 2) (2, 3) (2, 4) (2, 5) (2, 6) (2, 7) (2, 8) (3, 0) (3, 1) (3, 2) (3, 3) (3, 4) (3, 5) (3, 6) (3, 7) (3, 8) (4, 0) (4, 1) (4, 2) (4, 3) (4, 4) (4, 5) (4, 6) (4, 7) (4, 8) (5, 0) (5, 1) (5, 2) (5, 3) (5, 4) (5, 5) (5, 6) (5, 7) (5, 8) (6, 0) (6, 1) (6, 2) (6, 3) (6, 4) (6, 5) (6, 6) (6, 7) (6, 8) (7, 0) (7, 1) (7, 2) (7, 3) (7, 4) (7, 5) (7, 6) (7, 7) (7, 8) (8, 0) (8, 1) (8, 2) (8, 3) (8, 4) (8, 5) (8, 6) (8, 7) (8, 8) (9, 0) (9, 1) (9, 2) (9, 3) (9, 4) (9, 5) (9, 6) (9, 7) (9, 8)
b`:
(0, 0) (1, 0) (2, 0) (3, 0) (4, 0) (5, 0) (6, 0) (7, 0) (8, 0) (0, 1) (1, 1) (2, 1) (3, 1) (4, 1) (5, 1) (6, 1) (7, 1) (8, 1) (0, 2) (1, 2) (2, 2) (3, 2) (4, 2) (5, 2) (6, 2) (7, 2) (8, 2) (0, 3) (1, 3) (2, 3) (3, 3) (4, 3) (5, 3) (6, 3) (7, 3) (8, 3) (0, 4) (1, 4) (2, 4) (3, 4) (4, 4) (5, 4) (6, 4) (7, 4) (8, 4) (0, 5) (1, 5) (2, 5) (3, 5) (4, 5) (5, 5) (6, 5) (7, 5) (8, 5) (0, 6) (1, 6) (2, 6) (3, 6) (4, 6) (5, 6) (6, 6) (7, 6) (8, 6) (0, 7) (1, 7) (2, 7) (3, 7) (4, 7) (5, 7) (6, 7) (7, 7) (8, 7) (0, 8) (1, 8) (2, 8) (3, 8) (4, 8) (5, 8) (6, 8) (7, 8) (8, 8) (0, 9) (1, 9) (2, 9) (3, 9) (4, 9) (5, 9) (6, 9) (7, 9) (8, 9)
so questions are:
1) drawing above correct interpretation of description?
2) correctly generating transposed graph?
3) if so, how should paint rotated block... should replace grid[row][col]
transpose()
?
public void paintcomponent(graphics g) { g.setcolor(color.black); g.fillrect(0, 0, getwidth(), getheight()); for(int row = 0; row < grid.length; row++) { for(int col = 0; col < grid[row].length; col++) { if(grid[row][col] != null) { //if there non-null space, tetris piece.. fill red if (grid[row][col].getcolor() != null) { g.setcolor(grid[row][col].getcolor()); g.fillrect(row * tilesize, col * tilesize, tilesize, tilesize); g.setcolor(color.white); g.drawstring("(" + row + ", " + col + ")", row * tilesize, col * tilesize+10); } } } } }
thanks!
transposing alone won't work of normal tetris pieces. example, take piece shaped this:
xx_ _xx ___
when transpose it, end this:
x__ xx_ _x_
so rather think particular piece, think happens when work square corners labeled:
a_b ___ d_c
this make clear if rotation or not. let's @ transpose:
a_d ___ b_c
in order clockwise rotation, can flip sideways:
d_a ___ c_b
performing same operation again should give full half rotation started. take transpose:
d_c ___ a_b
and flip it:
c_d ___ b_a
so in order clockwise rotation, take transpose , flip it. happens if opposite, flip , take transpose? working previous orientation, if flip it:
d_c ___ a_b
now take transpose:
d_a ___ c_b
and we're had after 1 rotate - counterclockwise rotation. transposing alone won't work, transposing combined horizontal flip need!
as questions,
i'm not sure drawing correct interpretation. based on comments left suspect may have misunderstood them, why made drawings above show needs happen.
yes, correctly transposed graph. don't want use
gridtranspose[j].length-1
in loops, cuts off 1 column.to paint rotated block, suggest having separate variable holding current piece. if piece @
x,y
, put inpaintcomponent
method:for(int row = 0; row < piece.length(); row++) { for(int col = 0; col < piece.width(); piece++) { if(piece.hasblockat(row, col) { g.setcolor(piece.getcolorat(row, col)); g.fillrect((row+x) * tilesize, (col+y) * tilesize, tilesize, tilesize); } } }
Comments
Post a Comment