-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollision.java
More file actions
133 lines (122 loc) · 4.5 KB
/
Collision.java
File metadata and controls
133 lines (122 loc) · 4.5 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import java.util.Scanner;
import java.util.Arrays;
public class Collision{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("How many objects are colliding?");
while (!input.hasNextInt()){
System.out.println("This is not a valid (integer) number of objects.");
input.nextLine();
}
int numObj = input.nextInt(); // gives number of rows of matrix
input.nextLine();
double[][] matrix = new double[2][numObj + 1]; // deal with massless particles later
for (int i = 0; i < numObj; ++i){
System.out.println();
System.out.println("Enter the mass of Object " + (i+1) + ":");
while (!input.hasNextDouble()){
System.out.println("This is not a valid (double) mass. Try again.");
input.nextLine();
}
matrix[0][i] += input.nextDouble();
matrix[1][i] += matrix[0][i];
input.nextLine();
}
for (int i = 0; i < numObj; ++i){
System.out.println();
System.out.println("Enter the initial velocity of Object " + (i+1) + ":");
while (!input.hasNextDouble()){
System.out.println("This is not a valid (double) velocity. Try again.");
input.nextLine();
}
double vel = input.nextDouble();
System.out.println("Enter the initial angle of Object " + (i+1) + " in degrees:");
while(!input.hasNextDouble()){
System.out.println("This is not a valid (double) angle. Try again.");
input.nextLine();
}
double angle = input.nextDouble();
matrix[0][numObj] += matrix[0][i]*vel*Math.cos(Math.toRadians(angle));
matrix[1][numObj] += matrix[0][i]*vel*Math.sin(Math.toRadians(angle));
input.nextLine();
}
for(int i = 0; i < numObj; ++i){ // account for unknown angles
System.out.println();
System.out.println("Enter the final angle of Object " + (i+1) + " in degrees:");
while (!input.hasNextDouble()){
System.out.println("This is not a valid (double) angle. Try again.");
input.nextLine();
}
double angle = input.nextDouble();
matrix[0][i] *= Math.cos(Math.toRadians(angle));
matrix[1][i] *= Math.sin(Math.toRadians(angle));
input.nextLine();
}
int numVarReq = numObj - 2; // assumes all angles are known
for (int i = 0; i < numVarReq; ++i){
System.out.println();
System.out.println("Which object's velocity do you know? (" + numVarReq + "needed):");
while (!input.hasNextInt()){
System.out.println("This is not a valid (int) object. Try again");
input.nextLine();
}
int index = input.nextInt(); // check that this is valid
input.nextLine();
System.out.println("Enter the velocity of Object " + index + ":");
while (!input.hasNextDouble()){
System.out.println("This is not a valid (double) velocity. Try again.");
input.nextLine();
}
double vel = input.nextDouble();
input.nextLine();
matrix[0][index - 1] *= vel;
matrix[1][index - 1] *= vel;
matrix[0][numObj] -= matrix[0][index - 1];
matrix[1][numObj] -= matrix[1][index - 1];
matrix[0][index - 1] = 0;
matrix[1][index - 1] = 0;
}
System.out.println(Arrays.deepToString(matrix));
toRREF(matrix);
System.out.println(Arrays.deepToString(matrix));
}
static void toRREF(double[][] M) {
int rowCount = M.length;
if (rowCount == 0)
return;
int columnCount = M[0].length;
int lead = 0;
for (int r = 0; r < rowCount; r++) {
if (lead >= columnCount)
break;
{
int i = r;
while (Math.round(M[i][lead]) == 0 && Math.abs(M[i][lead] - Math.round(M[i][lead])) < 0.0001) { // account for double precision -- originally the argument was M[i][lead] == 0
i++;
if (i == rowCount) {
i = r;
lead++;
if (lead == columnCount)
return;
}
}
double[] temp = M[r];
M[r] = M[i];
M[i] = temp;
}
{
double lv = M[r][lead];
for (int j = 0; j < columnCount; j++)
M[r][j] /= lv;
}
for (int i = 0; i < rowCount; i++) {
if (i != r) {
double lv = M[i][lead];
for (int j = 0; j < columnCount; j++)
M[i][j] -= lv * M[r][j];
}
}
lead++;
}
}
}