Co-authored-by: sigonasr2 <sigonasr2@gmail.com>origin
parent
dfe6210836
commit
7b0467cc5d
@ -1,17 +1,51 @@ |
||||
package sig; |
||||
|
||||
public class Vector { |
||||
public float x,y,z; |
||||
public float x,y,z,w; |
||||
Vector() { |
||||
this(0,0,0); |
||||
this(0,0,0,1); |
||||
} |
||||
public Vector(float x,float y,float z) { |
||||
this(x,y,z,1); |
||||
} |
||||
public Vector(float x,float y,float z,float w) { |
||||
this.x=x; |
||||
this.y=y; |
||||
this.z=z; |
||||
this.w=w; |
||||
} |
||||
@Override |
||||
protected Object clone(){ |
||||
return new Vector(x,y,z); |
||||
} |
||||
|
||||
public static Vector add(Vector v1,Vector v2) { |
||||
return new Vector(v1.x+v2.x,v1.y+v2.y,v1.z+v2.z); |
||||
} |
||||
public static Vector subtract(Vector v1,Vector v2) { |
||||
return new Vector(v1.x-v2.x,v1.y-v2.y,v1.z-v2.z); |
||||
} |
||||
public static Vector multiply(Vector v1,float k) { |
||||
return new Vector(v1.x*k,v1.y*k,v1.z*k); |
||||
} |
||||
public static Vector divide(Vector v1,float k) { |
||||
return new Vector(v1.x/k,v1.y/k,v1.z/k); |
||||
} |
||||
public static float dotProduct(Vector v1,Vector v2) { |
||||
return v1.x*v2.x+v1.y*v2.y+v1.z*v2.z; |
||||
} |
||||
public static float length(Vector v) { |
||||
return (float)Math.sqrt(dotProduct(v,v)); |
||||
} |
||||
public static Vector normalize(Vector v) { |
||||
float l = length(v); |
||||
return new Vector(v.x/l,v.y/l,v.z/l); |
||||
} |
||||
public static Vector crossProduct(Vector v1,Vector v2) { |
||||
Vector v = new Vector(); |
||||
v.x=v1.y*v2.z-v1.z*v2.y; |
||||
v.y=v1.z*v2.x-v1.x*v2.z; |
||||
v.z=v1.x*v2.y-v1.y*v2.x; |
||||
return v; |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue