Project

General

Profile

Download (13.4 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / helpers / FactorySticker.java @ 7aa4c349

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.helpers;
21

    
22
import android.graphics.Canvas;
23
import android.graphics.Paint;
24

    
25
import static org.distorted.objects.TwistyObject.TEXTURE_HEIGHT;
26
import static org.distorted.objects.TwistyObject.COLOR_BLACK;
27
import static org.distorted.objects.TwistyIvy.IVY_D;
28
import static org.distorted.objects.TwistyIvy.IVY_C;
29
import static org.distorted.objects.TwistyIvy.IVY_M;
30
import static org.distorted.objects.TwistyRex.REX_D;
31

    
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33

    
34
public class FactorySticker
35
  {
36
  private static final float SQ2 = (float)Math.sqrt(2);
37
  private static final float REX_X,REX_R,REX_B,REX_A, REX_P, REX_T, REX_C, REX_S;
38
  private static FactorySticker mThis;
39

    
40
  static
41
    {
42
    float F = REX_D*SQ2;
43
    float G = (1-REX_D)*SQ2/2;
44

    
45
    REX_X = (0.5f-REX_D*REX_D)/(2*REX_D);
46
    REX_R = (float)Math.sqrt(2*REX_X*REX_X+0.5f);
47
    REX_B = (float) ((180/Math.PI)*(2*Math.asin( Math.sqrt(REX_D*REX_D-REX_D+0.5f) / (2*REX_R) )));
48
    REX_A = (float) ((180/Math.PI)*Math.acos(REX_X/REX_R)) - 45;
49
    REX_P = 45 + REX_B/2 + REX_A;
50
    REX_T = (float) ( Math.tan( (Math.PI/180)*(45-REX_P/2) ) );
51
    REX_C = (float)(REX_R/Math.cos((Math.PI/180)*REX_B/2) );
52
    REX_S = (float)(1/Math.sqrt(1+4*G*G/(F*F)));
53
    }
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
  private FactorySticker()
58
    {
59

    
60
    }
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
  public static FactorySticker getInstance()
65
    {
66
    if( mThis==null ) mThis = new FactorySticker();
67

    
68
    return mThis;
69
    }
70

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

    
73
  private float computeAngle(float dx, float dy)
74
    {
75
    float PI = (float)Math.PI;
76
    double angle = Math.atan2(dy,dx);
77
    float ret = (float)(3*PI/2-angle);
78

    
79
    if( ret>2*PI ) ret-= 2*PI;
80

    
81
    return ret;
82
    }
83

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

    
86
  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)
87
    {
88
    pX = (0.5f+pX)*TEXTURE_HEIGHT;
89
    pY = (0.5f-pY)*TEXTURE_HEIGHT;
90
    cX = (0.5f+cX)*TEXTURE_HEIGHT;
91
    cY = (0.5f-cY)*TEXTURE_HEIGHT;
92
    nX = (0.5f+nX)*TEXTURE_HEIGHT;
93
    nY = (0.5f-nY)*TEXTURE_HEIGHT;
94

    
95
    canvas.drawLine(left+pX,top+pY,left+cX,top+cY,paint);
96

    
97
    float aX = pX-cX;
98
    float aY = pY-cY;
99
    float bX = cX-nX;
100
    float bY = cY-nY;
101

    
102
    float aLen = (float)Math.sqrt(aX*aX+aY*aY);
103
    float bLen = (float)Math.sqrt(bX*bX+bY*bY);
104

    
105
    aX /= aLen;
106
    aY /= aLen;
107
    bX /= bLen;
108
    bY /= bLen;
109

    
110
    float sX = (aX-bX)/2;
111
    float sY = (aY-bY)/2;
112
    float sLen = (float)Math.sqrt(sX*sX+sY*sY);
113
    sX /= sLen;
114
    sY /= sLen;
115

    
116
    float startAngle = computeAngle(bX,-bY);
117
    float endAngle   = computeAngle(aX,-aY);
118
    float sweepAngle = endAngle-startAngle;
119
    if( sweepAngle<0 ) sweepAngle += 2*Math.PI;
120

    
121
    float R = r*TEXTURE_HEIGHT+stroke/2;
122
    float C = (float)Math.cos(sweepAngle/2);
123
    float A = R/C;
124

    
125
    left += (cX+A*sX);
126
    top  += (cY+A*sY);
127

    
128
    if( C< (2*R-stroke)/(2*R+stroke) )
129
      {
130
      float alpha = startAngle + sweepAngle/2;
131
      float B  = (R-stroke/2)/C;
132
      float sx = (float)Math.cos(alpha);
133
      float sy = (float)Math.sin(alpha);
134

    
135
      float startX = left + R*sx;
136
      float startY = top  + R*sy;
137
      float stopX  = left + B*sx;
138
      float stopY  = top  + B*sy;
139

    
140
      canvas.drawLine(startX,startY,stopX,stopY,paint);
141
      }
142

    
143
    startAngle *= 180/(Math.PI);
144
    sweepAngle *= 180/(Math.PI);
145

    
146
    canvas.drawArc( left-R, top-R, left+R, top+R, startAngle, sweepAngle, false, paint);
147
    }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
// PUBLIC
151

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

    
156
    paint.setAntiAlias(true);
157
    paint.setStrokeWidth(stroke);
158
    paint.setColor(color);
159
    paint.setStyle(Paint.Style.FILL);
160

    
161
    canvas.drawRect(left,top,left+TEXTURE_HEIGHT,top+TEXTURE_HEIGHT,paint);
162

    
163
    paint.setColor(COLOR_BLACK);
164
    paint.setStyle(Paint.Style.STROKE);
165

    
166
    int length = vertices.length;
167
    int numVertices = length/2;
168

    
169
    float prevX = vertices[length-2];
170
    float prevY = vertices[length-1];
171
    float currX = vertices[0];
172
    float currY = vertices[1];
173
    float nextX = vertices[2];
174
    float nextY = vertices[3];
175

    
176
    for(int vert=0; vert<numVertices; vert++)
177
      {
178
      drawCurrVertex(canvas, paint, left, top, radius, stroke, prevX,prevY,currX,currY,nextX,nextY);
179

    
180
      prevX = currX;
181
      prevY = currY;
182
      currX = nextX;
183
      currY = nextY;
184

    
185
      if( 2*(vert+2)+1 < length )
186
        {
187
        nextX = vertices[2*(vert+2)  ];
188
        nextY = vertices[2*(vert+2)+1];
189
        }
190
      else
191
        {
192
        nextX = vertices[0];
193
        nextY = vertices[1];
194
        }
195
      }
196
    }
197

    
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
///////////////////////////////////////////////////////////////////////////////////////////////////
278

    
279
  public void drawRexCornerSticker(Canvas canvas, Paint paint, int left, int top, int color, float stroke, float radius1, float radius2)
280
    {
281
    paint.setColor(color);
282
    paint.setStyle(Paint.Style.FILL);
283
    canvas.drawRect(left,top,left+TEXTURE_HEIGHT,top+TEXTURE_HEIGHT,paint);
284

    
285
    paint.setColor(COLOR_BLACK);
286
    paint.setStyle(Paint.Style.STROKE);
287
    paint.setStrokeWidth(stroke*TEXTURE_HEIGHT);
288

    
289
    float F = REX_D*SQ2;
290
    float G = (1-REX_D)*SQ2/2;
291

    
292
    float cx1 = left + (0.5f-F/2)*TEXTURE_HEIGHT;
293
    float cx2 = left + (0.5f+F/2)*TEXTURE_HEIGHT;
294
    float cy  = top  + (0.5f+G/3)*TEXTURE_HEIGHT;
295

    
296
    canvas.drawLine(cx1, cy, cx2, cy, paint);
297

    
298
    float X   = REX_C-F/2;
299
    float R1  = TEXTURE_HEIGHT*(REX_R + 0.5f*stroke);
300
    float cx3 = left + (0.5f-X)*TEXTURE_HEIGHT;
301
    float cx4 = left + (0.5f+X)*TEXTURE_HEIGHT;
302

    
303
    canvas.drawArc( cx3-R1, cy-R1, cx3+R1, cy+R1, 360-REX_B/2, REX_B/2, false ,paint);
304
    canvas.drawArc( cx4-R1, cy-R1, cx4+R1, cy+R1, 180        , REX_B/2, false ,paint);
305

    
306
    float cx5 = left + (0.5f+F/2-radius2)*TEXTURE_HEIGHT;
307
    float cx6 = left + (0.5f-F/2+radius2)*TEXTURE_HEIGHT;
308
    float cy1 = top  + (0.5f+G/3-radius2)*TEXTURE_HEIGHT;
309
    float R2  = TEXTURE_HEIGHT*radius2;
310

    
311
    canvas.drawArc( cx5-R2, cy1-R2, cx5+R2, cy1+R2,  0, 90, false ,paint);
312
    canvas.drawArc( cx6-R2, cy1-R2, cx6+R2, cy1+R2, 90, 90, false ,paint);
313

    
314
    float cx7 = left + 0.5f*TEXTURE_HEIGHT;
315
    float cy2 = top  + (0.5f-2*G/3 + radius1/REX_S)*TEXTURE_HEIGHT;
316
    float R3  = TEXTURE_HEIGHT*(radius1 + 0.5f*stroke);
317
    canvas.drawArc( cx7-R3, cy2-R3, cx7+R3, cy2+R3, 270-(90-REX_B/2), 180-REX_B, false ,paint);
318
    }
319

    
320
///////////////////////////////////////////////////////////////////////////////////////////////////
321

    
322
  public void drawRexEdgeSticker(Canvas canvas, Paint paint, int left, int top, int color, float stroke, float radius)
323
    {
324
    paint.setColor(color);
325
    paint.setStyle(Paint.Style.FILL);
326
    canvas.drawRect(left,top,left+TEXTURE_HEIGHT,top+TEXTURE_HEIGHT,paint);
327

    
328
    paint.setColor(COLOR_BLACK);
329
    paint.setStyle(Paint.Style.STROKE);
330
    paint.setStrokeWidth(stroke*TEXTURE_HEIGHT);
331

    
332
    final float D = ( 0.5f - (0.5f-REX_D)/3);
333
    float cx1 = left+ TEXTURE_HEIGHT*(0.5f-REX_X);
334
    float cy1 = top + TEXTURE_HEIGHT*(0.5f+REX_X+D);
335
    float cx2 = left+ TEXTURE_HEIGHT*(0.5f+REX_X);
336
    float R1  = TEXTURE_HEIGHT*REX_R;
337

    
338
    canvas.drawArc( cx1-R1, cy1-R1, cx1+R1, cy1+R1, 315-REX_A-REX_B, REX_B, false ,paint);
339
    canvas.drawArc( cx2-R1, cy1-R1, cx2+R1, cy1+R1, 225+REX_A      , REX_B, false ,paint);
340

    
341
    float CORR_Y = radius/12;
342
    float CORR_A = 10;
343
    float sin = (float)Math.sin(Math.PI*REX_P/180);
344
    float cx = left + 0.5f*TEXTURE_HEIGHT;
345
    float cy = top  + ( 0.5f + 2*(0.5f-REX_D)/3 -CORR_Y -radius/sin )*TEXTURE_HEIGHT;
346
    float R2  = TEXTURE_HEIGHT*radius;
347

    
348
    canvas.drawArc( cx-R2, cy-R2, cx+R2, cy+R2, 90-(REX_P-CORR_A), 2*(REX_P-CORR_A), false ,paint);
349

    
350
    float F = 0.1f;
351
    float G = 0.6f;
352
    float R3  = TEXTURE_HEIGHT*radius*G*0.9f;
353
    float X = G*radius/REX_T;
354
    float cx4 = left + X*TEXTURE_HEIGHT;
355
    float cx3 = left + (1-X)*TEXTURE_HEIGHT;
356
    float cy3 = top + (0.5f - (0.5f-REX_D)/3 + G*radius - F*stroke)*TEXTURE_HEIGHT;
357

    
358
    canvas.drawArc( cx3-R3, cy3-R3, cx3+R3, cy3+R3, 270           , 90+REX_P, false ,paint);
359
    canvas.drawArc( cx4-R3, cy3-R3, cx4+R3, cy3+R3, 270-(90+REX_P), 90+REX_P, false ,paint);
360

    
361
    float cy5 = top + D*TEXTURE_HEIGHT;
362

    
363
    paint.setStrokeWidth((1-2*F)*stroke*TEXTURE_HEIGHT);
364
    canvas.drawLine(left, cy5, left+TEXTURE_HEIGHT, cy5, paint);
365
    }
366
  }
(5-5/12)