-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnit.java
More file actions
78 lines (60 loc) · 3.08 KB
/
Unit.java
File metadata and controls
78 lines (60 loc) · 3.08 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
/**
* Created by niklas on 26.02.17.
*/
public class Unit {
private int length_dimension;
private int time_dimension;
private int mass_dimension;
private double length_factor_si;
private double time_factor_si;
private double mass_factor_si;
public Unit(int length_dimension, int time_dimension, int mass_dimension, double length_factor_si, double time_factor_si, double mass_factor_si) {
this.length_dimension = length_dimension;
this.time_dimension = time_dimension;
this.mass_dimension = mass_dimension;
this.length_factor_si = length_factor_si;
this.time_factor_si = time_factor_si;
this.mass_factor_si = mass_factor_si;
}
public Unit(int length_dimension, int time_dimension, int mass_dimension) {
this(length_dimension, time_dimension, mass_dimension, 1.0, 1.0, 1.0);
}
public Unit getOtherDimension(int length_dimension, int time_dimension, int mass_dimension) {
return new Unit(length_dimension, time_dimension, mass_dimension, this.length_factor_si, this.time_factor_si, this.mass_factor_si);
}
public boolean equal_dimension(Unit a) {
if(length_dimension != a.getLength_dimension())
return false;
if(time_dimension != a.getTime_dimension())
return false;
if(mass_dimension != a.getMass_dimension())
return false;
return true;
}
public static double convert(double a, Unit from, Unit to) {
if(!from.equal_dimension(to)) throw new RuntimeException("Unit dimensions does not match");
a *= Math.pow(from.getLength_factor_si(),from.getLength_dimension());
a *= Math.pow(from.getTime_factor_si(),from.getTime_dimension());
a *= Math.pow(from.getMass_factor_si(),from.getMass_dimension());
a /= Math.pow(to.getLength_factor_si(),to.getLength_dimension());
a /= Math.pow(to.getTime_factor_si(),to.getTime_dimension());
a /= Math.pow(to.getMass_factor_si(),to.getMass_dimension());
return a;
}
public static MathVector convert(MathVector a, Unit from, Unit to) {
if(!from.equal_dimension(to)) throw new RuntimeException("Unit dimensions does not match");
a = a.multiply(Math.pow(from.getLength_factor_si(),from.getLength_dimension()));
a = a.multiply(Math.pow(from.getTime_factor_si(),from.getTime_dimension()));
a = a.multiply(Math.pow(from.getMass_factor_si(),from.getMass_dimension()));
a = a.divide(Math.pow(to.getLength_factor_si(),to.getLength_dimension()));
a = a.divide(Math.pow(to.getTime_factor_si(),to.getTime_dimension()));
a = a.divide(Math.pow(to.getMass_factor_si(),to.getMass_dimension()));
return a;
}
public int getLength_dimension() {return length_dimension;}
public int getTime_dimension() {return time_dimension;}
public int getMass_dimension() {return mass_dimension;}
public double getLength_factor_si() {return length_factor_si;}
public double getTime_factor_si() {return time_factor_si;}
public double getMass_factor_si() {return mass_factor_si;}
}