用Java实现一个向量场(三)

2014-11-24 08:12:24 · 作者: · 浏览: 1
Vector.build(1, -1);

public static Vector build(double x, double y) {
return new Vector(x, y);
}

public Vector clone() {
return Vector.build(this.x, this.y);
}

public double slop() {
if(this.x == 0) {
return Vector.infinite;
}
return this.y / this.x;
}

public Vector(double _x, double _y) {
this.x = _x;
this.y = _y;
}

public Vector intVal() {
return new Vector((int)this.x, (int)this.y);
}

public Vector abs() {
return Vector.build((x>=0 x:-x), (y>=0 y:-y));
}

public Vector add(Vector v) {
return new Vector(this.x+v.x, this.y+v.y);
}

public Vector neg() {
return new Vector(-this.x, -this.y);
}

public Vector mul(double m) {
return new Vector(m*this.x, m*this.y);
}

public Vector dotMul(Vector m) {
return new Vector(this.x * m.x, this.y * m.y);
}
public Vector dotMul(double m) {
return Vector.build(this.x*m, this.y*m);
}

public Vector dotDiv(Vector m) {
return new Vector(this.x/m.x, this.y/m.y);
}

public Vector dotDiv(double d) {
return Vector.build(this.x/d, this.y/d);
}

public String toString() {
return String.format("<%.2f, %.2f>", x, y);
}

}


配置代码,可以查看不同的效果:

[java]
double scale = 80;
double aa = 3;
double dd = 0.15;

Area area = new Area(Vector.build(-aa, aa), Vector.build(aa, -aa));
Vector grid = Vector.build(dd, dd);
CanCalc c = new CanCalc(){
public double doCalc(Vector v) {
return v.x*v.x - v.y - 2;
}

};

摘自 stalendp的专栏