Discuss Scratch

game_pr0grammer
Scratcher
500+ posts

How to rotate a matrix?

I am making a pen tetris and the shapes work by using a matrix. This is the matrix for a T-shaped piece.

#1: ~ ~ ~
#2: 1 1 1
#3: ~ 1 ~

I searched up how to make matrices transpose and I programmed it into my game but it doesn't seem to work. Here is my project:
https://beta.scratch.mit.edu/#251889022

I used the scratch 3.0 website because I don't want to publish an unfinished project..

The custom block that rotates the player is called “rotate player ()”

EDIT:

The way that it's supposed to work is by transposing the matrix.

Last edited by game_pr0grammer (Dec. 11, 2018 01:08:36)

deck26
Scratcher
1000+ posts

How to rotate a matrix?

Not sure you should be referring to i2 in the ‘set x y of player to id script. Shouldn’t it be i4?
game_pr0grammer
Scratcher
500+ posts

How to rotate a matrix?

no

Last edited by game_pr0grammer (Nov. 18, 2018 16:00:41)

game_pr0grammer
Scratcher
500+ posts

How to rotate a matrix?

i fixed the rotation (it was actually a bug with the set of player block) but now when it rotates two items get removed… why?
deck26
Scratcher
1000+ posts

How to rotate a matrix?

game_pr0grammer wrote:

i fixed the rotation (it was actually a bug with the set of player block) but now when it rotates two items get removed… why?
Yes, you replaced i2 with i4 like I suggested by the look of things!
game_pr0grammer
Scratcher
500+ posts

How to rotate a matrix?

deck26 wrote:

game_pr0grammer wrote:

i fixed the rotation (it was actually a bug with the set of player block) but now when it rotates two items get removed… why?
Yes, you replaced i2 with i4 like I suggested by the look of things!

A very astute observation.
deck26
Scratcher
1000+ posts

How to rotate a matrix?

So do you still have a problem? What items are removed?
game_pr0grammer
Scratcher
500+ posts

How to rotate a matrix?

deck26 wrote:

So do you still have a problem?

well obviously.

deck26 wrote:

What items are removed?
after further observation, i realized the tetranimo does not actually rotate; it only removes some items. and also, why won't you actually check out the project?
deck26
Scratcher
1000+ posts

How to rotate a matrix?

game_pr0grammer wrote:

deck26 wrote:

So do you still have a problem?

well obviously.

deck26 wrote:

What items are removed?
after further observation, i realized the tetranimo does not actually rotate; it only removes some items. and also, why won't you actually check out the project?
How do you think I identified the first problem if I didn't look at the project?

I'm happy to help if you provide enough info for me to do so but you've provided very little so far. You haven't answered my last question which would tell me what's actually happening.
game_pr0grammer
Scratcher
500+ posts

How to rotate a matrix?

When I press “up,” the last item from the two top rows get removed.
When I press “down,” the tetranimo flips up/down, and after that, the last item from the two bottom rows get removed.
game_pr0grammer
Scratcher
500+ posts

How to rotate a matrix?

bump
deck26
Scratcher
1000+ posts

How to rotate a matrix?

Your custom block ‘set x y of player to id’ should repeat the length of the list item rather than the length minus 1.

I don't know what the first part of your rotate script is supposed to be doing - it identifies letters in the player entries but just puts them back in the same place. It is less confusing if you start i and i2 at 1 instead of 0 - then you don't have to add one every time you use them!

If you swap temp1 and temp2 in the calls to ‘set x y of player to id’ the first part of the script then works but it reflects the array across the diagonal. So I'm still baffled about the first part of that custom block.

If we take that first bit out the code for dir > 0 reflects the array in a vertical line down the middle and dir < 0 reflects it in a horizontal line. So you're mainipulating the array but not in the right way. Use a test array like 123, 456, 789 so you can see this - if you test with something that has symnmetry you might not see what your scripts are doing.

Can you fix it from there?
RokCoder
Scratcher
1000+ posts

How to rotate a matrix?

I was playing around with this and @deck26 gave a nice answer while I was doing so. Might as well paste what I came up with anyway

waituntilkeyright arrowpressed?orkeyleft arrowpressed?Rotatekeyright arrowpressed?enddefineRotateClockwise?deleteallofTemprepeatlengthofPlayeradditem1ofPlayertoTempdelete1ofPlayerrepeatlengthofitem1ofTempaddtoPlayersetdelta src jto1-2*Clockwise?setdelta dst jto0-deltasrcjsetstart dst jtoClockwise?+lengthofTemp*notClockwise?setsrc jtonotClockwise?+lengthofPlayer*Clockwise?repeatlengthofTempsetsrc ito1setdst jtostartdstjrepeatlengthofPlayerreplaceitemdstjofPlayerwithjoinitemdstjofPlayerlettersrciofitemsrcjofTempchangesrc iby1changedst jbydeltadstjchangesrc jbydeltasrcj
game_pr0grammer
Scratcher
500+ posts

How to rotate a matrix?

RokCoder wrote:

I was playing around with this and @deck26 gave a nice answer while I was doing so. Might as well paste what I came up with anyway

snip

wow that's really complicated since i don't know what each part does

just tell me how to transpose matrices then, please
game_pr0grammer
Scratcher
500+ posts

How to rotate a matrix?

oh nevermind it was just a problem with the set of player block because i was overthinking something

Last edited by game_pr0grammer (Dec. 11, 2018 01:32:39)

RokCoder
Scratcher
1000+ posts

How to rotate a matrix?

game_pr0grammer wrote:

just tell me how to transpose matrices then, please

I thought you wanted to rotate the matrix, not transpose it? The above script will rotate your n x m player matrix either clockwise or anticlockwise. Isn't that what you needed for your project?
game_pr0grammer
Scratcher
500+ posts

How to rotate a matrix?

RokCoder wrote:

I thought you wanted to rotate the matrix, not transpose it? The above script will rotate your n x m player matrix either clockwise or anticlockwise. Isn't that what you needed for your project?

~ ~ ~ ~ 1 ~
1 1 1 transposed is 1 1 ~
~ 1 ~ ~ 1 ~

the original code was supposed to transpose it and then flip it across an axis based on whether it's rotating counterclockwise or not
deck26
Scratcher
1000+ posts

How to rotate a matrix?

As per my earlier post if you sort out the first part of the rotate script it gives you the transposed matrix - reflection along the main diagonal. So if you want that you nearly had it but you were putting the values you extracted back in the places you got them from.

If you want to talk about rotation/reflection/transposition it is much better if you use a non-symmetrical matrix.

game_pr0grammer wrote:

~ ~ ~ ~ 1 ~
1 1 1 transposed is 1 1 ~
~ 1 ~ ~ 1 ~

could equally well be a rotation 90 degrees anti-clockwise.

1 2 3
4 5 6
7 8 9

is a much better example as we can then tell the difference between the different operations.

Powered by DjangoBB