Implementation of:
- Matrix multiplication
- 3D projection + a fix for points behind the camera
1 parent e32b437 commit f9dc322893cb98cf7afb0681d3a64265e489725a
@cory cory authored on 6 Nov 2022
Showing 6 changed files
View
10
src/Line.java
nextPix();
try {
img.setRGB(returnVal[0], returnVal[1], Color.HSBtoRGB(1, 1, 1));
}
catch (ArrayIndexOutOfBoundsException ref){
System.out.println(returnVal[0] + " " + returnVal[1]);
catch (ArrayIndexOutOfBoundsException ignored){
// System.out.println(returnVal[0] + " " + returnVal[1]);
}
}
} else {
for (int i = 0; i <= dy; i++){
nextPix();
try {
img.setRGB(returnVal[0], returnVal[1], Color.HSBtoRGB(.5f, 1, 1));
}
catch (ArrayIndexOutOfBoundsException ref){
System.out.println(returnVal[0] + " " + returnVal[1]);
catch (ArrayIndexOutOfBoundsException ignored){
// System.out.println(returnVal[0] + " " + returnVal[1]);
}
}
}
/*
return returnVal;
}
else {
is_initialised = false;
return null;
throw new RuntimeException("Accessed too many line pixels");
}
iteratorVal += 1;
return returnVal;
}
View
64
src/Matrix.java 0 → 100644
public class Matrix {
protected int x;
protected int y;
private double[][] items;
 
public Matrix(int _x, int _y){
x = _x; y= _y;
items = new double[y][x];
}
public double item(int _x, int _y){
return items[_y][_x];
}
public void setItem(int _x, int _y, double val){
items[_y][_x] = val;
}
public double[][] getItems() {
return items;
}
public void setItems(double[][] newItems){
items = newItems;
}
public Matrix multiply(Matrix mult) {
Matrix result = new Matrix(mult.x, this.y);
double newItem;
if(x==mult.y){
for(int rx = 0; rx< result.x; rx+=1){
for(int ry = 0; ry< result.y; ry+=1){
newItem = 0;
for(int i = 0; i<x; i+=1){
newItem += this.item(i, ry)*mult.item(rx, i);
}
result.setItem(rx, ry, newItem);
}}
} else {
throw new RuntimeException("wrong dimensions");
}
return result;
}
public Point3D multiplyPoint3(Point3D point) {
double px;double py; double pz;
if(x==3){
px = point.x * item(0,0) + point.y*item(1,0) + point.z*item(2,0);
py = point.x * item(0,1) + point.y*item(1,1) + point.z*item(2,1);
pz = point.x * item(0,2) + point.y*item(1,2) + point.z*item(2,2);
} else if(x == 4){
px = point.x * item(0,0) + point.y*item(1,0) + point.z*item(2,0) + item(3,0);
py = point.x * item(0,1) + point.y*item(1,1) + point.z*item(2,1) + item(3,1);
pz = point.x * item(0,2) + point.y*item(1,2) + point.z*item(2,2) + item(3,2);
} else {throw new RuntimeException("wrong-dimensions");}
return new Point3D(px, py, pz);
}
public Point2D multiplyPoint2(Point3D point) throws Exception {
double px;double py;
if(x==2){
px = point.x * item(0,0) + point.y*item(0,1);
py = point.x * item(1,0) + point.y*item(1,1);
} else if(x == 3){
px = point.x * item(0,0) + point.y*item(0,1) + item(0,2);
py = point.x * item(1,0) + point.y*item(1,1) + item(1,2);
} else {throw new Exception("wrong-dimensions");}
return new Point2D(px, py);
}
}
View
src/Player.java
View
src/Point3D.java
View
src/Screen.java
View
src/Triangle.java