| CS585 Homework Assignment 1: Face\Vect.java |
1 /*
2 * Vect.java
3 *
4 * A simple Object Class to encapsulate Linear Algebra on Vector operations
5 * This class supports both 2D and 3D vectors and computes magnitude, dot product
6 * and the angle (represented as the COS(Theta))
7 *
8 * Created on September 21, 2005, 11:13 PM
9 */
10
11 public class Vect {
12
13 public int x, y, z;
14
15 /** CONSTRUCTORS */
16 public Vect(int xComp, int yComp) {
17 x = xComp;
18 y = yComp;
19 z = 0;
20 }
21
22 public Vect(int xComp, int yComp, int zComp) {
23 x = xComp;
24 y = yComp;
25 z = zComp;
26 }
27
28 //Returns the Magnitude of the vector
29 public double magnitude() {
30 return Math.sqrt(x*x + y*y + z*z);
31 }
32
33 //Static Method that returns the Dot Product of two Vectors
34 public static double dotProduct(Vect V1, Vect V2) {
35 return V1.x*V2.x + V1.y*V2.y + V1.z*V2.z;
36 }
37
38
39 //Static method to calculate the relationship between two vectors
40 // based on the equation:
41 /*
42 * V1 (dot-product) V2
43 * COS (THETA) = --------------------
44 * |V1| * |V2|
45 */
46 // NOTE: this method actually returns the COS(Theta)
47 public static double Theta(Vect V1, Vect V2) {
48 return (dotProduct(V1, V2) / (V1.magnitude()*V2.magnitude()));
49 }
50
51 }
52