-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicVectorProp.java
More file actions
83 lines (74 loc) · 1.96 KB
/
BasicVectorProp.java
File metadata and controls
83 lines (74 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* In this class I will be automating the following tasks that might prove useful later on
* 1. Finding the magnitude of a vector
* 2. find the angle between them
* 3. finding the projection of a vector on another vector
* 4. finding the cross product
* @author akopti
*
*/
public class BasicVectorProp
{
//data
public static double U[]= new double[3];
public static double V[]= new double[3];
//methods
//The magnitude of the method
public static double mag(double[] vector)
{
return(Math.sqrt((Math.pow(vector[0],2) + Math.pow(vector[1], 2) + Math.pow(vector[2], 2))));
}
//the dot product of two vectors
public static double dotProduct(double[] a, double[] b)
{
return((a[0]*b[0])+(a[1]*b[1])+(a[2]*b[2]));
}
//finding the angle between two vectors
public static double angleBet(double [] a, double [] b)
{
return(Math.acos((dotProduct(a,b))/(mag(a)*mag(b))));
}
//the projection of a vector on another vector
public static double[] projection(double[] a, double[] b)
{
double cons = (dotProduct(a,b)/Math.pow(mag(b),2));
for(int i =0; i<a.length;i++)
{
b[i] *= cons;
}
return b;
}
//the crossProduct of two vectors
public static double[] crossProduct( double[] a, double[] b)
{
double[] temp = new double[3];
temp[0]= (a[1]*b[2]) - (a[2]*b[1]);
temp[1]= (a[2]*b[0]) - (a[0]*b[2]);
temp[2]= (a[0]*b[1]) - (a[1]*b[0]);
return(temp);
}
public static void printArr(double [] a)
{
for(int i=0; i<a.length;i++)
{
if(i!=2)
{
System.out.print(a[i] + ", ");
}
else
{
System.out.print(a[i] + ".");
}
}
System.out.println();
System.out.println("---------");
}
//to test these I will find the cross product of two vectors
//and will see if the resuls match wolfram alpha's
public static void main(String [] args)
{
double [] U = {1,2,4};
double [] V = {1,2,3};
printArr(crossProduct(U,V));
}
}