Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Vec {
- public final static Vec X_AXIS=new Vec(1,0);
- public final static Vec Y_AXIS=new Vec(0,1);
- public final static Vec ORIGIN=new Vec(0,0);
- final double x,y;
- public Vec(double x, double y) {
- this.x=x;
- this.y=y;
- }
- public double dotProduct(Vec v) {
- return x*v.x+y*v.y;
- }
- public double crossProduct(Vec v) {
- double r=x*v.y-y*v.x;
- return r;
- }
- public double angle() {
- return Math.atan2(y,x);
- }
- public double angleTo(Vec v) {
- double angle=v.angle()-angle();
- while(angle <= -Math.PI) angle+=Math.PI*2;
- while(angle > Math.PI) angle-=Math.PI*2;
- return angle;
- }
- public double getLength() {
- return Math.sqrt(x*x+y*y);
- }
- public Vec translate(Vec v) {
- return new Vec(this.x-v.x, this.y-v.y);
- }
- public Vec rotate90() {
- return new Vec(-y, x);
- }
- public Vec rotate270() {
- return new Vec(y, -x);
- }
- public Vec rotate180() {
- return new Vec(-x, -y);
- }
- public Vec rotate(double theta) {
- double sin = Math.sin(theta);
- double cos = Math.cos(theta);
- return new Vec(x*cos-y*sin,
- x*sin+y*cos);
- }
- public Vec normalize() {
- double length=getLength();
- if(length==0) return X_AXIS;
- return new Vec(x/length, y/length);
- }
- public Vec add(Vec v) {
- return new Vec(x+v.x, y+v.y);
- }
- public Vec sub(Vec v) {
- return new Vec(x-v.x, y-v.y);
- }
- public Vec mul(double m) {
- return new Vec(x*m, y*m);
- }
- public Vec div(double d) {
- return new Vec(x/d, y/d);
- }
- public Vec neg() {
- return new Vec(-x, -y);
- }
- public String toString() {
- return String.format(java.util.Locale.ENGLISH,
- "(%.3f, %.3f)", x, y);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement