1 // Code from Visualizing Data, First Edition, Copyright 2008 Ben Fry.
2 
3 
4 public class BoundsIntegrator {
5 
6   static final float ATTRACTION = 0.2f;
7   static final float DAMPING = 0.5f;
8 
9   float valueX, velocityX, accelerationX;
10   float valueY, velocityY, accelerationY;
11   float valueW, velocityW, accelerationW;
12   float valueH, velocityH, accelerationH;
13 
14   float damping;
15   float attraction;
16 
17   boolean targeting;
18   float targetX, targetY, targetW, targetH;
19 
20 
BoundsIntegrator()21   public BoundsIntegrator() {
22     this.valueX = 0;
23     this.valueY = 0;
24     this.valueW = 1;
25     this.valueH = 1;
26 
27     this.damping = DAMPING;
28     this.attraction = ATTRACTION;
29   }
30 
31 
BoundsIntegrator(float x, float y, float w, float h)32   public BoundsIntegrator(float x, float y, float w, float h) {
33     this.valueX = x;
34     this.valueY = y;
35     this.valueW = w;
36     this.valueH = h;
37 
38     this.damping = DAMPING;
39     this.attraction = ATTRACTION;
40   }
41 
42 
set(float x, float y, float w, float h)43   public void set(float x, float y, float w, float h) {
44     this.valueX = x;
45     this.valueY = y;
46     this.valueW = w;
47     this.valueH = h;
48   }
49 
50 
getX()51   public float getX() {
52     return valueX;
53   }
54 
55 
getY()56   public float getY() {
57     return valueY;
58   }
59 
60 
getW()61   public float getW() {
62     return valueW;
63   }
64 
65 
getH()66   public float getH() {
67     return valueH;
68   }
69 
70 
spanX(float pointX, float start, float span)71   public float spanX(float pointX, float start, float span) {
72     if (valueW != 0) {
73       //return (pointX - valueX) / valueW;
74       float n = (pointX - valueX) / valueW;
75       return start + n*span;
76     } else {
77       return Float.NaN;
78     }
79   }
80 
81 
spanY(float pointY, float start, float span)82   public float spanY(float pointY, float start, float span) {
83     if (valueH != 0) {
84       //return (pointY - valueY) / valueH;
85       float n = (pointY - valueY) / valueH;
86       return start + n*span;
87     } else {
88       return Float.NaN;
89     }
90   }
91 
92 
setAttraction(float a)93   public void setAttraction(float a) {
94     attraction = a;
95   }
96 
97 
setDamping(float d)98   public void setDamping(float d) {
99     damping = d;
100   }
101 
102 
update()103   public boolean update() {
104     if (targeting) {
105       accelerationX += attraction * (targetX - valueX);
106       velocityX = (velocityX + accelerationX) * damping;
107       valueX += velocityX;
108       accelerationX = 0;
109       boolean updated = (Math.abs(velocityX) > 0.0001f);
110 
111       accelerationY += attraction * (targetY - valueY);
112       velocityY = (velocityY + accelerationY) * damping;
113       valueY += velocityY;
114       accelerationY = 0;
115       updated |= (Math.abs(velocityY) > 0.0001f);
116 
117       accelerationW += attraction * (targetW - valueW);
118       velocityW = (velocityW + accelerationW) * damping;
119       valueW += velocityW;
120       accelerationW = 0;
121       updated |= (Math.abs(velocityW) > 0.0001f);
122 
123       accelerationH += attraction * (targetH - valueH);
124       velocityH = (velocityH + accelerationH) * damping;
125       valueH += velocityH;
126       accelerationH = 0;
127       updated |= (Math.abs(velocityH) > 0.0001f);
128     }
129     return false;
130   }
131 
132 
target(float tx, float ty, float tw, float th)133   public void target(float tx, float ty, float tw, float th) {
134     targeting = true;
135     targetX = tx;
136     targetY = ty;
137     targetW = tw;
138     targetH = th;
139   }
140 
141 
targetLocation(float tx, float ty)142   public void targetLocation(float tx, float ty) {
143     targeting = true;
144     targetX = tx;
145     targetY = ty;
146   }
147 
148 
targetSize(float tw, float th)149   public void targetSize(float tw, float th) {
150     targeting = true;
151     targetW = tw;
152     targetH = th;
153   }
154 
155 
targetX(float tx)156   public void targetX(float tx) {
157     targeting = true;
158     targetX = tx;
159   }
160 
161 
targetY(float ty)162   public void targetY(float ty) {
163     targeting = true;
164     targetY = ty;
165   }
166 
167 
targetW(float tw)168   public void targetW(float tw) {
169     targeting = true;
170     targetW = tw;
171   }
172 
173 
targetH(float th)174   public void targetH(float th) {
175     targeting = true;
176     targetH = th;
177   }
178 }
179