Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / FactorySticker.java @ c5338d5e

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