| | 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); |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | |