Project

General

Profile

« Previous | Next » 

Revision 76c2bd07

Added by Leszek Koltunski about 4 years ago

Simplify drawing textures.

View differences:

src/main/java/org/distorted/objects/TwistyCube.java
129 129

  
130 130
  void createFaceTexture(Canvas canvas, Paint paint, int face, int left)
131 131
    {
132
    final float R = TEXTURE_HEIGHT*0.10f;
133
    final float M = TEXTURE_HEIGHT*0.05f;
132
    float F =  0.5f;
133
    float R = 0.10f;
134
    float S = 0.10f;
135
    float[] vertices = { -F,-F, +F,-F, +F,+F, -F,+F};
134 136

  
135
    paint.setColor(FACE_COLORS[face]);
136
    canvas.drawRoundRect( left+M, M, left+TEXTURE_HEIGHT-M, TEXTURE_HEIGHT-M, R, R, paint);
137
    drawRoundedPolygon(canvas, paint, left, vertices, S, FACE_COLORS[face], R);
137 138
    }
138 139

  
139 140
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/objects/TwistyDiamond.java
248 248

  
249 249
  void createFaceTexture(Canvas canvas, Paint paint, int face, int left)
250 250
    {
251
    float STROKE = 0.044f*TEXTURE_HEIGHT;
252
    float OFF = STROKE/2 -1;
253
    float OFF2 = 0.5f*TEXTURE_HEIGHT + OFF;
254
    float HEIGHT = TEXTURE_HEIGHT - OFF;
255
    float RADIUS = TEXTURE_HEIGHT/12.0f;
256
    float ARC1_H = 0.2f*TEXTURE_HEIGHT;
257
    float ARC1_W = TEXTURE_HEIGHT*0.5f;
258
    float ARC2_W = 0.153f*TEXTURE_HEIGHT;
259
    float ARC2_H = 0.905f*TEXTURE_HEIGHT;
260
    float ARC3_W = TEXTURE_HEIGHT-ARC2_W;
261

  
262
    float M = SQ3/2;
263
    float D = (M/2 - 0.51f)*TEXTURE_HEIGHT;
264

  
265
    paint.setAntiAlias(true);
266
    paint.setStrokeWidth(STROKE);
267
    paint.setColor(FACE_COLORS[face]);
268
    paint.setStyle(Paint.Style.FILL);
269

  
270
    canvas.drawRect(left,0,left+TEXTURE_HEIGHT,TEXTURE_HEIGHT,paint);
271

  
272
    paint.setColor(INTERIOR_COLOR);
273
    paint.setStyle(Paint.Style.STROKE);
274

  
275
    canvas.drawLine(                     left,         M*HEIGHT+D,  TEXTURE_HEIGHT       +left, M*HEIGHT+D, paint);
276
    canvas.drawLine(                OFF +left, M*TEXTURE_HEIGHT+D,                 OFF2  +left,          D, paint);
277
    canvas.drawLine((TEXTURE_HEIGHT-OFF)+left, M*TEXTURE_HEIGHT+D, (TEXTURE_HEIGHT-OFF2) +left,          D, paint);
278

  
279
    canvas.drawArc( ARC1_W-RADIUS+left, M*(ARC1_H-RADIUS)+D, ARC1_W+RADIUS+left, M*(ARC1_H+RADIUS)+D, 225, 90, false, paint);
280
    canvas.drawArc( ARC2_W-RADIUS+left, M*(ARC2_H-RADIUS)+D, ARC2_W+RADIUS+left, M*(ARC2_H+RADIUS)+D, 105, 90, false, paint);
281
    canvas.drawArc( ARC3_W-RADIUS+left, M*(ARC2_H-RADIUS)+D, ARC3_W+RADIUS+left, M*(ARC2_H+RADIUS)+D, 345, 90, false, paint);
251
    float E = SQ3/2;
252
    float F =  0.5f;
253
    float R = 0.06f;
254
    float S = 0.07f;
255
    float[] vertices = { -F,-E/3, +F,-E/3, 0.0f,2*E/3};
256

  
257
    drawRoundedPolygon(canvas, paint, left, vertices, S, FACE_COLORS[face], R);
282 258
    }
283 259

  
284 260
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/objects/TwistyDino.java
208 208

  
209 209
  void createFaceTexture(Canvas canvas, Paint paint, int face, int left)
210 210
    {
211
    float STROKE = 0.04f*TEXTURE_HEIGHT;
212
    float L= left;
213
    float H= 0.333f*TEXTURE_HEIGHT;
214
    float LEN = 0.5f*TEXTURE_HEIGHT;
215

  
216
    paint.setAntiAlias(true);
217
    paint.setStrokeWidth(STROKE);
218
    paint.setColor(FACE_COLORS[face]);
219
    paint.setStyle(Paint.Style.FILL);
220

  
221
    canvas.drawRect(left,0,left+TEXTURE_HEIGHT,TEXTURE_HEIGHT,paint);
222

  
223
    paint.setColor(INTERIOR_COLOR);
224
    paint.setStyle(Paint.Style.STROKE);
225

  
226
    canvas.drawLine( L      , H,  L+2*LEN, H    , paint);
227
    canvas.drawLine( L      , H,  L+  LEN, H+LEN, paint);
228
    canvas.drawLine( L+2*LEN, H,  L+  LEN, H+LEN, paint);
229

  
230
    float S1 = 0.150f*TEXTURE_HEIGHT;
231
    float S2 = 0.090f*TEXTURE_HEIGHT;
232
    float X  = 0.7f*S2;
233
    float Y  = 0.2f*S1;
234

  
235
    float LA = left+0.500f*TEXTURE_HEIGHT;
236
    float RA = left;
237
    float TA = 0.333f*TEXTURE_HEIGHT;
238
    float BA = 0.833f*TEXTURE_HEIGHT;
239

  
240
    canvas.drawArc( RA+X                  , TA     , RA+X+S2            , TA+S2, 135,135, false, paint);
241
    canvas.drawArc( RA+TEXTURE_HEIGHT-S2-X, TA     , RA+TEXTURE_HEIGHT-X, TA+S2, 270,135, false, paint);
242
    canvas.drawArc( LA-S1/2               , BA-S1-Y, LA+S1/2            , BA-Y ,  45, 90, false, paint);
211
    float F = 0.5f;
212
    float R = 0.035f;
213
    float S = 0.05f;
214
    float[] vertices = { -F,F/3, 0,-2*F/3, +F,F/3 };
215

  
216
    drawRoundedPolygon(canvas, paint, left, vertices, S, FACE_COLORS[face], R);
243 217
    }
244 218

  
245 219
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/objects/TwistyHelicopter.java
287 287

  
288 288
  void createFaceTexture(Canvas canvas, Paint paint, int face, int left)
289 289
    {
290
    float STROKE = 0.035f*TEXTURE_HEIGHT;
291
    float L= left+0.125f*TEXTURE_HEIGHT;
292
    float H= 0.375f*TEXTURE_HEIGHT;
293
    float LEN = 0.5f*TEXTURE_HEIGHT;
290
    float R = 0.025f;
291
    float S = 0.035f;
292
    float E = 0.5f;
293
    float[] vertices = { -E+E/4,E/4, E/4,-E+E/4, E/4,E/4};
294 294

  
295
    paint.setAntiAlias(true);
296
    paint.setStrokeWidth(STROKE);
297
    paint.setColor(FACE_COLORS[face]);
298
    paint.setStyle(Paint.Style.FILL);
299

  
300
    canvas.drawRect(left,0,left+TEXTURE_HEIGHT,TEXTURE_HEIGHT,paint);
301

  
302
    paint.setColor(INTERIOR_COLOR);
303
    paint.setStyle(Paint.Style.STROKE);
304

  
305
    canvas.drawLine( L    , H,  L+LEN, H    , paint);
306
    canvas.drawLine( L    , H,  L+LEN, H+LEN, paint);
307
    canvas.drawLine( L+LEN, H,  L+LEN, H+LEN, paint);
308

  
309
    float S1 = 0.125f*TEXTURE_HEIGHT;
310
    float S2 = 0.070f*TEXTURE_HEIGHT;
311
    float X  = 0.7f*S2;
312

  
313
    float LA = left+0.625f*TEXTURE_HEIGHT;
314
    float RA = left+0.125f*TEXTURE_HEIGHT;
315
    float TA = 0.375f*TEXTURE_HEIGHT;
316
    float BA = 0.875f*TEXTURE_HEIGHT;
317

  
318
    canvas.drawArc( LA-S1, TA     , LA     , TA+S1, 270, 90, false, paint);
319
    canvas.drawArc( RA+X , TA     , RA+X+S2, TA+S2, 135,135, false, paint);
320
    canvas.drawArc( LA-S2, BA-X-S2, LA     , BA-X ,   0,135, false, paint);
295
    drawRoundedPolygon(canvas, paint, left, vertices, S, FACE_COLORS[face], R);
321 296
    }
322 297

  
323 298
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/objects/TwistyObject.java
341 341

  
342 342
///////////////////////////////////////////////////////////////////////////////////////////////////
343 343

  
344
  void drawRoundedPolygon()
344
  private float computeAngle(float dx, float dy)
345 345
    {
346
    float PI = (float)Math.PI;
347
    double angle = Math.atan2(dy,dx);
348
    float ret = (float)(3*PI/2-angle);
346 349

  
350
    if( ret>2*PI ) ret-= 2*PI;
351

  
352
    return ret;
353
    }
354

  
355
///////////////////////////////////////////////////////////////////////////////////////////////////
356

  
357
  private void drawCurrVertex(Canvas canvas, Paint paint, float left, float r, float stroke, float pX,float pY, float cX, float cY, float nX, float nY)
358
    {
359
    pX = (0.5f+pX)*TEXTURE_HEIGHT;
360
    pY = (0.5f-pY)*TEXTURE_HEIGHT;
361
    cX = (0.5f+cX)*TEXTURE_HEIGHT;
362
    cY = (0.5f-cY)*TEXTURE_HEIGHT;
363
    nX = (0.5f+nX)*TEXTURE_HEIGHT;
364
    nY = (0.5f-nY)*TEXTURE_HEIGHT;
365

  
366
    canvas.drawLine(left+pX,pY,left+cX,cY,paint);
367

  
368
    float aX = pX-cX;
369
    float aY = pY-cY;
370
    float bX = cX-nX;
371
    float bY = cY-nY;
372

  
373
    float aLen = (float)Math.sqrt(aX*aX+aY*aY);
374
    float bLen = (float)Math.sqrt(bX*bX+bY*bY);
375

  
376
    aX /= aLen;
377
    aY /= aLen;
378
    bX /= bLen;
379
    bY /= bLen;
380

  
381
    float sX = (aX-bX)/2;
382
    float sY = (aY-bY)/2;
383
    float sLen = (float)Math.sqrt(sX*sX+sY*sY);
384
    sX /= sLen;
385
    sY /= sLen;
386

  
387
    float startAngle = computeAngle(bX,-bY);
388
    float endAngle   = computeAngle(aX,-aY);
389
    float sweepAngle = endAngle-startAngle;
390
    if( sweepAngle<0 ) sweepAngle += 2*Math.PI;
391

  
392
    float R = r*TEXTURE_HEIGHT+stroke/2;
393

  
394
    float A = (float)(R/(Math.cos(sweepAngle/2)));
395

  
396
    float rX = cX + A*sX;
397
    float rY = cY + A*sY;
398

  
399
    startAngle *= 180/(Math.PI);
400
    sweepAngle *= 180/(Math.PI);
401

  
402
    canvas.drawArc( left+rX-R, rY-R, left+rX+R, rY+R, startAngle, sweepAngle, false, paint);
403
    }
404

  
405
///////////////////////////////////////////////////////////////////////////////////////////////////
406

  
407
  void drawRoundedPolygon(Canvas canvas, Paint paint, int left, float[] vertices, float stroke, int color, float r)
408
    {
409
    stroke *= TEXTURE_HEIGHT;
410

  
411
    paint.setAntiAlias(true);
412
    paint.setStrokeWidth(stroke);
413
    paint.setColor(color);
414
    paint.setStyle(Paint.Style.FILL);
415

  
416
    canvas.drawRect(left,0,left+TEXTURE_HEIGHT,TEXTURE_HEIGHT,paint);
417

  
418
    paint.setColor(INTERIOR_COLOR);
419
    paint.setStyle(Paint.Style.STROKE);
420

  
421
    int length = vertices.length;
422
    int numVertices = length/2;
423

  
424
    float prevX = vertices[length-2];
425
    float prevY = vertices[length-1];
426
    float currX = vertices[0];
427
    float currY = vertices[1];
428
    float nextX = vertices[2];
429
    float nextY = vertices[3];
430

  
431
    for(int vert=0; vert<numVertices; vert++)
432
      {
433
      drawCurrVertex(canvas, paint, left, r, stroke, prevX,prevY,currX,currY,nextX,nextY);
434

  
435
      prevX = currX;
436
      prevY = currY;
437
      currX = nextX;
438
      currY = nextY;
439

  
440
      if( 2*(vert+2)+1 < length )
441
        {
442
        nextX = vertices[2*(vert+2)  ];
443
        nextY = vertices[2*(vert+2)+1];
444
        }
445
      else
446
        {
447
        nextX = vertices[0];
448
        nextY = vertices[1];
449
        }
450
      }
347 451
    }
348 452

  
349 453
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/objects/TwistyPyraminx.java
236 236

  
237 237
  void createFaceTexture(Canvas canvas, Paint paint, int face, int left)
238 238
    {
239
    float STROKE = 0.044f*TEXTURE_HEIGHT;
240
    float OFF = STROKE/2 -1;
241
    float OFF2 = 0.5f*TEXTURE_HEIGHT + OFF;
242
    float HEIGHT = TEXTURE_HEIGHT - OFF;
243
    float RADIUS = TEXTURE_HEIGHT/12.0f;
244
    float ARC1_H = 0.2f*TEXTURE_HEIGHT;
245
    float ARC1_W = TEXTURE_HEIGHT*0.5f;
246
    float ARC2_W = 0.153f*TEXTURE_HEIGHT;
247
    float ARC2_H = 0.905f*TEXTURE_HEIGHT;
248
    float ARC3_W = TEXTURE_HEIGHT-ARC2_W;
249

  
250
    float M = SQ3/2;
251
    float D = (M/2 - 0.51f)*TEXTURE_HEIGHT;
252

  
253
    paint.setAntiAlias(true);
254
    paint.setStrokeWidth(STROKE);
255
    paint.setColor(FACE_COLORS[face]);
256
    paint.setStyle(Paint.Style.FILL);
257

  
258
    canvas.drawRect(left,0,left+TEXTURE_HEIGHT,TEXTURE_HEIGHT,paint);
259

  
260
    paint.setColor(INTERIOR_COLOR);
261
    paint.setStyle(Paint.Style.STROKE);
262

  
263
    canvas.drawLine(                     left, M*HEIGHT        +D,  TEXTURE_HEIGHT       +left, M*HEIGHT+D, paint);
264
    canvas.drawLine(                OFF +left, M*TEXTURE_HEIGHT+D,                 OFF2  +left,          D, paint);
265
    canvas.drawLine((TEXTURE_HEIGHT-OFF)+left, M*TEXTURE_HEIGHT+D, (TEXTURE_HEIGHT-OFF2) +left,          D, paint);
266

  
267
    canvas.drawArc( ARC1_W-RADIUS+left, M*(ARC1_H-RADIUS)+D, ARC1_W+RADIUS+left, M*(ARC1_H+RADIUS)+D, 225, 90, false, paint);
268
    canvas.drawArc( ARC2_W-RADIUS+left, M*(ARC2_H-RADIUS)+D, ARC2_W+RADIUS+left, M*(ARC2_H+RADIUS)+D, 105, 90, false, paint);
269
    canvas.drawArc( ARC3_W-RADIUS+left, M*(ARC2_H-RADIUS)+D, ARC3_W+RADIUS+left, M*(ARC2_H+RADIUS)+D, 345, 90, false, paint);
239
    float E = SQ3/2;
240
    float F =  0.5f;
241
    float R = 0.06f;
242
    float S = 0.08f;
243
    float[] vertices = { -F,-E/3, +F,-E/3, 0.0f,2*E/3};
244

  
245
    drawRoundedPolygon(canvas, paint, left, vertices, S, FACE_COLORS[face], R);
270 246
    }
271 247

  
272 248
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/objects/TwistySkewb.java
253 253

  
254 254
    if( face<COLORS )
255 255
      {
256
      float STROKE = 0.035f*TEXTURE_HEIGHT;
257
      float L= left+0.125f*TEXTURE_HEIGHT;
258
      float H= 0.375f*TEXTURE_HEIGHT;
259
      float LEN = 0.5f*TEXTURE_HEIGHT;
260

  
261
      paint.setAntiAlias(true);
262
      paint.setStrokeWidth(STROKE);
263
      paint.setColor(FACE_COLORS[face]);
264
      paint.setStyle(Paint.Style.FILL);
265

  
266
      canvas.drawRect(left,0,left+TEXTURE_HEIGHT,TEXTURE_HEIGHT,paint);
267

  
268
      paint.setColor(INTERIOR_COLOR);
269
      paint.setStyle(Paint.Style.STROKE);
270

  
271
      canvas.drawLine( L    , H,  L+LEN, H    , paint);
272
      canvas.drawLine( L    , H,  L+LEN, H+LEN, paint);
273
      canvas.drawLine( L+LEN, H,  L+LEN, H+LEN, paint);
274

  
275
      float S1 = 0.125f*TEXTURE_HEIGHT;
276
      float S2 = 0.070f*TEXTURE_HEIGHT;
277
      float X  = 0.7f*S2;
278

  
279
      float LA = left+0.625f*TEXTURE_HEIGHT;
280
      float RA = left+0.125f*TEXTURE_HEIGHT;
281
      float TA = 0.375f*TEXTURE_HEIGHT;
282
      float BA = 0.875f*TEXTURE_HEIGHT;
283

  
284
      canvas.drawArc( LA-S1, TA     , LA     , TA+S1, 270, 90, false, paint);
285
      canvas.drawArc( RA+X , TA     , RA+X+S2, TA+S2, 135,135, false, paint);
286
      canvas.drawArc( LA-S2, BA-X-S2, LA     , BA-X ,   0,135, false, paint);
256
      float R = 0.025f;
257
      float S = 0.035f;
258
      float E = 0.5f;
259
      float[] vertices = { -E+E/4,E/4, E/4,-E+E/4, E/4,E/4};
260
      drawRoundedPolygon(canvas, paint, left, vertices, S, FACE_COLORS[face], R);
287 261
      }
288 262
    else
289 263
      {
290
      final float R = (SQ2/2)*TEXTURE_HEIGHT*0.10f;
291
      final float M = TEXTURE_HEIGHT*(0.5f-SQ2/4+0.018f);
292

  
293
      paint.setColor(FACE_COLORS[face-COLORS]);
294
      paint.setStyle(Paint.Style.FILL);
295
      canvas.drawRoundRect( left+M, M, left+TEXTURE_HEIGHT-M, TEXTURE_HEIGHT-M, R, R, paint);
264
      float R = 0.055f;
265
      float S = 0.035f;
266
      float E = SQ2/4;
267
      float[] vertices = { -E,-E, +E,-E, +E,+E, -E,+E };
268
      drawRoundedPolygon(canvas, paint, left, vertices, S, FACE_COLORS[face-COLORS], R);
296 269
      }
297 270
    }
298 271

  

Also available in: Unified diff