Project

General

Profile

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

magiccube / src / main / java / org / distorted / helpers / FactorySticker.java @ be56193c

1 b89898c5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 749ef882 Leszek Koltunski
package org.distorted.helpers;
21 b89898c5 Leszek Koltunski
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 be56193c Leszek Koltunski
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 b89898c5 Leszek Koltunski
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33
34 749ef882 Leszek Koltunski
public class FactorySticker
35 b89898c5 Leszek Koltunski
  {
36 dad33773 Leszek Koltunski
  private static final float SQ2 = (float)Math.sqrt(2);
37 da36b97e Leszek Koltunski
  private static final float REX_X,REX_R,REX_B,REX_A, REX_P, REX_T, REX_C, REX_S;
38 b89898c5 Leszek Koltunski
  private static FactorySticker mThis;
39
40 da36b97e Leszek Koltunski
  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 b89898c5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 ae755eda Leszek Koltunski
  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 b89898c5 Leszek Koltunski
    {
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 ae755eda Leszek Koltunski
    canvas.drawLine(left+pX,top+pY,left+cX,top+cY,paint);
96 b89898c5 Leszek Koltunski
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
123
    float A = (float)(R/(Math.cos(sweepAngle/2)));
124
125
    float rX = cX + A*sX;
126
    float rY = cY + A*sY;
127
128
    startAngle *= 180/(Math.PI);
129
    sweepAngle *= 180/(Math.PI);
130
131 ae755eda Leszek Koltunski
    canvas.drawArc( left+rX-R, top+rY-R, left+rX+R, top+rY+R, startAngle, sweepAngle, false, paint);
132 b89898c5 Leszek Koltunski
    }
133
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135 749ef882 Leszek Koltunski
// PUBLIC
136 b89898c5 Leszek Koltunski
137 749ef882 Leszek Koltunski
  public void drawRoundedPolygon(Canvas canvas, Paint paint, int left, int top, float[] vertices, float stroke, int color, float radius)
138 b89898c5 Leszek Koltunski
    {
139
    stroke *= TEXTURE_HEIGHT;
140
141
    paint.setAntiAlias(true);
142
    paint.setStrokeWidth(stroke);
143
    paint.setColor(color);
144
    paint.setStyle(Paint.Style.FILL);
145
146 ae755eda Leszek Koltunski
    canvas.drawRect(left,top,left+TEXTURE_HEIGHT,top+TEXTURE_HEIGHT,paint);
147 b89898c5 Leszek Koltunski
148
    paint.setColor(COLOR_BLACK);
149
    paint.setStyle(Paint.Style.STROKE);
150
151
    int length = vertices.length;
152
    int numVertices = length/2;
153
154
    float prevX = vertices[length-2];
155
    float prevY = vertices[length-1];
156
    float currX = vertices[0];
157
    float currY = vertices[1];
158
    float nextX = vertices[2];
159
    float nextY = vertices[3];
160
161
    for(int vert=0; vert<numVertices; vert++)
162
      {
163 c5338d5e Leszek Koltunski
      drawCurrVertex(canvas, paint, left, top, radius, stroke, prevX,prevY,currX,currY,nextX,nextY);
164 b89898c5 Leszek Koltunski
165
      prevX = currX;
166
      prevY = currY;
167
      currX = nextX;
168
      currY = nextY;
169
170
      if( 2*(vert+2)+1 < length )
171
        {
172
        nextX = vertices[2*(vert+2)  ];
173
        nextY = vertices[2*(vert+2)+1];
174
        }
175
      else
176
        {
177
        nextX = vertices[0];
178
        nextY = vertices[1];
179
        }
180
      }
181
    }
182 49cd8581 Leszek Koltunski
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184
185 749ef882 Leszek Koltunski
  public void drawIvyCornerSticker(Canvas canvas, Paint paint, int left, int top, int color, float stroke, float radius)
186 49cd8581 Leszek Koltunski
    {
187 9e5b990e Leszek Koltunski
    paint.setAntiAlias(true);
188 49cd8581 Leszek Koltunski
    paint.setColor(color);
189
    paint.setStyle(Paint.Style.FILL);
190
    canvas.drawRect(left,top,left+TEXTURE_HEIGHT,top+TEXTURE_HEIGHT,paint);
191
192 6a224bdc Leszek Koltunski
    paint.setColor(COLOR_BLACK);
193
    paint.setStyle(Paint.Style.STROKE);
194 886d1ebb Leszek Koltunski
    paint.setStrokeWidth(IVY_C*stroke*TEXTURE_HEIGHT);
195 9e5b990e Leszek Koltunski
196 886d1ebb Leszek Koltunski
    float tmp1 = ((IVY_D-0.5f)-IVY_M)*IVY_C;
197 9e5b990e Leszek Koltunski
    float cx1 = TEXTURE_HEIGHT*(tmp1 + 0.5f);
198
    float cy1 = TEXTURE_HEIGHT*(0.5f - tmp1);
199
200 886d1ebb Leszek Koltunski
    float halfL1 = IVY_C*TEXTURE_HEIGHT*(1.0f-2*IVY_D);
201 9e5b990e Leszek Koltunski
202
    canvas.drawArc( left+cx1-halfL1, top+cy1-halfL1, left+cx1+halfL1, top+cy1+halfL1, 270, 90, false, paint);
203
204 886d1ebb Leszek Koltunski
    float tmp2 = (+0.5f-IVY_M)*IVY_C;
205
    float tmp3 = (-0.5f-IVY_M)*IVY_C;
206 9e5b990e Leszek Koltunski
207
    float x0 = TEXTURE_HEIGHT*(+tmp2 + 0.5f);
208
    float y0 = TEXTURE_HEIGHT*(-tmp3 + 0.5f);
209
    float x1 = TEXTURE_HEIGHT*(+tmp2 + 0.5f);
210
    float y1 = TEXTURE_HEIGHT*(-tmp2 + 0.5f);
211
    float x2 = TEXTURE_HEIGHT*(+tmp3 + 0.5f);
212
    float y2 = TEXTURE_HEIGHT*(-tmp2 + 0.5f);
213
214
    canvas.drawLine(left+x0,top+y0,left+x1,top+y1,paint);
215
    canvas.drawLine(left+x1,top+y1,left+x2,top+y2,paint);
216
217 886d1ebb Leszek Koltunski
    float tmp4 = ((0.5f-stroke/2-radius/2)-IVY_M)*IVY_C;
218 9e5b990e Leszek Koltunski
    float cx2 = TEXTURE_HEIGHT*(tmp4 + 0.5f);
219
    float cy2 = TEXTURE_HEIGHT*(0.5f - tmp4);
220
221 886d1ebb Leszek Koltunski
    float halfL2 = IVY_C*TEXTURE_HEIGHT*radius;
222 9e5b990e Leszek Koltunski
223 886d1ebb Leszek Koltunski
    paint.setStrokeWidth(IVY_C*radius*TEXTURE_HEIGHT);
224 9e5b990e Leszek Koltunski
    canvas.drawArc( left+cx2-halfL2, top+cy2-halfL2, left+cx2+halfL2, top+cy2+halfL2, 270, 90, false, paint);
225 49cd8581 Leszek Koltunski
    }
226
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228
229 749ef882 Leszek Koltunski
  public void drawIvyCenterSticker(Canvas canvas, Paint paint, int left, int top, int color, float stroke, float radius)
230 49cd8581 Leszek Koltunski
    {
231 9e5b990e Leszek Koltunski
    paint.setAntiAlias(true);
232 49cd8581 Leszek Koltunski
    paint.setColor(color);
233
    paint.setStyle(Paint.Style.FILL);
234
    canvas.drawRect(left,top,left+TEXTURE_HEIGHT,top+TEXTURE_HEIGHT,paint);
235 6a224bdc Leszek Koltunski
236
    paint.setColor(COLOR_BLACK);
237
    paint.setStyle(Paint.Style.STROKE);
238
    paint.setStrokeWidth(stroke*TEXTURE_HEIGHT);
239
240 9e5b990e Leszek Koltunski
    float cx1 = TEXTURE_HEIGHT*IVY_D;
241
    float cy1 = TEXTURE_HEIGHT*(1-IVY_D);
242
    float cx2 = TEXTURE_HEIGHT*(1.0f-IVY_D);
243
    float cy2 = TEXTURE_HEIGHT*IVY_D;
244 6a224bdc Leszek Koltunski
245 9e5b990e Leszek Koltunski
    float halfL = TEXTURE_HEIGHT*(1.0f - 2*IVY_D);
246 6a224bdc Leszek Koltunski
247 9e5b990e Leszek Koltunski
    canvas.drawArc( left+cx1-halfL, top+cy1-halfL, left+cx1+halfL, top+cy1+halfL, 270, 90, false, paint);
248
    canvas.drawArc( left+cx2-halfL, top+cy2-halfL, left+cx2+halfL, top+cy2+halfL,  90, 90, false, paint);
249 886d1ebb Leszek Koltunski
250
    float tmp = TEXTURE_HEIGHT*(IVY_D+stroke*0.5f+radius*0.5f);
251
    float cx3 = tmp;
252
    float cy3 = tmp;
253
    float cx4 = TEXTURE_HEIGHT - cx3;
254
    float cy4 = TEXTURE_HEIGHT - cy3;
255
    float halfR = TEXTURE_HEIGHT*radius;
256
257
    paint.setStrokeWidth(radius*TEXTURE_HEIGHT);
258
    canvas.drawArc( left+cx3-halfR, top+cy3-halfR, left+cx3+halfR, top+cy3+halfR, 180, 90, false, paint);
259
    canvas.drawArc( left+cx4-halfR, top+cy4-halfR, left+cx4+halfR, top+cy4+halfR,   0, 90, false, paint);
260 49cd8581 Leszek Koltunski
    }
261 59b87d56 Leszek Koltunski
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263
264 749ef882 Leszek Koltunski
  public void drawRexCornerSticker(Canvas canvas, Paint paint, int left, int top, int color, float stroke, float radius1, float radius2)
265 59b87d56 Leszek Koltunski
    {
266
    paint.setColor(color);
267
    paint.setStyle(Paint.Style.FILL);
268
    canvas.drawRect(left,top,left+TEXTURE_HEIGHT,top+TEXTURE_HEIGHT,paint);
269 dad33773 Leszek Koltunski
270
    paint.setColor(COLOR_BLACK);
271
    paint.setStyle(Paint.Style.STROKE);
272
    paint.setStrokeWidth(stroke*TEXTURE_HEIGHT);
273
274 da36b97e Leszek Koltunski
    float F = REX_D*SQ2;
275
    float G = (1-REX_D)*SQ2/2;
276 59b87d56 Leszek Koltunski
277 da36b97e Leszek Koltunski
    float cx1 = left + (0.5f-F/2)*TEXTURE_HEIGHT;
278
    float cx2 = left + (0.5f+F/2)*TEXTURE_HEIGHT;
279
    float cy  = top  + (0.5f+G/3)*TEXTURE_HEIGHT;
280 59b87d56 Leszek Koltunski
281 da36b97e Leszek Koltunski
    canvas.drawLine(cx1, cy, cx2, cy, paint);
282 dad33773 Leszek Koltunski
283 da36b97e Leszek Koltunski
    float X   = REX_C-F/2;
284
    float R1  = TEXTURE_HEIGHT*(REX_R + 0.5f*stroke);
285
    float cx3 = left + (0.5f-X)*TEXTURE_HEIGHT;
286
    float cx4 = left + (0.5f+X)*TEXTURE_HEIGHT;
287 dad33773 Leszek Koltunski
288 da36b97e Leszek Koltunski
    canvas.drawArc( cx3-R1, cy-R1, cx3+R1, cy+R1, 360-REX_B/2, REX_B/2, false ,paint);
289
    canvas.drawArc( cx4-R1, cy-R1, cx4+R1, cy+R1, 180        , REX_B/2, false ,paint);
290
291
    float cx5 = left + (0.5f+F/2-radius2)*TEXTURE_HEIGHT;
292
    float cx6 = left + (0.5f-F/2+radius2)*TEXTURE_HEIGHT;
293 688f7712 Leszek Koltunski
    float cy1 = top  + (0.5f+G/3-radius2)*TEXTURE_HEIGHT;
294 da36b97e Leszek Koltunski
    float R2  = TEXTURE_HEIGHT*radius2;
295
296
    canvas.drawArc( cx5-R2, cy1-R2, cx5+R2, cy1+R2,  0, 90, false ,paint);
297
    canvas.drawArc( cx6-R2, cy1-R2, cx6+R2, cy1+R2, 90, 90, false ,paint);
298
299
    float cx7 = left + 0.5f*TEXTURE_HEIGHT;
300
    float cy2 = top  + (0.5f-2*G/3 + radius1/REX_S)*TEXTURE_HEIGHT;
301
    float R3  = TEXTURE_HEIGHT*(radius1 + 0.5f*stroke);
302
    canvas.drawArc( cx7-R3, cy2-R3, cx7+R3, cy2+R3, 270-(90-REX_B/2), 180-REX_B, false ,paint);
303 59b87d56 Leszek Koltunski
    }
304
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306
307 749ef882 Leszek Koltunski
  public void drawRexEdgeSticker(Canvas canvas, Paint paint, int left, int top, int color, float stroke, float radius)
308 59b87d56 Leszek Koltunski
    {
309
    paint.setColor(color);
310
    paint.setStyle(Paint.Style.FILL);
311
    canvas.drawRect(left,top,left+TEXTURE_HEIGHT,top+TEXTURE_HEIGHT,paint);
312 0b8ffe1a Leszek Koltunski
313
    paint.setColor(COLOR_BLACK);
314
    paint.setStyle(Paint.Style.STROKE);
315
    paint.setStrokeWidth(stroke*TEXTURE_HEIGHT);
316
317 da36b97e Leszek Koltunski
    final float D = ( 0.5f - (0.5f-REX_D)/3);
318
    float cx1 = left+ TEXTURE_HEIGHT*(0.5f-REX_X);
319
    float cy1 = top + TEXTURE_HEIGHT*(0.5f+REX_X+D);
320
    float cx2 = left+ TEXTURE_HEIGHT*(0.5f+REX_X);
321
    float R1  = TEXTURE_HEIGHT*REX_R;
322
323
    canvas.drawArc( cx1-R1, cy1-R1, cx1+R1, cy1+R1, 315-REX_A-REX_B, REX_B, false ,paint);
324
    canvas.drawArc( cx2-R1, cy1-R1, cx2+R1, cy1+R1, 225+REX_A      , REX_B, false ,paint);
325 0b8ffe1a Leszek Koltunski
326 da36b97e Leszek Koltunski
    float CORR_Y = radius/12;
327
    float CORR_A = 10;
328
    float sin = (float)Math.sin(Math.PI*REX_P/180);
329
    float cx = left + 0.5f*TEXTURE_HEIGHT;
330
    float cy = top  + ( 0.5f + 2*(0.5f-REX_D)/3 -CORR_Y -radius/sin )*TEXTURE_HEIGHT;
331
    float R2  = TEXTURE_HEIGHT*radius;
332 0b8ffe1a Leszek Koltunski
333 da36b97e Leszek Koltunski
    canvas.drawArc( cx-R2, cy-R2, cx+R2, cy+R2, 90-(REX_P-CORR_A), 2*(REX_P-CORR_A), false ,paint);
334 0b8ffe1a Leszek Koltunski
335 da36b97e Leszek Koltunski
    float F = 0.1f;
336
    float G = 0.6f;
337
    float R3  = TEXTURE_HEIGHT*radius*G*0.9f;
338
    float X = G*radius/REX_T;
339
    float cx4 = left + X*TEXTURE_HEIGHT;
340
    float cx3 = left + (1-X)*TEXTURE_HEIGHT;
341
    float cy3 = top + (0.5f - (0.5f-REX_D)/3 + G*radius - F*stroke)*TEXTURE_HEIGHT;
342 0b8ffe1a Leszek Koltunski
343 da36b97e Leszek Koltunski
    canvas.drawArc( cx3-R3, cy3-R3, cx3+R3, cy3+R3, 270           , 90+REX_P, false ,paint);
344
    canvas.drawArc( cx4-R3, cy3-R3, cx4+R3, cy3+R3, 270-(90+REX_P), 90+REX_P, false ,paint);
345 0b8ffe1a Leszek Koltunski
346 da36b97e Leszek Koltunski
    float cy5 = top + D*TEXTURE_HEIGHT;
347 0b8ffe1a Leszek Koltunski
348 da36b97e Leszek Koltunski
    paint.setStrokeWidth((1-2*F)*stroke*TEXTURE_HEIGHT);
349
    canvas.drawLine(left, cy5, left+TEXTURE_HEIGHT, cy5, paint);
350 59b87d56 Leszek Koltunski
    }
351 b89898c5 Leszek Koltunski
  }