Array rotation

  • Hello! Please help with array rotation. 90 degrees clockwise for example.

    The first image is the original array. The second is an array after turning clockwise.

    How can i do this?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • If the array is square you can set another array of the same size to be a rotated version. Here's a simplified version.

    for CW 90 degrees

    array1: for each xy
    -- array2: set at (array1.width-1-array1.curY, array1.curX) to array1.curValue

    for CCW 90 degrees

    array1: for each xy
    -- array2: set at (array1.curY, array1.width-1-array1.curX) to array1.curValue

    Long version on how that's derived

    2d rotation is defined by these formulas:

    newX = (x-centerX)*cos(angle)-(y-centerY)*sin(angle) +centerX
    newY = (x-centerX)*sin(angle)+(y-centerY)*cos(angle) +centerY

    Then we can simplify things when using say 90 degrees for the angle since cos(90)=0 and sin(90)=1.

    newX = -(y-centerY)+centerX
    newY = (x-centerX)+centerY

    Then we can simplify further since the array is square so centerX=centerY, and centerX = (array.width-1)/2. Plug that into the formulas and we get:

    newX = -y+array.width-1
    newY = x

    Using similar logic you could do rotation of non square arrays, you'd just have to flip the x and y sizes of the second array.

    I suppose you could do something clever to rotate an array in place, but I think it's much simpler to just save the rotation on something else and then copy it back to the original if needed.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)