#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# Invert x-axis of a matrix

edited March 2014

I already asked this question in the hardware section while announcing my Oculus Rift solution:

How do i invert the x-axis of a 4x4 matrix before applying it to the sketch via applyMatrix()?

Any idea? I haven't found a simple solution in Processing yet. Next i'll have a look at javax.vecmath.Matrix4f.

Tagged:

• Dunno, but I'm gonna risk an attempt answer -> `scale(-1, 0);` :-SS

• edited March 2014

Assuming your 4x4 matrix is in this form:

Rx1, Rx2, Rx3, x
Ry1, Ry2, Ry3, y
Rz1, Rz2, Rz3, z
0.0, 0.0, 0.0, 1.0

You would need to multiply the top right element by -1. This would probably be element [3] if it is a single dimension array representation or element [0][3] if it is a double array.

• If it is being done with matrix multiplication this would do it:

-1, 0, 0, 0
0, 1, 0, 0
0, 0, 1, 0
0, 0, 0, 1

• edited March 2014

Like @GoToLoop wrote, using scale(-1.0, 1.0) will invert the x-axis and produce the following matrix:

``````-1.0  0.0  0.0  0.0
-0.0  1.0  0.0  0.0
-0.0  0.0  1.0  0.0
-0.0  0.0  0.0  1.0
``````

You can also multiply the matrix "by hand", like @asimes mentioned, using applyMatrix():

``````applyMatrix(-1.0,  0.0,  0.0,  0.0,
0.0,  1.0,  0.0,  0.0,
0.0,  0.0,  1.0,  0.0,
0.0,  0.0,  0.0,  1.0);
``````

A little example - without the scale() call the arrow would be facing right:

``````void setup() {
size(400, 400, P2D);
noStroke();
fill(#ffffff);
}

void draw() {

background(#000000);

translate(width / 2, height / 2);
scale(-1.0, 1.0);

beginShape();
vertex(0, -20);
vertex(60, -20);
vertex(60, -60);
vertex(120, 0);
vertex(60, 60);
vertex(60, 20);
vertex(0, 20);
endShape(CLOSE);

}
``````
• Thanks for the answers! I'll implement it next week and hopefully publish the Oculus Rift code.

• No problem. Would love to see your Code! And don't forget to mark the correct answer/answers. It will help others to find the solution more quickly. ;)