Revision 97dadfe5
Added by Leszek Koltunski over 7 years ago
src/main/java/org/distorted/examples/dynamic/DynamicSurfaceView.java | ||
---|---|---|
44 | 44 |
public static final int DIM_3DXY = 2; |
45 | 45 |
public static final int DIM_3DXZ = 3; |
46 | 46 |
|
47 |
private static final int NUM_INTERPOLATIONS= 100;
|
|
48 |
private static final int MAX_VECTORS = 6;
|
|
47 |
private static final int NUM_POINTS = 400;
|
|
48 |
private static final int MAX_VECTORS= 6; |
|
49 | 49 |
|
50 | 50 |
private static final Object lock = new Object(); |
51 | 51 |
|
52 |
private static int xDown,yDown; |
|
53 |
private static int mScrW, mScrH; |
|
54 |
|
|
55 | 52 |
private static Dynamic1D di1D; |
56 | 53 |
private static Dynamic2D di2D; |
57 | 54 |
private static Dynamic3D di3D; |
58 | 55 |
|
59 | 56 |
private static Paint mPaint; |
60 | 57 |
private static int moving; |
61 |
private static long mTime = 0; |
|
62 | 58 |
private static int mDuration; |
63 |
private static float mPosition;
|
|
59 |
private static int mPosition;
|
|
64 | 60 |
private static float mNoise0, mNoise1, mNoise2; |
65 | 61 |
|
66 |
private static int currentDim = DIM_2D;
|
|
62 |
private static int currentDim; |
|
67 | 63 |
|
68 | 64 |
private static Static1D p1D; |
69 | 65 |
private static Static2D p2D; |
... | ... | |
73 | 69 |
private static Static2D p2N; |
74 | 70 |
private static Static3D p3N; |
75 | 71 |
|
76 |
private static float[] mDrawCoord = new float[3];
|
|
72 |
private static float[] mPoints = new float[3*NUM_POINTS];
|
|
77 | 73 |
|
78 | 74 |
/////////////////////////////////////////////////////////////////// |
79 | 75 |
|
... | ... | |
84 | 80 |
mPaint = new Paint(); |
85 | 81 |
mPaint.setStyle(Style.FILL); |
86 | 82 |
mPaint.setAntiAlias(true); |
87 |
|
|
83 |
|
|
84 |
currentDim= DIM_2D; |
|
88 | 85 |
moving = -1; |
89 | 86 |
mDuration = 10000; |
90 | 87 |
mPosition = 0; |
... | ... | |
92 | 89 |
mNoise1 = 0.0f; |
93 | 90 |
mNoise2 = 0.0f; |
94 | 91 |
|
95 |
di1D = new Dynamic1D(mDuration,0.5f);
|
|
92 |
di1D = new Dynamic1D(mDuration,0.0f);
|
|
96 | 93 |
p1N = new Static1D(mNoise0); |
97 |
di2D = new Dynamic2D(mDuration,0.5f);
|
|
94 |
di2D = new Dynamic2D(mDuration,0.0f);
|
|
98 | 95 |
p2N = new Static2D(mNoise0,mNoise1); |
99 |
di3D = new Dynamic3D(mDuration,0.5f);
|
|
96 |
di3D = new Dynamic3D(mDuration,0.0f);
|
|
100 | 97 |
p3N = new Static3D(mNoise0,mNoise1,mNoise2); |
101 | 98 |
|
102 | 99 |
if(!isInEditMode()) |
... | ... | |
124 | 121 |
di2D.setMode(mode); |
125 | 122 |
di3D.setMode(mode); |
126 | 123 |
} |
127 |
|
|
128 |
/////////////////////////////////////////////////////////////////// |
|
129 |
|
|
130 |
public static void setScreenSize(int width, int height) |
|
131 |
{ |
|
132 |
mScrW = width; |
|
133 |
mScrH = height; |
|
134 |
} |
|
135 | 124 |
|
136 | 125 |
/////////////////////////////////////////////////////////////////// |
137 | 126 |
|
... | ... | |
174 | 163 |
di1D.removeAll(); |
175 | 164 |
di2D.removeAll(); |
176 | 165 |
di3D.removeAll(); |
166 |
|
|
167 |
for(int i=0; i<3*NUM_POINTS; i++) |
|
168 |
{ |
|
169 |
mPoints[i] = -10.0f; |
|
170 |
} |
|
177 | 171 |
} |
178 | 172 |
} |
179 | 173 |
|
... | ... | |
185 | 179 |
|
186 | 180 |
public static void drawCurve(Canvas c, long time) |
187 | 181 |
{ |
188 |
mPosition += (mTime>0 && mDuration>0) ? ((float)(time-mTime)*NUM_INTERPOLATIONS/mDuration) : 0;
|
|
182 |
if ( ++mPosition >= NUM_POINTS ) mPosition=0;
|
|
189 | 183 |
|
190 | 184 |
synchronized(lock) |
191 | 185 |
{ |
... | ... | |
196 | 190 |
default : drawCurve3D(c,time); break; |
197 | 191 |
} |
198 | 192 |
} |
199 |
|
|
200 |
mTime = time; |
|
201 | 193 |
} |
202 | 194 |
|
203 | 195 |
/////////////////////////////////////////////////////////////////// |
... | ... | |
205 | 197 |
private static void drawCurve1D(Canvas c, long time) |
206 | 198 |
{ |
207 | 199 |
int len = di1D.getNumPoints(); |
208 |
|
|
200 |
int size1 = DynamicRenderer.texH/200; |
|
201 |
int size2 = DynamicRenderer.texH/80; |
|
202 |
|
|
209 | 203 |
mPaint.setColor(0xff000000); |
210 | 204 |
|
211 |
c.drawLine(0, DynamicRenderer.BHEI/2, DynamicRenderer.BWID, DynamicRenderer.BHEI/2, mPaint);
|
|
212 |
c.drawText("x", 0.95f* DynamicRenderer.BWID, 0.55f* DynamicRenderer.BHEI , mPaint);
|
|
213 |
|
|
205 |
c.drawLine(0, DynamicRenderer.texH/2, DynamicRenderer.texW, DynamicRenderer.texH/2, mPaint);
|
|
206 |
c.drawText("x", 0.95f* DynamicRenderer.texW, 0.55f* DynamicRenderer.texH , mPaint);
|
|
207 |
|
|
214 | 208 |
if( len>=2 ) |
215 | 209 |
{ |
216 |
for(int i=0; i<NUM_INTERPOLATIONS; i++) |
|
210 |
di1D.interpolateMain(mPoints,3*mPosition, time); |
|
211 |
|
|
212 |
for(int i=0; i<NUM_POINTS; i++) |
|
217 | 213 |
{ |
218 |
int color = i<=mPosition ? 0xff - ((int)mPosition -i)*0xff/(NUM_INTERPOLATIONS-1)
|
|
219 |
: 0xff - ((int)mPosition+NUM_INTERPOLATIONS-i)*0xff/(NUM_INTERPOLATIONS-1);
|
|
214 |
int color = i<=mPosition ? 0xff - (mPosition -i)*0xff/(NUM_POINTS-1)
|
|
215 |
: 0xff - (mPosition+NUM_POINTS-i)*0xff/(NUM_POINTS-1);
|
|
220 | 216 |
|
221 | 217 |
mPaint.setColor( 0xffffff + ((color&0xff)<<24) ); |
222 |
di1D.interpolate(mDrawCoord,0, (float)i/NUM_INTERPOLATIONS); |
|
223 |
c.drawCircle(mDrawCoord[0], DynamicRenderer.BHEI/2 , 2.0f, mPaint ); |
|
218 |
c.drawCircle(mPoints[3*i], DynamicRenderer.texH/2 , size1, mPaint ); |
|
224 | 219 |
} |
225 | 220 |
} |
226 | 221 |
|
... | ... | |
229 | 224 |
for(int curr=0; curr<len; curr++) |
230 | 225 |
{ |
231 | 226 |
p1D = di1D.getPoint(curr); |
232 |
c.drawCircle(p1D.getX(), DynamicRenderer.BHEI/2 , 5.0f, mPaint);
|
|
227 |
c.drawCircle(p1D.getX(), DynamicRenderer.texH/2 , size2, mPaint);
|
|
233 | 228 |
} |
234 | 229 |
} |
235 | 230 |
|
... | ... | |
238 | 233 |
private static void drawCurve2D(Canvas c, long time) |
239 | 234 |
{ |
240 | 235 |
int len = di2D.getNumPoints(); |
241 |
|
|
236 |
int size1 = DynamicRenderer.texH/200; |
|
237 |
int size2 = DynamicRenderer.texH/80; |
|
238 |
|
|
242 | 239 |
mPaint.setColor(0xff000000); |
243 | 240 |
|
244 |
c.drawLine(0, DynamicRenderer.BHEI/2, DynamicRenderer.BWID, DynamicRenderer.BHEI/2, mPaint);
|
|
245 |
c.drawLine(DynamicRenderer.BWID/2, 0, DynamicRenderer.BWID/2, DynamicRenderer.BHEI, mPaint);
|
|
241 |
c.drawLine(0, DynamicRenderer.texH/2, DynamicRenderer.texW, DynamicRenderer.texH/2, mPaint);
|
|
242 |
c.drawLine(DynamicRenderer.texW/2, 0, DynamicRenderer.texW/2, DynamicRenderer.texH, mPaint);
|
|
246 | 243 |
|
247 |
c.drawText("x", 0.95f* DynamicRenderer.BWID, 0.55f* DynamicRenderer.BHEI , mPaint);
|
|
248 |
c.drawText("y", 0.55f* DynamicRenderer.BWID, 0.05f* DynamicRenderer.BHEI , mPaint);
|
|
244 |
c.drawText("x", 0.95f* DynamicRenderer.texW, 0.55f* DynamicRenderer.texH , mPaint);
|
|
245 |
c.drawText("y", 0.55f* DynamicRenderer.texW, 0.05f* DynamicRenderer.texH , mPaint);
|
|
249 | 246 |
|
250 | 247 |
if( len>=2 ) |
251 | 248 |
{ |
252 |
for(int i=0; i<NUM_INTERPOLATIONS; i++) |
|
249 |
di2D.interpolateMain(mPoints,3*mPosition, time); |
|
250 |
|
|
251 |
//android.util.Log.e("surface", "pos="+mPosition+" x="+mPoints[3*mPosition]+" y="+mPoints[3*mPosition+1]+" time="+time); |
|
252 |
|
|
253 |
for(int i=0; i<NUM_POINTS; i++) |
|
253 | 254 |
{ |
254 |
int color = i<=mPosition ? 0xff - ((int)mPosition -i)*0xff/(NUM_INTERPOLATIONS-1)
|
|
255 |
: 0xff - ((int)mPosition+NUM_INTERPOLATIONS-i)*0xff/(NUM_INTERPOLATIONS-1);
|
|
255 |
int color = i<=mPosition ? 0xff - (mPosition -i)*0xff/(NUM_POINTS-1)
|
|
256 |
: 0xff - (mPosition+NUM_POINTS-i)*0xff/(NUM_POINTS-1);
|
|
256 | 257 |
|
257 |
mPaint.setColor( 0xffffff + ((color&0xff)<<24) ); |
|
258 |
di2D.interpolate(mDrawCoord,0, (float)i/NUM_INTERPOLATIONS); |
|
259 |
c.drawCircle(mDrawCoord[0], mDrawCoord[1], 2.0f, mPaint ); |
|
258 |
mPaint.setColor( 0xffffff + ((color&0xff)<<24) ); |
|
259 |
c.drawCircle(mPoints[3*i], mPoints[3*i+1], size1, mPaint ); |
|
260 | 260 |
} |
261 | 261 |
} |
262 | 262 |
|
... | ... | |
265 | 265 |
for(int curr=0; curr<len; curr++) |
266 | 266 |
{ |
267 | 267 |
p2D = di2D.getPoint(curr); |
268 |
c.drawCircle(p2D.getX(),p2D.getY(), 5.0f, mPaint);
|
|
268 |
c.drawCircle(p2D.getX(),p2D.getY(), size2, mPaint);
|
|
269 | 269 |
} |
270 | 270 |
} |
271 | 271 |
|
... | ... | |
274 | 274 |
private static void drawCurve3D(Canvas c, long time) |
275 | 275 |
{ |
276 | 276 |
int len = di3D.getNumPoints(); |
277 |
|
|
277 |
int size1 = DynamicRenderer.texH/200; |
|
278 |
int size2 = DynamicRenderer.texH/80; |
|
279 |
|
|
278 | 280 |
mPaint.setColor(0xff000000); |
279 | 281 |
|
280 |
c.drawLine(0, DynamicRenderer.BHEI/2, DynamicRenderer.BWID, DynamicRenderer.BHEI/2, mPaint);
|
|
281 |
c.drawLine(DynamicRenderer.BWID/2, 0, DynamicRenderer.BWID/2, DynamicRenderer.BHEI, mPaint);
|
|
282 |
c.drawLine(0, DynamicRenderer.texH/2, DynamicRenderer.texW , DynamicRenderer.texH/2, mPaint);
|
|
283 |
c.drawLine(DynamicRenderer.texW/2, 0, DynamicRenderer.texW/2, DynamicRenderer.texH , mPaint);
|
|
282 | 284 |
|
283 |
c.drawText("x", 0.95f* DynamicRenderer.BWID, 0.55f* DynamicRenderer.BHEI , mPaint);
|
|
284 |
c.drawText( currentDim==DIM_3DXY ? "y" : "z", 0.55f* DynamicRenderer.BWID, 0.05f* DynamicRenderer.BHEI , mPaint);
|
|
285 |
c.drawText("x", 0.95f* DynamicRenderer.texW, 0.55f* DynamicRenderer.texH , mPaint);
|
|
286 |
c.drawText( currentDim==DIM_3DXY ? "y" : "z", 0.55f* DynamicRenderer.texW, 0.05f* DynamicRenderer.texH , mPaint);
|
|
285 | 287 |
|
286 | 288 |
if( len>=2 ) |
287 | 289 |
{ |
288 |
for(int i=0; i<NUM_INTERPOLATIONS; i++) |
|
290 |
di3D.interpolateMain(mPoints, 3*mPosition, time); |
|
291 |
|
|
292 |
for(int i=0; i<NUM_POINTS; i++) |
|
289 | 293 |
{ |
290 |
int color = i<=mPosition ? 0xff - ((int)mPosition -i)*0xff/(NUM_INTERPOLATIONS-1)
|
|
291 |
: 0xff - ((int)mPosition+NUM_INTERPOLATIONS-i)*0xff/(NUM_INTERPOLATIONS-1);
|
|
294 |
int color = i<=mPosition ? 0xff - (mPosition -i)*0xff/(NUM_POINTS-1)
|
|
295 |
: 0xff - (mPosition+NUM_POINTS-i)*0xff/(NUM_POINTS-1);
|
|
292 | 296 |
|
293 | 297 |
mPaint.setColor( 0xffffff + ((color&0xff)<<24) ); |
294 |
di3D.interpolate(mDrawCoord,0, (float)i/NUM_INTERPOLATIONS); |
|
295 |
c.drawCircle(mDrawCoord[0], mDrawCoord[currentDim==DIM_3DXY ? 1:2], 2.0f, mPaint ); |
|
298 |
c.drawCircle(mPoints[3*i], mPoints[3*i + (currentDim==DIM_3DXY ? 1:2) ], size1, mPaint ); |
|
296 | 299 |
} |
297 | 300 |
} |
298 | 301 |
|
... | ... | |
301 | 304 |
for(int curr=0; curr<len; curr++) |
302 | 305 |
{ |
303 | 306 |
p3D = di3D.getPoint(curr); |
304 |
c.drawCircle(p3D.getX(), currentDim==DIM_3DXY ? p3D.getY():p3D.getZ(), 5.0f, mPaint);
|
|
307 |
c.drawCircle(p3D.getX(), currentDim==DIM_3DXY ? p3D.getY():p3D.getZ(), size2, mPaint);
|
|
305 | 308 |
} |
306 | 309 |
} |
307 | 310 |
|
... | ... | |
321 | 324 |
p1D = di1D.getPoint(g); |
322 | 325 |
gx = p1D.getX(); |
323 | 326 |
|
324 |
if( (xDown-gx)*(xDown-gx) < (DynamicRenderer.BHEI)*(DynamicRenderer.BHEI)/100 )
|
|
327 |
if( (x-gx)*(x-gx) < (DynamicRenderer.texH)*(DynamicRenderer.texH)/100 )
|
|
325 | 328 |
{ |
326 | 329 |
moving = g; |
327 | 330 |
break; |
... | ... | |
332 | 335 |
synchronized(lock) |
333 | 336 |
{ |
334 | 337 |
if( len>=MAX_VECTORS ) di1D.removeAll(); |
335 |
di1D.add(new Static1D(xDown));
|
|
338 |
di1D.add(new Static1D(x)); |
|
336 | 339 |
} |
337 | 340 |
} |
338 | 341 |
break; |
... | ... | |
344 | 347 |
gx = p2D.getX(); |
345 | 348 |
gy = p2D.getY(); |
346 | 349 |
|
347 |
if( (xDown-gx)*(xDown-gx) + (yDown-gy)*(yDown-gy) < (DynamicRenderer.BHEI)*(DynamicRenderer.BHEI)/100 )
|
|
350 |
if( (x-gx)*(x-gx) + (y-gy)*(y-gy) < (DynamicRenderer.texH)*(DynamicRenderer.texH)/100 )
|
|
348 | 351 |
{ |
349 | 352 |
moving = g; |
350 | 353 |
break; |
... | ... | |
355 | 358 |
synchronized(lock) |
356 | 359 |
{ |
357 | 360 |
if( len>=MAX_VECTORS ) di2D.removeAll(); |
358 |
di2D.add(new Static2D(xDown,yDown));
|
|
361 |
di2D.add(new Static2D(x,y));
|
|
359 | 362 |
} |
360 | 363 |
} |
361 | 364 |
break; |
... | ... | |
370 | 373 |
|
371 | 374 |
if( currentDim==DIM_3DXY ) |
372 | 375 |
{ |
373 |
if( (xDown-gx)*(xDown-gx) + (yDown-gy)*(yDown-gy) < (DynamicRenderer.BHEI)*(DynamicRenderer.BHEI)/100 )
|
|
376 |
if( (x-gx)*(x-gx) + (y-gy)*(y-gy) < (DynamicRenderer.texH)*(DynamicRenderer.texH)/100 )
|
|
374 | 377 |
{ |
375 | 378 |
moving = g; |
376 | 379 |
break; |
... | ... | |
378 | 381 |
} |
379 | 382 |
if( currentDim==DIM_3DXZ ) |
380 | 383 |
{ |
381 |
if( (xDown-gx)*(xDown-gx) + (yDown-gz)*(yDown-gz) < (DynamicRenderer.BHEI)*(DynamicRenderer.BHEI)/100 )
|
|
384 |
if( (x-gx)*(x-gx) + (y-gz)*(y-gz) < (DynamicRenderer.texH)*(DynamicRenderer.texH)/100 )
|
|
382 | 385 |
{ |
383 | 386 |
moving = g; |
384 | 387 |
break; |
... | ... | |
393 | 396 |
|
394 | 397 |
if( currentDim==DIM_3DXY ) |
395 | 398 |
{ |
396 |
di3D.add(new Static3D(xDown,yDown, DynamicRenderer.BHEI/2));
|
|
399 |
di3D.add(new Static3D(x,y, DynamicRenderer.texH/2));
|
|
397 | 400 |
} |
398 | 401 |
if( currentDim==DIM_3DXZ ) |
399 | 402 |
{ |
400 |
di3D.add(new Static3D(xDown, DynamicRenderer.BHEI/2,yDown));
|
|
403 |
di3D.add(new Static3D(x, DynamicRenderer.texH/2,y));
|
|
401 | 404 |
} |
402 | 405 |
} |
403 | 406 |
} |
... | ... | |
410 | 413 |
@Override public boolean onTouchEvent(MotionEvent event) |
411 | 414 |
{ |
412 | 415 |
int action = event.getAction(); |
413 |
|
|
416 |
int xDown, yDown; |
|
417 |
|
|
414 | 418 |
switch(action) |
415 | 419 |
{ |
416 |
case MotionEvent.ACTION_DOWN: xDown = (int)event.getX()* DynamicRenderer.BWID/mScrW;
|
|
417 |
yDown = (int)event.getY()* DynamicRenderer.BHEI/mScrH;
|
|
420 |
case MotionEvent.ACTION_DOWN: xDown = (int)event.getX(); |
|
421 |
yDown = (int)event.getY(); |
|
418 | 422 |
|
419 | 423 |
addNewPoint(xDown,yDown); |
420 | 424 |
|
421 | 425 |
break; |
422 | 426 |
case MotionEvent.ACTION_MOVE: if( moving>=0 ) |
423 | 427 |
{ |
424 |
xDown = (int)event.getX()* DynamicRenderer.BWID/mScrW;
|
|
425 |
yDown = (int)event.getY()* DynamicRenderer.BHEI/mScrH;
|
|
428 |
xDown = (int)event.getX(); |
|
429 |
yDown = (int)event.getY(); |
|
426 | 430 |
|
427 | 431 |
switch(currentDim) |
428 | 432 |
{ |
Also available in: Unified diff
1. Attempt to deal with unstable Orthonormal Base in Dynamic class (so far unsuccessful)
2. Improvements to the 'Dynamic' (and by necessity, 'MovingEffects') applications (to be able to debug the previous)