Project

General

Profile

« Previous | Next » 

Revision 9ce78850

Added by Leszek Koltunski over 3 years ago

Generalize FactorySticker - make it possible to draw stickers whose sides are curved.
Convert the Ivy to use this new functionality.

View differences:

src/main/java/org/distorted/helpers/FactorySticker.java
52 52
    REX_S = (float)(1/Math.sqrt(1+4*G*G/(F*F)));
53 53
    }
54 54

  
55
  private float mOX, mOY, mR;
56

  
55 57
///////////////////////////////////////////////////////////////////////////////////////////////////
56 58

  
57 59
  private FactorySticker()
......
81 83
    return ret;
82 84
    }
83 85

  
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

  
88
  private float getAngle(float[] angles, int index)
89
    {
90
    return angles==null ? 0 : angles[index];
91
    }
92

  
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94

  
95
  private void computeCircleCoords(float lX,float lY, float rX, float rY, float alpha)
96
    {
97
    float ctg= 1.0f/((float)Math.tan(alpha));
98
    mOX = 0.5f*(lX+rX) + ctg*0.5f*(lY-rY);
99
    mOY = 0.5f*(lY+rY) - ctg*0.5f*(lX-rX);
100
    float dx = mOX-lX;
101
    float dy = mOY-lY;
102
    mR = (float)Math.sqrt(dx*dx+dy*dy);
103
    }
104

  
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
// circle1: center (x1,y1) radius r1; circle2: center (x2,y2) radius r2.
107
// Guaranteed to intersect in two points. Find the intersection. Which one? the one that's closer
108
// to (nearx,neary).
109

  
110
  private void findCircleIntersection(float x1,float y1, float r1, float x2, float y2, float r2, float nearx, float neary )
111
    {
112
    float dx = x2-x1;
113
    float dy = y2-y1;
114
    float d = (float)Math.sqrt(dx*dx+dy*dy);
115

  
116
    if( d>0 )
117
      {
118
      float Dx = dx/d;
119
      float Dy = dy/d;
120
      float cos = (r1*r1+d*d-r2*r2)/(2*r1*d);
121
      float sin = (float)Math.sqrt(1-cos*cos);
122

  
123
      float ox1 = x1 + r1*cos*Dx + r1*sin*Dy;
124
      float oy1 = y1 + r1*cos*Dy - r1*sin*Dx;
125
      float ox2 = x1 + r1*cos*Dx - r1*sin*Dy;
126
      float oy2 = y1 + r1*cos*Dy + r1*sin*Dx;
127

  
128
      dx = nearx-ox1;
129
      dy = neary-oy1;
130
      float d1 = dx*dx+dy*dy;
131
      dx = nearx-ox2;
132
      dy = neary-oy2;
133
      float d2 = dx*dx+dy*dy;
134

  
135
      if( d1<d2 )
136
        {
137
        mOX = ox1;
138
        mOY = oy1;
139
        }
140
      else
141
        {
142
        mOX = ox2;
143
        mOY = oy2;
144
        }
145
      }
146
    else
147
      {
148
      mOX = x1;
149
      mOY = y1;
150
      }
151
    }
152

  
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

  
155
  private void drawCurrCurvVertex(Canvas canvas, Paint paint, int left, int top, float r, float stroke, float pX, float pY, float cX, float cY, float nX, float nY, float pA, float cA)
156
    {
157
    pX = (0.5f+pX)*TEXTURE_HEIGHT;
158
    pY = (0.5f-pY)*TEXTURE_HEIGHT;
159
    cX = (0.5f+cX)*TEXTURE_HEIGHT;
160
    cY = (0.5f-cY)*TEXTURE_HEIGHT;
161
    nX = (0.5f+nX)*TEXTURE_HEIGHT;
162
    nY = (0.5f-nY)*TEXTURE_HEIGHT;
163

  
164
    computeCircleCoords(pX,pY,cX,cY,pA);
165
    float o1x = mOX;
166
    float o1y = mOY;
167
    float r1  = mR;
168
    computeCircleCoords(cX,cY,nX,nY,cA);
169
    float o2x = mOX;
170
    float o2y = mOY;
171
    float r2  = mR;
172

  
173
    float dx = o1x-pX;
174
    float dy = o1y-pY;
175
    float startA = computeAngle(dy,dx);
176
    float sweepA = 2*pA;
177

  
178
    startA *= 180/(Math.PI);
179
    sweepA *= 180/(Math.PI);
180

  
181

  
182

  
183
android.util.Log.e("D", "o1x="+o1x+" o1y="+o1y+" r1="+r1+" stA="+startA+" swA="+sweepA);
184

  
185

  
186
    canvas.drawArc( left+o1x-r1, top+o1y-r1, left+o1x+r1, top+o1y+r1, startA, sweepA, false, paint);
187

  
188
    float r3  = r*TEXTURE_HEIGHT + stroke/2;
189
    findCircleIntersection(o1x,o1y,r1-r3,o2x,o2y,r2-r3,cX,cY);
190
    float o3x = mOX;
191
    float o3y = mOY;
192

  
193
    dx = o1x-o3x;
194
    dy = o1y-o3y;
195
    startA = computeAngle(dy,dx);
196
    dx = o2x-o3x;
197
    dy = o2y-o3y;
198
    float endA = computeAngle(dy,dx);
199

  
200
    sweepA = endA-startA;
201
    if( sweepA<0 ) sweepA += 2*Math.PI;
202

  
203
    startA *= 180/(Math.PI);
204
    sweepA *= 180/(Math.PI);
205

  
206
    canvas.drawArc( left+o3x-r3, top+o3y-r3, left+o3x+r3, top+o3y+r3, startA, sweepA, false, paint);
207
    }
208

  
84 209
///////////////////////////////////////////////////////////////////////////////////////////////////
85 210

  
86 211
  private void drawCurrVertex(Canvas canvas, Paint paint, int left, int top, float r, float stroke, float pX, float pY, float cX, float cY, float nX, float nY)
......
149 274
///////////////////////////////////////////////////////////////////////////////////////////////////
150 275
// PUBLIC
151 276

  
152
  public void drawRoundedPolygon(Canvas canvas, Paint paint, int left, int top, float[] vertices, float stroke, int color, float radius)
277
  public void drawRoundedPolygon(Canvas canvas, Paint paint, int left, int top, float[] vertices, float[] angles, float stroke, int color, float radius)
153 278
    {
154 279
    stroke *= TEXTURE_HEIGHT;
155 280

  
......
173 298
    float nextX = vertices[2];
174 299
    float nextY = vertices[3];
175 300

  
301
    float prevA = getAngle(angles,numVertices-1);
302
    float currA = getAngle(angles,0);
303

  
176 304
    for(int vert=0; vert<numVertices; vert++)
177 305
      {
178
      drawCurrVertex(canvas, paint, left, top, radius, stroke, prevX,prevY,currX,currY,nextX,nextY);
306
      if( prevA==0 )
307
        {
308
        drawCurrVertex(canvas, paint, left, top, radius, stroke, prevX,prevY,currX,currY,nextX,nextY);
309
        }
310
      else
311
        {
312
        drawCurrCurvVertex(canvas, paint, left, top, radius, stroke, prevX,prevY,currX,currY,nextX,nextY,prevA,currA);
313
        }
179 314

  
180 315
      prevX = currX;
181 316
      prevY = currY;
182 317
      currX = nextX;
183 318
      currY = nextY;
184 319

  
320
      prevA = currA;
321
      currA = getAngle(angles, vert==numVertices-1 ? 0 : vert+1);
322

  
185 323
      if( 2*(vert+2)+1 < length )
186 324
        {
187 325
        nextX = vertices[2*(vert+2)  ];
......
195 333
      }
196 334
    }
197 335

  
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

  
200
  public void drawIvyCornerSticker(Canvas canvas, Paint paint, int left, int top, int color, float stroke, float radius)
201
    {
202
    paint.setAntiAlias(true);
203
    paint.setColor(color);
204
    paint.setStyle(Paint.Style.FILL);
205
    canvas.drawRect(left,top,left+TEXTURE_HEIGHT,top+TEXTURE_HEIGHT,paint);
206

  
207
    paint.setColor(COLOR_BLACK);
208
    paint.setStyle(Paint.Style.STROKE);
209
    paint.setStrokeWidth(IVY_C*stroke*TEXTURE_HEIGHT);
210

  
211
    float tmp1 = ((IVY_D-0.5f)-IVY_M)*IVY_C;
212
    float cx1 = TEXTURE_HEIGHT*(tmp1 + 0.5f);
213
    float cy1 = TEXTURE_HEIGHT*(0.5f - tmp1);
214

  
215
    float halfL1 = IVY_C*TEXTURE_HEIGHT*(1.0f-2*IVY_D);
216

  
217
    canvas.drawArc( left+cx1-halfL1, top+cy1-halfL1, left+cx1+halfL1, top+cy1+halfL1, 270, 90, false, paint);
218

  
219
    float tmp2 = (+0.5f-IVY_M)*IVY_C;
220
    float tmp3 = (-0.5f-IVY_M)*IVY_C;
221

  
222
    float x0 = TEXTURE_HEIGHT*(+tmp2 + 0.5f);
223
    float y0 = TEXTURE_HEIGHT*(-tmp3 + 0.5f);
224
    float x1 = TEXTURE_HEIGHT*(+tmp2 + 0.5f);
225
    float y1 = TEXTURE_HEIGHT*(-tmp2 + 0.5f);
226
    float x2 = TEXTURE_HEIGHT*(+tmp3 + 0.5f);
227
    float y2 = TEXTURE_HEIGHT*(-tmp2 + 0.5f);
228

  
229
    canvas.drawLine(left+x0,top+y0,left+x1,top+y1,paint);
230
    canvas.drawLine(left+x1,top+y1,left+x2,top+y2,paint);
231

  
232
    float tmp4 = ((0.5f-stroke/2-radius/2)-IVY_M)*IVY_C;
233
    float cx2 = TEXTURE_HEIGHT*(tmp4 + 0.5f);
234
    float cy2 = TEXTURE_HEIGHT*(0.5f - tmp4);
235

  
236
    float halfL2 = IVY_C*TEXTURE_HEIGHT*radius;
237

  
238
    paint.setStrokeWidth(IVY_C*radius*TEXTURE_HEIGHT);
239
    canvas.drawArc( left+cx2-halfL2, top+cy2-halfL2, left+cx2+halfL2, top+cy2+halfL2, 270, 90, false, paint);
240
    }
241

  
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

  
244
  public void drawIvyCenterSticker(Canvas canvas, Paint paint, int left, int top, int color, float stroke, float radius)
245
    {
246
    paint.setAntiAlias(true);
247
    paint.setColor(color);
248
    paint.setStyle(Paint.Style.FILL);
249
    canvas.drawRect(left,top,left+TEXTURE_HEIGHT,top+TEXTURE_HEIGHT,paint);
250

  
251
    paint.setColor(COLOR_BLACK);
252
    paint.setStyle(Paint.Style.STROKE);
253
    paint.setStrokeWidth(stroke*TEXTURE_HEIGHT);
254

  
255
    float cx1 = TEXTURE_HEIGHT*IVY_D;
256
    float cy1 = TEXTURE_HEIGHT*(1-IVY_D);
257
    float cx2 = TEXTURE_HEIGHT*(1.0f-IVY_D);
258
    float cy2 = TEXTURE_HEIGHT*IVY_D;
259

  
260
    float halfL = TEXTURE_HEIGHT*(1.0f - 2*IVY_D);
261

  
262
    canvas.drawArc( left+cx1-halfL, top+cy1-halfL, left+cx1+halfL, top+cy1+halfL, 270, 90, false, paint);
263
    canvas.drawArc( left+cx2-halfL, top+cy2-halfL, left+cx2+halfL, top+cy2+halfL,  90, 90, false, paint);
264

  
265
    float tmp = TEXTURE_HEIGHT*(IVY_D+stroke*0.5f+radius*0.5f);
266
    float cx3 = tmp;
267
    float cy3 = tmp;
268
    float cx4 = TEXTURE_HEIGHT - cx3;
269
    float cy4 = TEXTURE_HEIGHT - cy3;
270
    float halfR = TEXTURE_HEIGHT*radius;
271

  
272
    paint.setStrokeWidth(radius*TEXTURE_HEIGHT);
273
    canvas.drawArc( left+cx3-halfR, top+cy3-halfR, left+cx3+halfR, top+cy3+halfR, 180, 90, false, paint);
274
    canvas.drawArc( left+cx4-halfR, top+cy4-halfR, left+cx4+halfR, top+cy4+halfR,   0, 90, false, paint);
275
    }
276

  
277 336
///////////////////////////////////////////////////////////////////////////////////////////////////
278 337

  
279 338
  public void drawRexCornerSticker(Canvas canvas, Paint paint, int left, int top, int color, float stroke, float radius1, float radius2)
src/main/java/org/distorted/objects/TwistyBandagedAbstract.java
384 384
    float[] vertices = { -X,-Y, +X,-Y, +X,+Y, -X,+Y};
385 385

  
386 386
    FactorySticker factory = FactorySticker.getInstance();
387
    factory.drawRoundedPolygon(canvas, paint, left, top, vertices, S, FACE_COLORS[color], R);
387
    factory.drawRoundedPolygon(canvas, paint, left, top, vertices, null, S, FACE_COLORS[color], R);
388 388
    }
389 389

  
390 390
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/objects/TwistyCube.java
185 185
    float S = 0.08f;
186 186

  
187 187
    FactorySticker factory = FactorySticker.getInstance();
188
    factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[0], S, FACE_COLORS[face], R);
188
    factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[0], null, S, FACE_COLORS[face], R);
189 189
    }
190 190

  
191 191
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/objects/TwistyDiamond.java
497 497
    float S = 0.07f;
498 498

  
499 499
    FactorySticker factory = FactorySticker.getInstance();
500
    factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[0], S, FACE_COLORS[face], R);
500
    factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[0], null, S, FACE_COLORS[face], R);
501 501
    }
502 502

  
503 503
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/objects/TwistyDino.java
219 219
    float S = 0.05f;
220 220

  
221 221
    FactorySticker factory = FactorySticker.getInstance();
222
    factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[0], S, FACE_COLORS[face], R);
222
    factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[0], null, S, FACE_COLORS[face], R);
223 223
    }
224 224

  
225 225
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/objects/TwistyHelicopter.java
381 381
    float S = 0.05f;
382 382

  
383 383
    FactorySticker factory = FactorySticker.getInstance();
384
    factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[0], S, FACE_COLORS[face], R);
384
    factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[0], null, S, FACE_COLORS[face], R);
385 385
    }
386 386

  
387 387
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/objects/TwistyIvy.java
396 396
    {
397 397
    int COLORS = FACE_COLORS.length;
398 398
    FactorySticker factory = FactorySticker.getInstance();
399
    float S = 0.08f;
400
    float R = 0.12f;
401 399

  
402 400
    if( face<COLORS )
403 401
      {
404
      factory.drawIvyCornerSticker(canvas, paint, left, top, FACE_COLORS[face%COLORS], S, R);
402
      float S = 0.03f;
403
      float R = 0.02f;
404

  
405
      float ANGLE = (float)(Math.PI/4);
406

  
407
      float A = (+0.5f-IVY_M)*IVY_C;
408
      float B = (-0.5f-IVY_M)*IVY_C;
409

  
410
      float[] vertices = new float[] { A, B, A, A, B, A };
411
      float[] angles   = new float[] { 0, 0, ANGLE };
412

  
413
      factory.drawRoundedPolygon(canvas, paint, left, top, vertices, angles, S, FACE_COLORS[face%COLORS], R);
405 414
      }
406 415
    else
407 416
      {
408
      factory.drawIvyCenterSticker(canvas, paint, left, top, FACE_COLORS[face%COLORS], S, R);
417
      float S = 0.08f;
418
      float R = 0.06f;
419

  
420
      float ANGLE = (float)(Math.PI/4);
421
      float A = 0.50f-IVY_D;
422

  
423
      float[] vertices = new float[] { -A, A, A, -A };
424
      float[] angles   = new float[] { ANGLE, ANGLE };
425

  
426
      factory.drawRoundedPolygon(canvas, paint, left, top, vertices, angles, S, FACE_COLORS[face%COLORS], R);
409 427
      }
410 428
    }
411 429

  
src/main/java/org/distorted/objects/TwistyJing.java
384 384
      }
385 385

  
386 386
    FactorySticker factory = FactorySticker.getInstance();
387
    factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[stickerType], S, FACE_COLORS[face%COLORS], R);
387
    factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[stickerType], null, S, FACE_COLORS[face%COLORS], R);
388 388
    }
389 389

  
390 390
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/objects/TwistyKilominx.java
646 646
      float cy = STICKERS[0][3]/CENTER_CORR;
647 647

  
648 648
      FactorySticker factory = FactorySticker.getInstance();
649
      factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[0], S, FACE_COLORS[face%NUM_FACES], R);
649
      factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[0], null, S, FACE_COLORS[face%NUM_FACES], R);
650 650
      canvas.drawCircle(left+(0.5f+cx)*TEXTURE_HEIGHT, top+(0.5f-cy)*TEXTURE_HEIGHT, 0.05f*TEXTURE_HEIGHT, paint);
651 651
      }
652 652
    else if( variant==0 ) // corner
......
654 654
      float R = 0.10f;
655 655
      float S = 0.11f;
656 656
      FactorySticker factory = FactorySticker.getInstance();
657
      factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[1], S, FACE_COLORS[face%COLORS], R);
657
      factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[1], null, S, FACE_COLORS[face%COLORS], R);
658 658
      }
659 659
    else  // edge
660 660
      {
661 661
      float R = 0.10f;
662 662
      float S = 0.10f;
663 663
      FactorySticker factory = FactorySticker.getInstance();
664
      factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[2], S, FACE_COLORS[face%COLORS], R);
664
      factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[2], null, S, FACE_COLORS[face%COLORS], R);
665 665
      }
666 666
    }
667 667

  
src/main/java/org/distorted/objects/TwistyMegaminx.java
555 555
      }
556 556

  
557 557
    FactorySticker factory = FactorySticker.getInstance();
558
    factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[index], S, FACE_COLORS[face%COLORS], R);
558
    factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[index], null, S, FACE_COLORS[face%COLORS], R);
559 559
    }
560 560

  
561 561
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/objects/TwistyPyraminx.java
364 364
    float S = 0.08f;
365 365

  
366 366
    FactorySticker factory = FactorySticker.getInstance();
367
    factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[0], S, FACE_COLORS[face], R);
367
    factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[0], null, S, FACE_COLORS[face], R);
368 368
    }
369 369

  
370 370
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/objects/TwistyRedi.java
386 386
      }
387 387

  
388 388
    FactorySticker factory = FactorySticker.getInstance();
389
    factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[stickerType], S, FACE_COLORS[face%COLORS], R);
389
    factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[stickerType], null, S, FACE_COLORS[face%COLORS], R);
390 390
    }
391 391

  
392 392
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/objects/TwistyRex.java
567 567
    else if( face<2*COLORS )
568 568
      {
569 569
      float[] vertices = { -REX_D,0.0f, 0.0f, -REX_D, +REX_D, 0.0f, 0.0f, +REX_D};
570
      factory.drawRoundedPolygon(canvas, paint, left, top, vertices, S1, FACE_COLORS[face%COLORS], R4);
570
      factory.drawRoundedPolygon(canvas, paint, left, top, vertices, null, S1, FACE_COLORS[face%COLORS], R4);
571 571
      }
572 572
    else
573 573
      {
src/main/java/org/distorted/objects/TwistySkewb.java
586 586
      }
587 587

  
588 588
    FactorySticker factory = FactorySticker.getInstance();
589
    factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[index], S, FACE_COLORS[face%COLORS], R);
589
    factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[index], null, S, FACE_COLORS[face%COLORS], R);
590 590
    }
591 591

  
592 592
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/objects/TwistySquare1.java
239 239
      }
240 240

  
241 241
    FactorySticker factory = FactorySticker.getInstance();
242
    factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[stickerType], S, FACE_COLORS[face%COLORS], R);
242
    factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[stickerType], null, S, FACE_COLORS[face%COLORS], R);
243 243
    }
244 244

  
245 245
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/objects/TwistySquare2.java
244 244
      }
245 245

  
246 246
    FactorySticker factory = FactorySticker.getInstance();
247
    factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[stickerType], S, FACE_COLORS[face%COLORS], R);
247
    factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[stickerType], null, S, FACE_COLORS[face%COLORS], R);
248 248
    }
249 249

  
250 250
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/objects/TwistyUltimate.java
357 357
      }
358 358

  
359 359
    FactorySticker factory = FactorySticker.getInstance();
360
    factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[stickerType], S, FACE_COLORS[face%COLORS], R);
360
    factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[stickerType], null, S, FACE_COLORS[face%COLORS], R);
361 361
    }
362 362

  
363 363
///////////////////////////////////////////////////////////////////////////////////////////////////

Also available in: Unified diff