Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.skysoldier.graphics3D;
- import java.io.PrintStream;
- public class Matrix {
- private int rows;
- private int cols;
- private double matrixData[][];
- private Values3D matrixType;
- public Matrix(){
- this(new double[1][1]);
- }
- public Matrix(double matrixData[][]){
- this(matrixData.length, matrixData[0].length, Values3D.MATRIX_NORMAL);
- this.matrixData = matrixData;
- }
- public Matrix(int rows, int cols, Values3D matrixType){
- this(rows, cols);
- this.matrixData = new double[rows][cols];
- this.matrixType = matrixType;
- }
- public Matrix(int rows, int cols){
- this.rows = rows;
- this.cols = cols;
- this.matrixData = new double[rows][cols];
- this.matrixType = Values3D.MATRIX_NORMAL;
- this.fill(0);
- }
- public Matrix multiply(Matrix multiplier){
- if(getCols() != multiplier.getRows()){
- throw new ArithmeticException("matrices not multipliable! (multiplicand cols (" + getCols() + ") != multiplier rows (" + multiplier.getRows() + ") )");
- }
- else{
- Matrix product = new Matrix(getRows(), multiplier.getCols());
- for(int i = 0; i < getRows(); i++){
- for(int j = 0; j < multiplier.getCols(); j++){
- double n = 0;
- for(int k = 0; k < getCols(); k++){ // or multiplier.getRows(); , no difference
- n += getCellData(i, k) * multiplier.getCellData(k, j);
- }
- product.setCellData(i, j, n);
- }
- }
- return product;
- }
- }
- public void print(PrintStream printer){
- if(printer != null){
- printer.println(toString());
- }
- else{
- System.out.println(toString());
- }
- System.out.println("");
- }
- public String toString(){
- String dataString = "";
- for(int i = 0; i < getRows(); i++){
- for(int j = 0; j < getCols(); j++){
- dataString += matrixData[i][j] + " ";
- }
- dataString += "\n";
- }
- return dataString;
- }
- public void fill(int n){
- for(int i = 0; i < matrixData.length; i++){
- for(int j = 0; j < matrixData[0].length; j++){
- matrixData[i][j] = n;
- }
- }
- }
- public Point3D toPoint3D(){
- if(!(getRows() == 4 && getCols() == 1)){
- throw new UnsupportedOperationException("Point3D Matrix should have size 4x1");
- }
- else{
- return new Point3D(getCellData(0, 0), getCellData(1, 0), getCellData(2, 0));
- }
- }
- public Matrix clone(){
- return new Matrix(getMatrixData());
- }
- public int getRows(){
- return rows;
- }
- public int getCols(){
- return cols;
- }
- public double[][] getMatrixData(){
- return matrixData;
- }
- public double getCellData(int row, int col){
- return matrixData[row][col];
- }
- public Values3D getMatrixType(){
- return matrixType;
- }
- public void setCellData(int row, int col, double n){
- matrixData[row][col] = n;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement