domingo, 15 de noviembre de 2009

Ejercicios con Vectores en 3D

CLASE MATRIZ

public class Matriz {
public int numeroFilas;
public int numeroColumnas;
public double [][]matriz;

//crea una matriz sin dimensiones
public Matriz(){
}

//constructor con parametros
//nF es el numero de filas
//nc es el numero de columnas
public Matriz(int nF, int nC){
numeroFilas = nF;
numeroColumnas = nC;
matriz = new double [numeroFilas][numeroColumnas];

for(int i=0; i < numeroFilas; i++)
for(int j=0; j < numeroColumnas; j++)
matriz[i][j]=0;
}


//metodo de suma de matrices
//b es el primer sumando
//retorna una matriz resultado de suma
public Matriz suma(Matriz b){
Matriz resultado;
//primero revisamos que las filas y las columnas sean iguales
//this referencia a un objeto que au no esta creado pero que alguien algun momento lo va a crear
if((this.numeroFilas == b.numeroFilas)&& (this.numeroColumnas == b.numeroColumnas)){
resultado = new Matriz(this.numeroFilas, this.numeroColumnas);
for(int i=0; i < this.numeroFilas; i++)
for(int j=0; j < this.numeroColumnas; j++)
resultado.matriz[i][j] = this.matriz[i][j]+ b.matriz[i][j];
return resultado;
}
else
System.out.println("ERROR EN DIMENSIONES DE LAS MATRICES");
resultado=null;
return resultado;
}

//metodo de multiplicacion de matrices
//retorna una matriz resultado resta
public Matriz multiplicacion(Matriz b){
Matriz resultado;
if(this.numeroFilas == b.numeroColumnas){
resultado=new Matriz(this.numeroFilas,b.numeroColumnas);
for(int i=0; i < this.numeroFilas; i++){
for(int j=0; j < b.numeroColumnas; j++){
for(int k=0; k < this.numeroFilas; k++)
resultado.matriz[i][j] += (this.matriz[i][k]*b.matriz[k][j]);
}
}
return resultado;
}
else
System.out.println("error en dimensiones de las matrices");
resultado = null;
return resultado;
}
//devuelve el objeto matriz en texto para poderlo imprimir
public String toString(){
String aux="\n[\n";
for(int i=0; i < numeroFilas; i++){
for(int j=0; j < numeroColumnas; j++){
aux += matriz[i][j]+" ";
}
aux+="\n";
}
aux+= "]";
return aux;
}
}

.........................................................................
.........................................................................
.........................................................................

Clase Vector

public class Vector3D extends Matriz{
double coorX;
double coorY;
double coorZ;

public Vector3D (){
super(1,3);
}
public Vector3D(double x, double y, double z){
super(1,3);
this.matriz[0][0] = x;
this.matriz[0][1] = y;
this.matriz[0][2] = x;
coorX=x;
coorY=y;
coorZ=z;

}

public double magnitud(){
double resultado = 0;
//double suma;
for(int i = 0; i < 3;i++){
resultado+=this.matriz[0][i]*this.matriz[0][i];
}
resultado = Math.sqrt(resultado);
return resultado;
}

public double magnitud1(){
double resultado;
resultado = this.coorX*this.coorX+this.coorY*this.coorY+this.coorZ*this.coorZ;
resultado= Math.sqrt(resultado);
return resultado;
}

public Vector3D unitario(){
Vector3D unitario = new Vector3D();
//unitario = (Vector3D)this.multiplicacionEscalar(1/(this.magnitud()));
for (int i = 0 ; i < 3; i++){
unitario.matriz[0][i] = this.matriz[0][i]/this.magnitud();
}
return unitario;
}

public double productoEscalar(Vector3D v){
double resultado = 0;
for(int i = 0; i < 3;i++){
resultado += this.matriz[0][i]*v.matriz[0][i];
}
return resultado;
}
public Vector3D productoCruz(Vector3D v){
Vector3D resultado;

resultado = new Vector3D ();
resultado.matriz[0][0]=(this.matriz[0][1]*v.matriz[0][2])-(this.matriz[0][2]*v.matriz[0][1]);
resultado.matriz[0][1]=(this.matriz[0][2]*v.matriz[0][0])-(this.matriz[0][0]*v.matriz[0][2]);
resultado.matriz[0][2]=(this.matriz[0][0]*v.matriz[0][1])-(this.matriz[0][1]*v.matriz[0][0]);

return resultado;
}



public static void main(String args[]){
Vector3D v1 = new Vector3D(-1,1,-1);
Vector3D v2 = new Vector3D(0,2,0);
System.out.println(v1);
System.out.println(v1+"\n"+v2+"\n suma "+(v1.suma(v2)));
System.out.println("Coordenada en x "+v1.coorX);
System.out.println(v1.magnitud()+"\n"+v1.magnitud1());
System.out.println(v1.unitario());
System.out.println(v1.productoEscalar(v2));
System.out.println(v1.productoCruz(v2));
}

}

6 comentarios:

  1. hola dieguito te falta la clase matriz sino la clase vector no podria ser heredada aqui te dejo otra forma de hacer la magnitud del vector:
    //tercera forma de hacer la magnitud
    public double magnitud2(){
    double resultado;
    resultado = Math.pow(this.coordenadaX, 2)+Math.pow(this.coordenadaY, 2)+Math.pow(this.coordenadaZ, 2);
    resultado = Math.sqrt(resultado);
    return resultado;
    }

    ResponderEliminar
  2. Hola que tal, el programa esta bien pero no funciona ya que necesitas la clase matriz y no la tienes solo tienes la clase Vector3D extends Matriz. Si te sirve esta es mi clase Matriz, podrias utilizarla para tu programa.

    CLASE MATRIZ

    public class Matriz {
    public int numeroFilas;
    public int numeroColumnas;
    public double [][]matriz;

    //crea una matriz sin dimensiones
    public Matriz(){
    }

    //constructor con parametros
    //nF es el numero de filas
    //nc es el numero de columnas
    public Matriz(int nF, int nC){
    numeroFilas = nF;
    numeroColumnas = nC;
    matriz = new double [numeroFilas][numeroColumnas];

    for(int i=0; i < numeroFilas; i++)
    for(int j=0; j < numeroColumnas; j++)
    matriz[i][j]=0;
    }


    //metodo de suma de matrices
    //b es el primer sumando
    //retorna una matriz resultado de suma
    public Matriz suma(Matriz b){
    Matriz resultado;
    //primero revisamos que las filas y las columnas sean iguales
    //this referencia a un objeto que au no esta creado pero que alguien algun momento lo va a crear
    if((this.numeroFilas == b.numeroFilas)&& (this.numeroColumnas == b.numeroColumnas)){
    resultado = new Matriz(this.numeroFilas, this.numeroColumnas);
    for(int i=0; i < this.numeroFilas; i++)
    for(int j=0; j < this.numeroColumnas; j++)
    resultado.matriz[i][j] = this.matriz[i][j]+ b.matriz[i][j];
    return resultado;
    }
    else
    System.out.println("ERROR EN DIMENSIONES DE LAS MATRICES");
    resultado=null;
    return resultado;
    }

    //metodo de multiplicacion de matrices
    //retorna una matriz resultado resta
    public Matriz multiplicacion(Matriz b){
    Matriz resultado;
    if(this.numeroFilas == b.numeroColumnas){
    resultado=new Matriz(this.numeroFilas,b.numeroColumnas);
    for(int i=0; i < this.numeroFilas; i++){
    for(int j=0; j < b.numeroColumnas; j++){
    for(int k=0; k < this.numeroFilas; k++)
    resultado.matriz[i][j] += (this.matriz[i][k]*b.matriz[k][j]);
    }
    }
    return resultado;
    }
    else
    System.out.println("error en dimensiones de las matrices");
    resultado = null;
    return resultado;
    }
    //devuelve el objeto matriz en texto para poderlo imprimir
    public String toString(){
    String aux="\n[\n";
    for(int i=0; i < numeroFilas; i++){
    for(int j=0; j < numeroColumnas; j++){
    aux += matriz[i][j]+" ";
    }
    aux+="\n";
    }
    aux+= "]";
    return aux;
    }
    }

    ResponderEliminar
  3. oye debes poner la clase matriz porque estas heredando la clase matriz y no sabemos como esta conformada.

    ResponderEliminar
  4. pondras titulo en la publicacion para saber de q se trata para poder corregir

    ResponderEliminar
  5. bueno te dije q te faltaba la clase matriz pero no te puse la clase entonces ahi te la pongo para q le anexes ya que no se entiende de donde heredas para hacer este programa...


    jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    String aux = jTextField1.getText();
    String aux1="";
    int nF = Integer.parseInt(aux);
    aux = jTextField2.getText();
    int nC = Integer.parseInt(aux);
    m1 = new Matriz(nF,nC);
    m1.leer();
    aux1 += m1.toString();
    aux = jTextField3.getText();
    nF = Integer.parseInt(aux);
    aux = jTextField4.getText();
    nC = Integer.parseInt(aux);
    m2 = new Matriz(nF,nC);
    m2.leer();
    aux1 += m2.toString();
    jTextArea1.setText("Suma de Matrices: \n"+aux1+(m1.suma(m2)).toString());
    }


    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    String aux = jTextField1.getText();
    String aux1="";
    int nF = Integer.parseInt(aux);
    aux = jTextField2.getText();
    int nC = Integer.parseInt(aux);
    m1 = new Matriz(nF,nC);
    m1.leer();
    aux1 += m1.toString();
    aux = jTextField3.getText();
    nF = Integer.parseInt(aux);
    aux = jTextField4.getText();
    nC = Integer.parseInt(aux);
    m2 = new Matriz(nF,nC);
    m2.leer();
    aux1 += m2.toString();
    jTextArea1.setText("Resta de Matrices: \n"+aux1+(m1.resta(m2)).toString());
    }


    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
    String aux = jTextField1.getText();
    String aux1="";
    int nF = Integer.parseInt(aux);
    aux = jTextField2.getText();
    int nC = Integer.parseInt(aux);
    m1 = new Matriz(nF,nC);
    m1.leer();
    aux1 += m1.toString();
    aux = jTextField3.getText();
    nF = Integer.parseInt(aux);
    aux = jTextField4.getText();
    nC = Integer.parseInt(aux);
    m2 = new Matriz(nF,nC);
    m2.leer();
    aux1 += m2.toString();
    jTextArea1.setText("Multiplicacion de Matrices: \n"+aux1+(m1.Multiplicacion(m2)).toString());
    }


    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
    String aux = jTextField1.getText();
    String aux1="";
    int nF = Integer.parseInt(aux);
    aux = jTextField2.getText();
    int nC = Integer.parseInt(aux);
    m1 = new Matriz(nF,nC);
    m1.leer();
    aux1+= m1.toString();
    jTextArea1.setText("Transpuesta de Matrices: \n"+aux1+(m1.Transpuesta()).toString());






    // TODO add your handling code here:
    }


    public Matriz m1;
    public Matriz m2;
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;

    ResponderEliminar
  6. Gracias a todos y si tenian razon sin la clase matriz no podia heredar y ya le puse gracias .....

    ResponderEliminar