checkpoint
[anneal.git] / src / edu / berkeley / qfat / geom / Vec.java
1 package edu.berkeley.qfat.geom;
2
3 /** vector in 3-space; immutable */
4 public final class Vec {
5     public final float x, y, z;
6     public Vec(double x, double y, double z) { this((float)x, (float)y, (float)z); }
7     public Vec(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
8     public Vec(Point p1, Point p2) { this(p2.x-p1.x, p2.y-p1.y, p2.z-p1.z); }
9     public Vec cross(Vec v) { return new Vec(y*v.z-z*v.y, z*v.x-x*v.z, x*v.y-y*v.x); }
10     public Vec plus(Vec v) { return new Vec(x+v.x, y+v.y, z+v.z); }
11     public Vec norm() { return mag()==0 ? this : div(mag()); }
12     public Vec times(Matrix m) { return m.apply(this); }
13     public float mag() { return (float)Math.sqrt(x*x+y*y+z*z); }
14     public float dot(Vec v) { return x*v.x + y*v.y + z*v.z; }
15     public Vec times(float mag) { return new Vec(x*mag, y*mag, z*mag); }
16     public Vec div(float mag) { return new Vec(x/mag, y/mag, z/mag); }
17     public String toString() { return "<"+x+","+y+","+z+">"; }
18 }