class MyVector {
private double x;
private double y;
public MyVector(double x, double y) {
this.x = x;
this.y = y;
}
public double vLength() {
double tempLen = Math.sqrt(x*x + y*y);
double newLen = new BigDecimal(tempLen).setScale(3, RoundingMode.UP).doubleValue();
return newLen;
}
public static MyVector sum(MyVector a, MyVector b) {
return new MyVector(a.x + b.x, a.y + b.y);
}
public static MyVector sub(MyVector a, MyVector b) {
return new MyVector(a.x - b.x, a.y - b.y);
}
public static MyVector mul(MyVector a, double c) {
return new MyVector(a.x * c, a.y * c);
}
public static double scalar(MyVector a, MyVector b) {
double tempScalar = a.x * b.x + a.y * b.y;
double newScalar = new BigDecimal(tempScalar).setScale(3, RoundingMode.UP).doubleValue();
return newScalar;
}
public static double cos(MyVector a, MyVector b) {
double tempCos = scalar(a, b) / (a.vLength() * b.vLength());
double newCos = new BigDecimal(tempCos).setScale(3, RoundingMode.UP).doubleValue();
return newCos;
}
public static double projection(MyVector a, MyVector b) {
double tempProj = scalar(a, b) / b.vLength();
double newProj = new BigDecimal(tempProj).setScale(5, RoundingMode.UP).doubleValue();
return newProj;
}
public static boolean isCollinear(MyVector a, MyVector b) throws ArithmeticException {
double eps = 0.0001;
if (b.x == 0 || b.y == 0) {
throw new ArithmeticException("Zero divider");
}
return (Math.abs(a.x / b.x - a.y / b.y) < eps);
}
public static double vecProduct(MyVector a, MyVector b) {
double tempProduct = a.vLength() * b.vLength() * Math.sqrt(1 - cos(a,b) * cos(a,b));
double newProduct = new BigDecimal(tempProduct).setScale(5, RoundingMode.UP).doubleValue();
return newProduct;
}
public String toString() {
return new String(x + " ; " + y);
}
}