# Lopez

I've coded a 3D plotter, which takes the 3 x,y,z co-ordinates and puts them to the screen. I've made a cube but I can only move the camera x,y,z I want to make the camera be able to rotate it's viewpoint along x axis (or y axis, both are the same), what is the technique for doing this?
or the technique for rotating the cube?

Someone must know the technique, I've seen so many rotating cube demos in my life. Does it involve the equation of a circle?

• Ahh, doesn't matter, sorted it now. You just draw a line from the bottom of viewpoint to the top of the viewpoint, then use that line to plot the points on. Instead of simply drawing a straight line from the bottom of the viewpoint and plotting on that line.

Um, I think this is right anyway, see if I can get it right now.

• Rotations: (I'm at school and I dont have a working matrix under my nose so I try to calculate it, I might do some mistake)

A little trig:
x=r*cos(a)
y=r*sin(a)
[x^2+y^2=(r*cos(a))^2+(r*sin(a))^2 ;normal/polar circle eq]

Now we do a rotation of angle b:

x=r*cos(a+b); [cos(a+b)=cos(a)*cos(b)-sin(a)*sin(b)]
y=r*sin(a+b); [sin(a+b)=sin(a)*cos(b)+cos(a)*sin(b)]
x=r*cos(a)+cos(b)-r*sin(a)*sin(b); [r*cos(a)=x, r*sin(a)=y]
y=r*sin(a)+cos(b)+r*cos(a)*sin(b); [r*cos(a)=x, r*sin(a)=y]
Finally:
x=x*cos(b)-y*sin(b)
y=x*sin(b)+y*cos(b)

Now if we look from -Z to +Z we see:
X
|_Y 2Dplane(x,y)
From -X to +X:
Z
|_Y 2Dplane(y,z)
From -Y to +Y:
X
|_Z 2Dplane(z,x)

Then the rotation around X-Axis of angle ax:
x=x
y=y*cos(ax)-z*sin(ax)
z=y*sin(ax)+z*cos(ax)

Y-Axis ay:
x=z*sin(ay)+x*cos(ay)
y=y
z=z*cos(ay)-x*sin(ay)

Z-Axis az:
x=x*cos(az)-y*sin(az)
y=x*sin(az)+y*cos(az)
z=z

2-Axis rotation takes: 8 mul, 2 add, 2 sub per point

Rotate of 25 on ax then 30 on ay, or first 30 on ay then 25 on ax dont change the result, so the order we rotate ours point dont actually matter.

Matrix: Rotate using a matrix

Z-Axis: (that u dont need but it's simple to understand)
x=x*( cos(az)) + y*(-sin(az)) + z*0
y=x*( sin(az)) + y*( cos(az)) + z*0
z=x*0 + y*0 + z*1

This sys of eq. can be express as the multiplication betwenn a 3x3 matrix and a 3D vector:
V'=Rz*V; [Rz:matrix, V:vector, V':rotated vector]

The coefficent of Rz are:
Rz0=cos(az), Rz1=-sin(az), Rz2=0,
Rz3=sin(az), Rz4=cos(az), Rz5=0,
Rz6=0, Rz7=0, Rz8=1
where:
x = x*Rz0 + y*Rz1 + z*Rz2
y = x*Rz3 + y*Rz4 + z*Rz5
z = x*Rz6 + y*Rz7 + z*Rz8

The coefficent of Ry:
Ry0=cos(ay), Rz1=0, Rz2=sin(ay),
Ry3=0, Rz4=1, Rz5=0,
Ry6=-sin(ay), Rz7=0, Rz8=cos(ay)

The coefficent of Rx:
Rx0=1, Rz1=0, Rz2=0,
Rx3=0, Rz4=cos(ax), Rz5=-sin(ax),
Rx6=0, Rz7=sin(ax), Rz8=cos(ay)

The multiplication of two matrixs create a thirth matrix that does the both 3d trasformation in the order of the multiplication, with rotation dosn't really matter but with matrixs u can perfor other 3D traformation where the order is important indeed.

Rx*Ry=Rxy:
Rxy0 = Rx0*Ry0 + Rx1*Ry3 + Rx2*Ry6
Rxy1 = Rx0*Ry1 + Rx1*Ry4 + Rx2*Ry7
Rxy2 = Rx0*Ry2 + Rx1*Ry5 + Rx2*Ry8
Rxy3 = Rx3*Ry0 + Rx4*Ry3 + Rx5*Ry6
Rxy4 = Rx3*Ry1 + Rx4*Ry4 + Rx5*Ry7
Rxy5 = Rx3*Ry2 + Rx4*Ry5 + Rx5*Ry8
Rxy6 = Rx6*Ry0 + Rx7*Ry3 + Rx8*Ry6
Rxy7 = Rx6*Ry1 + Rx7*Ry4 + Rx8*Ry7
Rxy8 = Rx6*Ry2 + Rx7*Ry5 + Rx8*Ry8

Ok sounds bad but it isn't really

Rxy0=cos(ay)
Rxy1=0
Rxy2=sin(ay)
Rxy3=sin(ax)*sin(ay)
Rxy4=cos(ax)
Rxy5=-sin(ax)*cos(ay)
Rxy6=-cos(ax)*sin(ay)
Rxy7=sin(ax)
Rxy8=cos(ax)*cos(ay)

where:

x = x*Rxy0 + y*0 + z*Rxy2 = x*Rxy0 + z*Rxy2
y = x*Rxy3 + y*Rxy4 + z*Rxy5
z = x*Rxy6 + y*Rxy7 + z*Rxy8

2-Axis matrix rotation takes: 8 mul, 5 add per point

The camera:
The rotation for the camera it's done in the same way, but we need to rotate the world around the negative camera's angles.
Just need to know that: cos(-a)=cos(a), sin(-a)=-sin(a)
cam_Rxy0=cos(ay)
cam_Rxy1=0
cam_Rxy2=-sin(ay)
cam_Rxy3=sin(ax)*sin(ay)
cam_Rxy4=cos(ax)
cam_Rxy5=sin(ax)*cos(ay)
cam_Rxy6=cos(ax)*sin(ay)
cam_Rxy7=-sin(ax)
cam_Rxy8=cos(ax)*cos(ay)

Here the matrix's way seems to be more complicated without gaining speed from it, but where u will facing 3-axis matrix rotation, then later use to describe on a 3d space an object or a camera a 4x4 matrix, it will be clear that the matrix's way is much better.
I suggest you to use and learn how to handle it.

Mutilate[OA]