public double[] getRotationOnXAxis(Camera camera, double y, double z) {
Matrix yAndZ = getRotationOnAxis(y, z);
yAndZ = yAndZ.multiply(getRotationMatrix(camera.xRotation));
return yAndZ.numbers[0];
}
public double[] getRotationOnYAxis(Camera camera, double x, double z) {
Matrix xAndZ = getRotationOnAxis(x, z);
xAndZ = xAndZ.multiply(getRotationMatrix(camera.yRotation));
return xAndZ.numbers[0];
}
public double[] getRotationOnZAxis(Camera camera, double x, double y) {
Matrix xAndY = getRotationOnAxis(x, y);
xAndY = xAndY.multiply(getRotationMatrix(camera.zRotation));
return xAndY.numbers[0];
}
private Matrix getRotationOnAxis(double firstPos, double secondPos) {
Matrix rotationOnAxisMatrix = new Matrix(new double[1][2]);
rotationOnAxisMatrix.numbers[0][0] = firstPos;
rotationOnAxisMatrix.numbers[0][1] = secondPos;
return rotationOnAxisMatrix;
}
private Matrix getRotationMatrix(double theta) {
Matrix rotationMatrix = new Matrix(new double[2][2]);
rotationMatrix.numbers[0][0] = Math.cos(theta);
rotationMatrix.numbers[0][1] = -Math.sin(theta);
rotationMatrix.numbers[1][0] = Math.sin(theta);
rotationMatrix.numbers[1][1] = Math.cos(theta);
return rotationMatrix;
}
public double getProjectedX(Camera camera) {
double x = position.numbers[0][0] + middle.numbers[0][0] - camera.x;
double y = position.numbers[0][1] + middle.numbers[0][1] - camera.y;
double z = position.numbers[0][2] + middle.numbers[0][2] - camera.z;
System.out.println(x + "," + y + "," + z);
//rotate
double[] newPos = new double[2];
newPos = getRotationOnXAxis(camera, y, z);
y = newPos[0];
z = newPos[1];
newPos = getRotationOnYAxis(camera, x, z);
x = newPos[0];
z = newPos[1];
newPos = getRotationOnZAxis(camera, x, y);
x = newPos[0];
y = newPos[1];
return translate(x, z, camera.focalLength);
}
public double getProjectedY(Camera camera) {
double x = position.numbers[0][0] + middle.numbers[0][0] - camera.x;
double y = position.numbers[0][1] + middle.numbers[0][1] - camera.y;
double z = position.numbers[0][2] + middle.numbers[0][2] - camera.z;
//rotate
double[] newPos = new double[2];
newPos = getRotationOnXAxis(camera, y, z);
y = newPos[0];
z = newPos[1];
newPos = getRotationOnYAxis(camera, x, z);
x = newPos[0];
z = newPos[1];
newPos = getRotationOnZAxis(camera, x, y);
x = newPos[0];
y = newPos[1];
return translate(y, z, camera.focalLength);
}
@SuppressWarnings("deprecation")
private double translate(double xOrY, double z, double focalLength) {
//System.out.println(focalLength + "*" + xOrY + " " + (focalLength + "+" + position.numbers[0][2] + "+" + middle.numbers[0][2] + "-" + camera.z));
return new BigDecimal(focalLength*xOrY).divide(
new BigDecimal(focalLength+z), BigDecimal.ROUND_HALF_DOWN).doubleValue();
}