Free Web Site - Free Web Space and Site Hosting - Web Hosting - Internet Store and Ecommerce Solution Provider - High Speed Internet
Search the Web

Vector espacial

Para muchos problemas de física, se utilizan vectores espaciales (tres dimensiones). Aquí lo que se hace es implementar un objeto con las operaciones básicas que se realizan sobre un vector, como el cálculo de:

  • Norma de un vector
  • Producto escalar de dos vectores
  • Multiplicar vector por un factor constante
  • Sumar dos vectores
  • Calcular vector unitario
  • class VectorEsp
    {
        public final static int DIMENSIONES = 3;
    
        public final static int X = 0;
        public final static int Y = 1;
        public final static int Z = 2;
    
    
        public double v[] = new double[DIMENSIONES];
    
        public VectorEsp(double x,double y,double z)
        {
            v[X] = x;
            v[Y] = y;
            v[Z] = z;
        }
    
        public VectorEsp(double vector_aux[])
        {
            for(int i = 0; i < DIMENSIONES; i++)
                v[i] = vector_aux[i];    
        }    
    
        public VectorEsp()
        {
            this(0,0,0);    
        }
    
    
        public static VectorEsp suma(VectorEsp a,VectorEsp b)
        {
            VectorEsp temp = new VectorEsp();
            for(int i = 0; i < DIMENSIONES ; i++)
                temp.v[i] = a.v[i] + b.v[i];
            return temp;    
        }
    
        public static double prod_escalar(VectorEsp a,VectorEsp b)
        {
            double s = 0.0;
            for(int i = 0; i < DIMENSIONES ; i++)
                s += a.v[i] * b.v[i];
            
            return s; 
        }
    
        private int coord_mayor()
        {
            int pos = X;
            double mayor = v[X];
            for(int i = 1; i < DIMENSIONES; i++)
                if (v[i] > mayor) 
                {
                    mayor = v[i];
                    pos = i;
                }
            return pos;
        }
    
        public double norma()
        {
            double s = 1.0;
            int mayor = coord_mayor();
        
            switch(mayor)
            {
                case X : 
                    s += Math.pow(v[Y] / v[X],2);
                    s += Math.pow(v[Z] / v[X],2);
                    break;
                case Y :
                    s += Math.pow(v[X] / v[Y],2);
                    s += Math.pow(v[Z] / v[Y],2);
                    break;
                case Z :    
                    s += Math.pow(v[X] / v[Z],2);
                    s += Math.pow(v[Y] / v[Z],2);
                    break;
            }
        
            return Math.sqrt(s) * Math.abs(v[mayor]);    
        }
    
        public VectorEsp vector_unitario()
        {
            return vector_unitario(this);
        }
    
        public void multiplicar(double factor)
        {
            for(int i = 0; i < DIMENSIONES ; i++)
                v[i] *= factor;    
        }
    
        public static VectorEsp vector_unitario(VectorEsp vect)
        {
            double aux_norma = vect.norma();
        
            VectorEsp temp = new VectorEsp();
        
            for(int i = 0; i < DIMENSIONES ; i++)    
            {
                temp.v[i] = vect.v[i] / aux_norma;    
            }
    
            return temp;
        }
    
        public String toString()
        {
            String s = "( ";
            for(int i = 0; i < DIMENSIONES; i++)
            {    
                s += Double.toString(v[i]);
            
                if (i == DIMENSIONES - 1)
                    s += " )";
                else 
                    s += " , "; 
            }
        
            return s; 
        }
    
        public static void main(String arg[])
        {
            System.out.println("\n\t\t\tPROGRAMA DE PRUEBA");
            
            VectorEsp a = new VectorEsp(34.80,53.30,9.210);
            VectorEsp b = new VectorEsp(-21.30,16.20,-4.90);
            
            System.out.println("\n\tvector\t\tcoordenadas\t\t\tnorma");
            System.out.println("\t------\t\t-----------\t\t\t-----");
            
            System.out.print("\ta\t\t" + a.toString());
            System.out.println("\t\t" + a.norma());
            
            System.out.print("\tb\t\t" + b.toString());
            System.out.println("\t\t" + b.norma());
            
            VectorEsp u = a.vector_unitario();
            System.out.println("\n\tvector unitario de a");
            System.out.println("\t--------------------");
            
            System.out.println("\n\t" + u.toString());
    
            System.out.println("\n\tmultiplicacion del vector anterior por su norma");
            System.out.println("\t-----------------------------------------------");
    
            u.multiplicar(a.norma());
            System.out.println("\n\t" + u.toString());
            
            System.out.println();
        }
    
    }
    
    
    Bajar archivo

    Para verificar resultados, se implementó un método main , que consiste en crear 2 vectores , mostrar sus coordenadas en pantalla y luego aplicar operaciones sobre el vector a.

    Salida a pantalla