-
Notifications
You must be signed in to change notification settings - Fork 1
/
Oscillator.java
82 lines (75 loc) · 2.12 KB
/
Oscillator.java
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
import java.util.*;
import java.awt.*;
public class Oscillator extends PhysicsElement implements Simulateable, SpringAttachable {
private static int id=0; // Oscillator's identification number
private double center; // oscillator's center
private double amplitude; // oscillation's amplitude
private double w; // oscillator's frequency [rad]
private double time;
private double pos_t;
private double pos_tPlusDelta;
private OscillatorView view; // Oscillator view of Model-View-Controller design pattern
private ArrayList<Elastic> springs;
private Oscillator(){ // nobody can create a block without state
this(1.0,0.3,0.5);
}
public Oscillator(double c, double a, double f){
super(id++);
pos_t = pos_tPlusDelta = center = c;
amplitude = a;
w = 2*Math.PI*f;
time=0;
view = new OscillatorView(this);
springs = new ArrayList<Elastic>();
}
public double getPosition() {
return pos_t;
}
public void computeNextState(double delta_t, MyWorld world) {
time+=delta_t;
pos_tPlusDelta = center + amplitude*Math.sin(w*time);
}
public void updateState(){
pos_t = pos_tPlusDelta;
}
public void updateView (Graphics2D g) {
view.updateView(g); // update this Oscillator's view in Model-View-Controller design pattern
}
public boolean contains(double x, double y) {
return view.contains(x,y);
}
public void setSelected(){
view.setSelected();
}
public void setReleased(){
view.setReleased();
}
public void dragTo(double x){ // pos_t = center +dx
center+= (x-pos_t); // x = new_center + dx
pos_t=x; // new_center= x - dx = x - (pos_t-center)
} // new_center=center +x-pos_t
public String getDescription() {
return "Oscillator_" + getId()+":x";
}
public String getState() {
return getPosition()+"";
}
public void attachSpring(Elastic s){
springs.add(s);
}
public void detachSpring(Elastic s){
springs.remove(s);
}
public double getMass() {
return 0.0;
}
public boolean collide(SpringAttachable b) {
return false;
}
public double getRadius() {
return 0.1;
}
public double getSpeed() {
return 0.0;
}
}