Newer
Older
EPQ-3D-renderer / src / Matrix.java
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 getItem(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.getItem(i, ry)*mult.getItem(rx, i);
                    }
                    result.setItem(rx, ry, newItem);
            }}
        } else {
            throw new RuntimeException("wrong dimensions");
        }
        return result;
    }
    public void multiplyPoint3to(Point3D point, Point3D result) {
        if(x==3){
            result.x = point.x * getItem(0,0) + point.y* getItem(1,0) + point.z* getItem(2,0);
            result.y = point.x * getItem(0,1) + point.y* getItem(1,1) + point.z* getItem(2,1);
            result.z = point.x * getItem(0,2) + point.y* getItem(1,2) + point.z* getItem(2,2);
        } else if(x == 4){
            result.x = point.x * getItem(0,0) + point.y* getItem(1,0) + point.z* getItem(2,0) + getItem(3,0);
            result.y = point.x * getItem(0,1) + point.y* getItem(1,1) + point.z* getItem(2,1) + getItem(3,1);
            result.z = point.x * getItem(0,2) + point.y* getItem(1,2) + point.z* getItem(2,2) + getItem(3,2);
        } else {throw new RuntimeException("wrong-dimensions");}
    }
    public void multiplyPoint2to(Point2D point, Point2D result) throws Exception {
        if(x==2){
            result.x = (int)(point.x * getItem(0,0) + point.y* getItem(0,1));
            result.y = (int)(point.x * getItem(1,0) + point.y* getItem(1,1));
        } else if(x == 3){
            result.x = (int)(point.x * getItem(0,0) + point.y* getItem(0,1) + getItem(0,2));
            result.y = (int)(point.x * getItem(1,0) + point.y* getItem(1,1) + getItem(1,2));
        } else {throw new Exception("wrong-dimensions");}
    }
}