1) In order to draw the cube, I came up with a couple of helping functions.
Aside from those functions, the core of the program was fairly simple:
function newX = project(X,R,T,K)
%Project the image formation process
newX = ones(1,size(X,2));
newX = T*newX; %The translation
newX=newX+R*X; %The Rotation
newX = K* newX; %For K
newX(1,:) = newX(1,:) ./ newX(3,:);
newX(2,:) = newX(2,:) ./ newX(3,:);
newX(3,:) = newX(3,:) ./ newX(3,:);
end
And to call this function with the given parameters, one would use the following code:
X=zeros(3,8);
X(:,1) = [0 0 0]; X(:,2) = [0 0 1]; X(:,3) = [1 0 1];
X(:,4) = [1 0 0]; X(:,5) = [1 1 0]; X(:,6) = [0 1 0];
X(:,7) = [0 1 1]; X(:,8) = [1 1 1];
drawCube(X);
K = [800, 0, 250; 0, 800, 250; 0, 0, 1];
R = [ 1, 0, 0; 0, cosd(20), -sind(20); 0, sind(20), cosd(20)];
T = [0;0;100];
pam = project(X,R,T,K);
drawCube(pam);
As you can see below, this code would then give us the before and after transformation images of the cube. Since the camera is firmly placed on the Z axis however, the perspective is slightly obscure. For reassurance, I've rotated the before and after images aswell to show that they are in fact done correctly:
BEFORE:
Helping functions:
function cube = drawCube( v )
figure;
hold on;
title('cube');
xlabel('X','FontSize',18);
ylabel('Y','FontSize',18)
zlabel('Z','FontSize',18)
drawPoint(v(:,1) ,v(:,2) );
drawPoint(v(:,2) ,v(:,3) );
drawPoint(v(:,3) ,v(:,4) );
drawPoint(v(:,1) ,v(:,4) );
drawPoint(v(:,4) ,v(:,5) );
drawPoint(v(:,5) ,v(:,6) );
drawPoint(v(:,7) ,v(:,6) );
drawPoint(v(:,7) ,v(:,8) );
drawPoint(v(:,3) ,v(:,8) );
drawPoint(v(:,7) ,v(:,2) );
drawPoint(v(:,1) ,v(:,6) );
drawPoint(v(:,5) ,v(:,8) )
hold off;
cube = true;
end
__________________________________________________________________________
function g= drawPoint( p1,p2 )
plot3([p1(1),p2(1)],[p1(2),p2(2)],[p1(3),p2(3)]);
g = true;
end
AFTER:
2)