/** Los principales problemas de perdida de precision se producen cuando el vector es creado en polares, ya que su radio esta definido como float mientras que todas las operaciones matematicas tienden a ser en Double. */ //************************************************************// // para el Metodo "multiplicacionPorEscarlar(float m)" // //************************************************************// import java.lang.*; public class Vector { private double X; private double Y; private float R; private double A; private boolean polares; Vector(double px, double py) { polares = false; X = px; Y = py; R = (float)Math.sqrt(Math.pow(X,2)+Math.pow(Y,2)); A = Math.tan(Y/X); } Vector(float rd, double ang) { polares = true; R = rd; A = ang; X = rd * Math.cos(ang); Y = rd * Math.sin(ang); } Vector() { polares = false; X = 0; Y = 0; R = 0; A = 0; } public void sumarle(Vector v) { X = this.X + v.X; Y = this.Y + v.Y; //aqui se pierde mucha precision R = (float)Math.sqrt(Math.pow(X,2)+Math.pow(Y,2)); A = (this.A + v.A)/2; } public void multiplicacionPorEscarlar(float m) { //esta operacion funciona muy bien. X = (double)this.X*m; Y = (double)this.Y*m; R = this.R * m; } public void rotar(float alfa) { A = this.A + alfa; //aca tb se pierde precision. X = R * Math.cos(A); Y = R * Math.sin(A); } public String toString() { String s; if(polares == true) s = new String("(" + this.R + ", " + this.A + ")"); else s = new String("(" + this.X + ", " + this.Y + ")"); return s; } public boolean equals(Vector v) { if(polares) { if(this.R == v.R && this.A == v.A) return true; } else if(this.X == v.X && this.Y == v.Y) return true; return false; } public static Vector suma(Vector a, Vector b) { Vector v = new Vector(a.X + b.X, a.Y + b.Y); return v; } public static Vector resta(Vector a, Vector b) { Vector v = new Vector(a.X - b.X, a.Y - b.Y); return v; } public static float productoPunto(Vector a , Vector b) { return (float)((a.X * b.X) + (a.Y * b.Y)); } }