Project

General

Profile

« Previous | Next » 

Revision 2666a48c

Added by Leszek Koltunski about 5 years ago

1. Change the API of Dynamic: split makeNowRunFor into two separate 'setDuration' and 'resetToBeginning'
2. Major changes to the 'Dynamic' app so that we can check more about the Dynamics.

View differences:

src/main/java/org/distorted/examples/dynamic/DynamicSurfaceView.java
22 22
import android.app.ActivityManager;
23 23
import android.content.Context;
24 24
import android.content.pm.ConfigurationInfo;
25
import android.graphics.Rect;
25 26
import android.opengl.GLSurfaceView;
26 27
import android.view.MotionEvent;
27 28
import android.util.AttributeSet;
......
47 48
    public static final int DIM_3DXZ = 3; 
48 49

  
49 50
    private static final int NUM_POINTS = 250;
50
    private static final int MAX_POINTS =   6;
51

  
52 51
    private static final Object lock = new Object();
53 52

  
53
    private static int halfScreenHeight=0;
54
    private static int halfScreenWidth =0;
55

  
54 56
    private Dynamic1D di1D;
55 57
    private Dynamic2D di2D;
56 58
    private Dynamic3D di3D;
57 59
    
58 60
    private Paint mPaint;
59
    private int moving;
61
    private int mMoving;
60 62
    private int mDuration;
61 63
    private int mPosition;
62
    private long mDiffTime, mLastTime;
64
    private long mDiffTime, mLastTime, mStartTime;
63 65
    private float mNoise0, mNoise1, mNoise2;
66
    private float mCount;
64 67

  
65 68
    private int mSize1, mSize2, mSizeT, mAvg;
69
    private float mFontHeight;
66 70

  
67 71
    private int currentDim= DIM_2D;
68 72
    
......
75 79
    private Static3D p3N;
76 80

  
77 81
    private float[] mPoints = new float[3*NUM_POINTS];
78
      
82
    private boolean mRunning;
83

  
79 84
///////////////////////////////////////////////////////////////////
80 85
    
81 86
    public DynamicSurfaceView(Context context, AttributeSet attrs)
......
86 91
      mPaint.setStyle(Style.FILL);
87 92
      mPaint.setAntiAlias(true);
88 93

  
89
      moving    = -1;
94
      mMoving = -1;
90 95
      mDuration = 10000;
96
      mCount    = 0.0f;
91 97
      mPosition = 0;
92 98
      mNoise0   = 0.0f;
93 99
      mNoise1   = 0.0f;
94 100
      mNoise2   = 0.0f;
95 101
      mDiffTime = -1;
96 102
      mLastTime = -1;
103
      mStartTime= -1;
104

  
105
      mRunning = false;
97 106

  
98
      di1D = new Dynamic1D(mDuration,0.0f);
107
      di1D = new Dynamic1D(mDuration,mCount);
99 108
      p1N  = new Static1D(mNoise0);
100
      di2D = new Dynamic2D(mDuration,0.0f);
109
      di2D = new Dynamic2D(mDuration,mCount);
101 110
      p2N  = new Static2D(mNoise0,mNoise1);
102
      di3D = new Dynamic3D(mDuration,0.0f);
111
      di3D = new Dynamic3D(mDuration,mCount);
103 112
      p3N  = new Static3D(mNoise0,mNoise1,mNoise2);
104 113

  
105 114
      di1D.setAccessMode(Dynamic.ACCESS_SEQUENTIAL);
......
125 134
      mAvg = (width+height)/2;
126 135

  
127 136
      mSize1 = mAvg/150;
128
      mSize2 = mAvg/60;
137
      mSize2 = mAvg/50;
129 138
      mSizeT = mAvg/30;
130 139

  
131 140
      mPaint.setTextSize(mSizeT);
141
      mPaint.setTextAlign(Paint.Align.CENTER);
142

  
143
      final Rect textBounds = new Rect();
144
      String text = "1";
145
      mPaint.getTextBounds(text, 0, text.length(), textBounds);
146
      mFontHeight = textBounds.exactCenterY();
132 147

  
133 148
      clearPoints();
134 149
      }
135 150

  
151
///////////////////////////////////////////////////////////////////
152

  
153
    public static void setHalfWidth(int hw)
154
      {
155
      halfScreenWidth = hw;
156
      }
157

  
158
///////////////////////////////////////////////////////////////////
159

  
160
    public static void setHalfHeight(int hh)
161
      {
162
      halfScreenHeight = hh;
163
      }
164

  
136 165
///////////////////////////////////////////////////////////////////
137 166

  
138 167
    public void setMode(int mode)
......
148 177
      {
149 178
      mDuration = duration;
150 179
      
151
      di1D.makeRunNowFor(duration);
152
      di2D.makeRunNowFor(duration);
153
      di3D.makeRunNowFor(duration);
180
      di1D.setDuration(duration);
181
      di2D.setDuration(duration);
182
      di3D.setDuration(duration);
183
      }
184

  
185
///////////////////////////////////////////////////////////////////
186

  
187
    public void setCount(float count)
188
      {
189
      mCount = count;
190

  
191
      di1D.setCount(count);
192
      di2D.setCount(count);
193
      di3D.setCount(count);
154 194
      }
155 195

  
156 196
///////////////////////////////////////////////////////////////////
......
178 218
        {
179 219
        if( !(currentDim==DIM_3DXY && dim==DIM_3DXZ) && !(currentDim==DIM_3DXZ && dim==DIM_3DXY) )
180 220
          {
181
          synchronized(lock)
182
            {
183
            di1D.removeAll();
184
            di2D.removeAll();
185
            di3D.removeAll();
186

  
187
            clearPoints();
188
            }
221
          resetPoints();
189 222
          }
190
      
223

  
191 224
        currentDim = dim;
192 225
        }
193 226
      }
......
196 229
    
197 230
    public void drawCurve(Canvas c, long time)
198 231
      {
199
      if ( ++mPosition >= NUM_POINTS ) mPosition=0;
200

  
201 232
      if( mLastTime<0 )
202 233
        {
203 234
        mLastTime = time;
......
211 242
        {
212 243
        switch(currentDim)
213 244
          {
214
          case DIM_1D: drawCurve1D(c,time); break;
215
          case DIM_2D: drawCurve2D(c,time); break;
216
          default    : drawCurve3D(c,time); break;
245
          case DIM_1D: drawHorizontalAxis(c,"x");
246
                       drawPath(c,di1D,1,time);
247
                       drawRedPoints1D(c);
248
                       break;
249
          case DIM_2D: drawHorizontalAxis(c,"x");
250
                       drawVerticalAxis  (c,"y");
251
                       drawPath(c,di2D,1,time);
252
                       drawRedPoints2D(c);
253
                       break;
254
          default    : drawHorizontalAxis(c,"x");
255
                       drawVerticalAxis  (c, currentDim==DIM_3DXY ? "y" : "z" );
256
                       drawPath(c,di3D,(currentDim==DIM_3DXY ? 1:2),time);
257
                       drawRedPoints3D(c);
258
                       break;
217 259
          }
218 260
        }
219 261

  
......
232 274

  
233 275
///////////////////////////////////////////////////////////////////
234 276

  
235
    private void drawCurve1D(Canvas c, long time)
277
    public void resetPoints()
236 278
      {
237
      int len = di1D.getNumPoints();   
238
      mPaint.setColor(0xff000000);
239
      
240
      c.drawLine(0, DynamicRenderer.texH/2, DynamicRenderer.texW, DynamicRenderer.texH/2, mPaint);
241
      c.drawText("x", 0.95f*DynamicRenderer.texW, DynamicRenderer.texH /2 + mSizeT , mPaint);
242

  
243
      if( len>=2 )
279
      synchronized(lock)
244 280
        {
245
        di1D.get(mPoints,3*mPosition, time, mDiffTime);
281
        clearPoints();
246 282

  
247
        for(int i=0; i<NUM_POINTS; i++)
283
        switch(currentDim)
248 284
          {
249
          int color = i<=mPosition ? 0xff - (mPosition           -i)*0xff/(NUM_POINTS-1)
250
                                   : 0xff - (mPosition+NUM_POINTS-i)*0xff/(NUM_POINTS-1);
251
         
252
          mPaint.setColor( 0xffffff + ((color&0xff)<<24) ); 
253
          c.drawCircle(mPoints[3*i], DynamicRenderer.texH/2 , mSize1, mPaint );
285
          case DIM_1D  : di1D.removeAll(); break;
286
          case DIM_2D  : di2D.removeAll(); break;
287
          case DIM_3DXY:
288
          case DIM_3DXZ: di3D.removeAll(); break;
254 289
          }
255 290
        }
256
     
257
      mPaint.setColor(0xffff0000);
258
      
259
      for(int curr=0; curr<len; curr++)
260
        {      
261
        p1D = di1D.getPoint(curr);
262
        c.drawCircle(p1D.get1(), DynamicRenderer.texH/2 , mSize2, mPaint);
263
        }   
264 291
      }
265
    
292

  
266 293
///////////////////////////////////////////////////////////////////
267
      
268
    private void drawCurve2D(Canvas c, long time)
294

  
295
    public void startDynamic()
296
      {
297
      mRunning = true;
298
      mLastTime= -1;
299
      mStartTime = System.currentTimeMillis();
300

  
301
      clearPoints();
302
      di1D.resetToBeginning();
303
      di2D.resetToBeginning();
304
      di3D.resetToBeginning();
305
      }
306

  
307
///////////////////////////////////////////////////////////////////
308

  
309
    public void stopDynamic()
310
      {
311
      mRunning = false;
312
      }
313

  
314
///////////////////////////////////////////////////////////////////
315

  
316
    private void drawHorizontalAxis(Canvas c, String label)
269 317
      {
270
      int len = di2D.getNumPoints();   
271 318
      mPaint.setColor(0xff000000);
272
      
273
      c.drawLine(0, DynamicRenderer.texH/2, DynamicRenderer.texW, DynamicRenderer.texH/2, mPaint);
274
      c.drawLine(DynamicRenderer.texW/2, 0, DynamicRenderer.texW/2, DynamicRenderer.texH, mPaint);
275
      
276
      c.drawText("x", 0.95f* DynamicRenderer.texW    , DynamicRenderer.texH/2+mSizeT , mPaint);
277
      c.drawText("y", DynamicRenderer.texW/2 + mSizeT,                        mSizeT , mPaint);
278
      
319

  
320
      c.drawLine(0, halfScreenHeight, halfScreenWidth*2, halfScreenHeight, mPaint);
321
      c.drawText( label, 0.95f*halfScreenWidth*2, halfScreenHeight + mSizeT , mPaint);
322
      }
323

  
324

  
325
///////////////////////////////////////////////////////////////////
326

  
327
    private void drawVerticalAxis(Canvas c, String label)
328
      {
329
      mPaint.setColor(0xff000000);
330

  
331
      c.drawLine(halfScreenWidth, 0, halfScreenWidth, halfScreenHeight*2, mPaint);
332
      c.drawText(label, halfScreenWidth + mSizeT,                mSizeT , mPaint);
333
      }
334

  
335
///////////////////////////////////////////////////////////////////
336

  
337
    private void drawPath(Canvas c, Dynamic dyn, int index, long time)
338
      {
339
      int len = dyn.getNumPoints();
340

  
279 341
      if( len>=2 )
280 342
        {
281
        di2D.get(mPoints,3*mPosition, time, mDiffTime);
343
        if( mRunning )
344
          {
345
          if ( ++mPosition >= NUM_POINTS ) mPosition=0;
346

  
347
          if( dyn.getDimension()==1 )
348
            {
349
            mPoints[3*mPosition+index] = halfScreenHeight;
350
            }
351

  
352
          if( dyn.get(mPoints,3*mPosition, time-mStartTime, mDiffTime) )
353
            {
354
            stopDynamic();
355
            }
356
          }
282 357

  
283 358
        for(int i=0; i<NUM_POINTS; i++)
284 359
          {
285 360
          int color = i<=mPosition ? 0xff - (mPosition           -i)*0xff/(NUM_POINTS-1)
286 361
                                   : 0xff - (mPosition+NUM_POINTS-i)*0xff/(NUM_POINTS-1);
287
         
362

  
288 363
          mPaint.setColor( 0xffffff + ((color&0xff)<<24) );
289
          c.drawCircle(mPoints[3*i], mPoints[3*i+1], mSize1, mPaint );
364
          c.drawCircle(mPoints[3*i], mPoints[3*i+index] , mSize1, mPaint );
290 365
          }
291 366
        }
292
     
293
      mPaint.setColor(0xffff0000);
294
      
367
      }
368

  
369
///////////////////////////////////////////////////////////////////
370

  
371
    private void drawRedPoints1D(Canvas c)
372
      {
373
      int len = di1D.getNumPoints();
374

  
295 375
      for(int curr=0; curr<len; curr++)
296
        {      
297
        p2D = di2D.getPoint(curr);
298
        c.drawCircle(p2D.get1(),p2D.get2(), mSize2, mPaint);
376
        {
377
        p1D = di1D.getPoint(curr);
378
        drawRedPoint(c,curr+"", p1D.get1(), halfScreenHeight);
299 379
        }
300 380
      }
301 381

  
302 382
///////////////////////////////////////////////////////////////////
303
      
304
    private void drawCurve3D(Canvas c, long time)
383

  
384
    private void drawRedPoints2D(Canvas c)
305 385
      {
306
      int len = di3D.getNumPoints();   
307
      mPaint.setColor(0xff000000);
308
      
309
      c.drawLine(0, DynamicRenderer.texH/2, DynamicRenderer.texW  , DynamicRenderer.texH/2, mPaint);
310
      c.drawLine(DynamicRenderer.texW/2, 0, DynamicRenderer.texW/2, DynamicRenderer.texH  , mPaint);
311
      
312
      c.drawText( "x"                             , 0.95f* DynamicRenderer.texW    , DynamicRenderer.texH/2 + mSizeT , mPaint);
313
      c.drawText( currentDim==DIM_3DXY ? "y" : "z", DynamicRenderer.texW/2 + mSizeT,                          mSizeT , mPaint);
314
      
315
      if( len>=2 )
316
        {
317
        di3D.get(mPoints, 3*mPosition, time, mDiffTime);
386
      int len = di2D.getNumPoints();
318 387

  
319
        for(int i=0; i<NUM_POINTS; i++)
320
          {
321
          int color = i<=mPosition ? 0xff - (mPosition           -i)*0xff/(NUM_POINTS-1)
322
                                   : 0xff - (mPosition+NUM_POINTS-i)*0xff/(NUM_POINTS-1);
323
         
324
          mPaint.setColor( 0xffffff + ((color&0xff)<<24) ); 
325
          c.drawCircle(mPoints[3*i], mPoints[3*i + (currentDim==DIM_3DXY ? 1:2) ], mSize1, mPaint );
326
          }
388
      for(int curr=0; curr<len; curr++)
389
        {
390
        p2D = di2D.getPoint(curr);
391
        drawRedPoint(c,curr+"", p2D.get1(), p2D.get2());
327 392
        }
328
     
329
      mPaint.setColor(0xffff0000);
330
      
393
      }
394

  
395
///////////////////////////////////////////////////////////////////
396

  
397
    private void drawRedPoints3D(Canvas c)
398
      {
399
      int len = di3D.getNumPoints();
400

  
331 401
      for(int curr=0; curr<len; curr++)
332
        {      
402
        {
333 403
        p3D = di3D.getPoint(curr);
334
        c.drawCircle(p3D.get1(), currentDim==DIM_3DXY ? p3D.get2():p3D.get3(), mSize2, mPaint);
335
        }   
404
        drawRedPoint(c,curr+"", p3D.get1(), currentDim==DIM_3DXY ? p3D.get2():p3D.get3());
405
        }
336 406
      }
337
    
407

  
408
///////////////////////////////////////////////////////////////////
409

  
410
    private void drawRedPoint(Canvas c, String label, float width, float height)
411
      {
412
      mPaint.setColor(0xffff0000);
413
      c.drawCircle( width, height, mSize2, mPaint);
414
      mPaint.setColor(0xffffffff);
415
      c.drawText(label, width,height-mFontHeight, mPaint);
416
      }
417

  
338 418
///////////////////////////////////////////////////////////////////
339 419

  
340 420
    private void addNewPoint(int x, int y)
......
353 433
                                    
354 434
                       if( (x-gx)*(x-gx) < (mAvg*mAvg/100) )
355 435
                         {
356
                         moving = g;
436
                         mMoving = g;
357 437
                         break;
358 438
                         }
359 439
                       }
360
                     if( moving<0 )
440
                     if( mMoving <0 )
361 441
                       {
362 442
                       synchronized(lock)
363 443
                         {
364
                         if( len>=MAX_POINTS )
365
                           {
366
                           di1D.removeAll();
367
                           clearPoints();
368
                           }
369 444
                         di1D.add(new Static1D(x));
370 445
                         }
371 446
                       }
......
380 455
                                    
381 456
                       if( (x-gx)*(x-gx) + (y-gy)*(y-gy) < (mAvg*mAvg/100) )
382 457
                         {
383
                         moving = g;
458
                         mMoving = g;
384 459
                         break;
385 460
                         }
386 461
                       }
387
                     if( moving<0 )
462
                     if( mMoving <0 )
388 463
                       {
389 464
                       synchronized(lock)
390 465
                         {
391
                         if( len>=MAX_POINTS )
392
                           {
393
                           di2D.removeAll();
394
                           clearPoints();
395
                           }
396 466
                         di2D.add(new Static2D(x,y));
397 467
                         }
398 468
                       }
......
410 480
                       {
411 481
                       if( (x-gx)*(x-gx) + (y-gy)*(y-gy) < (mAvg*mAvg/100) )
412 482
                         {
413
                         moving = g;
483
                         mMoving = g;
414 484
                         break;
415 485
                         }
416 486
                       }
......
418 488
                       {
419 489
                       if( (x-gx)*(x-gx) + (y-gz)*(y-gz) < (mAvg*mAvg/100) )
420 490
                         {
421
                         moving = g;
491
                         mMoving = g;
422 492
                         break;
423 493
                         }
424 494
                       }
425 495
                     }
426
                   if( moving<0 )
496
                   if( mMoving <0 )
427 497
                     { 
428 498
                     synchronized(lock)
429 499
                       {
430
                       if( len>=MAX_POINTS )
431
                         {
432
                         di3D.removeAll();
433
                         clearPoints();
434
                         }
435
                    
436 500
                       if( currentDim==DIM_3DXY )
437 501
                         {
438
                         di3D.add(new Static3D(x,y, DynamicRenderer.texH/2));
502
                         di3D.add(new Static3D(x,y, halfScreenHeight));
439 503
                         }
440 504
                       if( currentDim==DIM_3DXZ )
441 505
                         {
442
                         di3D.add(new Static3D(x, DynamicRenderer.texH/2,y));
506
                         di3D.add(new Static3D(x, halfScreenHeight,y));
443 507
                         }
444 508
                       }
445 509
                     }
......
449 513
    
450 514
///////////////////////////////////////////////////////////////////
451 515
    
452
    @Override public boolean onTouchEvent(MotionEvent event) 
516
    @Override
517
    public boolean onTouchEvent(MotionEvent event)
453 518
      {
454 519
      int action = event.getAction();
455 520
      int xDown, yDown;
......
462 527
                                      addNewPoint(xDown,yDown);
463 528
                                    
464 529
                                      break;
465
        case MotionEvent.ACTION_MOVE: if( moving>=0 )
530
        case MotionEvent.ACTION_MOVE: if( mMoving >=0 )
466 531
                                        {
467 532
                                        xDown = (int)event.getX();
468 533
                                        yDown = (int)event.getY();
469 534
                                        
470 535
                                        switch(currentDim)
471 536
                                          {
472
                                          case DIM_1D  : di1D.setPoint(moving, xDown); 
537
                                          case DIM_1D  : di1D.setPoint(mMoving, xDown);
473 538
                                                         break;
474
                                          case DIM_2D  : di2D.setPoint(moving, xDown, yDown);
539
                                          case DIM_2D  : di2D.setPoint(mMoving, xDown, yDown);
475 540
                                                         break;
476
                                          case DIM_3DXY: di3D.setPoint(moving, xDown, yDown, (int)di3D.getPoint(moving).get3());
541
                                          case DIM_3DXY: di3D.setPoint(mMoving, xDown, yDown, (int)di3D.getPoint(mMoving).get3());
477 542
                                                         break;
478
                                          case DIM_3DXZ: di3D.setPoint(moving, xDown, (int)di3D.getPoint(moving).get2(), yDown);
543
                                          case DIM_3DXZ: di3D.setPoint(mMoving, xDown, (int)di3D.getPoint(mMoving).get2(), yDown);
479 544
                                                         break;
480 545
                                          }
481 546
                                        }                           
482 547
                                      break;
483
        case MotionEvent.ACTION_UP  : moving = -1;
548
        case MotionEvent.ACTION_UP  : mMoving = -1;
484 549
                                      break;
485 550
        }
486 551
            

Also available in: Unified diff