Project

General

Profile

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

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

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.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
import static org.distorted.objects.FactoryCubit.IVY_D;
28
import static org.distorted.objects.FactoryCubit.IVY_C;
29
import static org.distorted.objects.FactoryCubit.IVY_M;
30
import static org.distorted.objects.FactoryCubit.REX_D;
31

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

    
34
class FactorySticker
35
  {
36
  private static final float SQ2 = (float)Math.sqrt(2);
37
  private static final float SQ3 = (float)Math.sqrt(3);
38

    
39
  private static FactorySticker mThis;
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

    
43
  private FactorySticker()
44
    {
45

    
46
    }
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

    
50
  public static FactorySticker getInstance()
51
    {
52
    if( mThis==null ) mThis = new FactorySticker();
53

    
54
    return mThis;
55
    }
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
  private float computeAngle(float dx, float dy)
60
    {
61
    float PI = (float)Math.PI;
62
    double angle = Math.atan2(dy,dx);
63
    float ret = (float)(3*PI/2-angle);
64

    
65
    if( ret>2*PI ) ret-= 2*PI;
66

    
67
    return ret;
68
    }
69

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

    
72
  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)
73
    {
74
    pX = (0.5f+pX)*TEXTURE_HEIGHT;
75
    pY = (0.5f-pY)*TEXTURE_HEIGHT;
76
    cX = (0.5f+cX)*TEXTURE_HEIGHT;
77
    cY = (0.5f-cY)*TEXTURE_HEIGHT;
78
    nX = (0.5f+nX)*TEXTURE_HEIGHT;
79
    nY = (0.5f-nY)*TEXTURE_HEIGHT;
80

    
81
    canvas.drawLine(left+pX,top+pY,left+cX,top+cY,paint);
82

    
83
    float aX = pX-cX;
84
    float aY = pY-cY;
85
    float bX = cX-nX;
86
    float bY = cY-nY;
87

    
88
    float aLen = (float)Math.sqrt(aX*aX+aY*aY);
89
    float bLen = (float)Math.sqrt(bX*bX+bY*bY);
90

    
91
    aX /= aLen;
92
    aY /= aLen;
93
    bX /= bLen;
94
    bY /= bLen;
95

    
96
    float sX = (aX-bX)/2;
97
    float sY = (aY-bY)/2;
98
    float sLen = (float)Math.sqrt(sX*sX+sY*sY);
99
    sX /= sLen;
100
    sY /= sLen;
101

    
102
    float startAngle = computeAngle(bX,-bY);
103
    float endAngle   = computeAngle(aX,-aY);
104
    float sweepAngle = endAngle-startAngle;
105
    if( sweepAngle<0 ) sweepAngle += 2*Math.PI;
106

    
107
    float R = r*TEXTURE_HEIGHT+stroke/2;
108

    
109
    float A = (float)(R/(Math.cos(sweepAngle/2)));
110

    
111
    float rX = cX + A*sX;
112
    float rY = cY + A*sY;
113

    
114
    startAngle *= 180/(Math.PI);
115
    sweepAngle *= 180/(Math.PI);
116

    
117
    canvas.drawArc( left+rX-R, top+rY-R, left+rX+R, top+rY+R, startAngle, sweepAngle, false, paint);
118
    }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

    
122
  void drawRoundedPolygon(Canvas canvas, Paint paint, int left, int top, float[] vertices, float stroke, int color, float r)
123
    {
124
    stroke *= TEXTURE_HEIGHT;
125

    
126
    paint.setAntiAlias(true);
127
    paint.setStrokeWidth(stroke);
128
    paint.setColor(color);
129
    paint.setStyle(Paint.Style.FILL);
130

    
131
    canvas.drawRect(left,top,left+TEXTURE_HEIGHT,top+TEXTURE_HEIGHT,paint);
132

    
133
    paint.setColor(COLOR_BLACK);
134
    paint.setStyle(Paint.Style.STROKE);
135

    
136
    int length = vertices.length;
137
    int numVertices = length/2;
138

    
139
    float prevX = vertices[length-2];
140
    float prevY = vertices[length-1];
141
    float currX = vertices[0];
142
    float currY = vertices[1];
143
    float nextX = vertices[2];
144
    float nextY = vertices[3];
145

    
146
    for(int vert=0; vert<numVertices; vert++)
147
      {
148
      drawCurrVertex(canvas, paint, left, top, r, stroke, prevX,prevY,currX,currY,nextX,nextY);
149

    
150
      prevX = currX;
151
      prevY = currY;
152
      currX = nextX;
153
      currY = nextY;
154

    
155
      if( 2*(vert+2)+1 < length )
156
        {
157
        nextX = vertices[2*(vert+2)  ];
158
        nextY = vertices[2*(vert+2)+1];
159
        }
160
      else
161
        {
162
        nextX = vertices[0];
163
        nextY = vertices[1];
164
        }
165
      }
166
    }
167

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

    
170
  void drawIvyCornerSticker(Canvas canvas, Paint paint, int left, int top, int color, float stroke, float radius)
171
    {
172
    paint.setAntiAlias(true);
173
    paint.setColor(color);
174
    paint.setStyle(Paint.Style.FILL);
175
    canvas.drawRect(left,top,left+TEXTURE_HEIGHT,top+TEXTURE_HEIGHT,paint);
176

    
177
    paint.setColor(COLOR_BLACK);
178
    paint.setStyle(Paint.Style.STROKE);
179
    paint.setStrokeWidth(IVY_C*stroke*TEXTURE_HEIGHT);
180

    
181
    float tmp1 = ((IVY_D-0.5f)-IVY_M)*IVY_C;
182
    float cx1 = TEXTURE_HEIGHT*(tmp1 + 0.5f);
183
    float cy1 = TEXTURE_HEIGHT*(0.5f - tmp1);
184

    
185
    float halfL1 = IVY_C*TEXTURE_HEIGHT*(1.0f-2*IVY_D);
186

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

    
189
    float tmp2 = (+0.5f-IVY_M)*IVY_C;
190
    float tmp3 = (-0.5f-IVY_M)*IVY_C;
191

    
192
    float x0 = TEXTURE_HEIGHT*(+tmp2 + 0.5f);
193
    float y0 = TEXTURE_HEIGHT*(-tmp3 + 0.5f);
194
    float x1 = TEXTURE_HEIGHT*(+tmp2 + 0.5f);
195
    float y1 = TEXTURE_HEIGHT*(-tmp2 + 0.5f);
196
    float x2 = TEXTURE_HEIGHT*(+tmp3 + 0.5f);
197
    float y2 = TEXTURE_HEIGHT*(-tmp2 + 0.5f);
198

    
199
    canvas.drawLine(left+x0,top+y0,left+x1,top+y1,paint);
200
    canvas.drawLine(left+x1,top+y1,left+x2,top+y2,paint);
201

    
202
    float tmp4 = ((0.5f-stroke/2-radius/2)-IVY_M)*IVY_C;
203
    float cx2 = TEXTURE_HEIGHT*(tmp4 + 0.5f);
204
    float cy2 = TEXTURE_HEIGHT*(0.5f - tmp4);
205

    
206
    float halfL2 = IVY_C*TEXTURE_HEIGHT*radius;
207

    
208
    paint.setStrokeWidth(IVY_C*radius*TEXTURE_HEIGHT);
209
    canvas.drawArc( left+cx2-halfL2, top+cy2-halfL2, left+cx2+halfL2, top+cy2+halfL2, 270, 90, false, paint);
210
    }
211

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

    
214
  void drawIvyCenterSticker(Canvas canvas, Paint paint, int left, int top, int color, float stroke, float radius)
215
    {
216
    paint.setAntiAlias(true);
217
    paint.setColor(color);
218
    paint.setStyle(Paint.Style.FILL);
219
    canvas.drawRect(left,top,left+TEXTURE_HEIGHT,top+TEXTURE_HEIGHT,paint);
220

    
221
    paint.setColor(COLOR_BLACK);
222
    paint.setStyle(Paint.Style.STROKE);
223
    paint.setStrokeWidth(stroke*TEXTURE_HEIGHT);
224

    
225
    float cx1 = TEXTURE_HEIGHT*IVY_D;
226
    float cy1 = TEXTURE_HEIGHT*(1-IVY_D);
227
    float cx2 = TEXTURE_HEIGHT*(1.0f-IVY_D);
228
    float cy2 = TEXTURE_HEIGHT*IVY_D;
229

    
230
    float halfL = TEXTURE_HEIGHT*(1.0f - 2*IVY_D);
231

    
232
    canvas.drawArc( left+cx1-halfL, top+cy1-halfL, left+cx1+halfL, top+cy1+halfL, 270, 90, false, paint);
233
    canvas.drawArc( left+cx2-halfL, top+cy2-halfL, left+cx2+halfL, top+cy2+halfL,  90, 90, false, paint);
234

    
235
    float tmp = TEXTURE_HEIGHT*(IVY_D+stroke*0.5f+radius*0.5f);
236
    float cx3 = tmp;
237
    float cy3 = tmp;
238
    float cx4 = TEXTURE_HEIGHT - cx3;
239
    float cy4 = TEXTURE_HEIGHT - cy3;
240
    float halfR = TEXTURE_HEIGHT*radius;
241

    
242
    paint.setStrokeWidth(radius*TEXTURE_HEIGHT);
243
    canvas.drawArc( left+cx3-halfR, top+cy3-halfR, left+cx3+halfR, top+cy3+halfR, 180, 90, false, paint);
244
    canvas.drawArc( left+cx4-halfR, top+cy4-halfR, left+cx4+halfR, top+cy4+halfR,   0, 90, false, paint);
245
    }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248
// TODO
249

    
250
  void drawRexCornerSticker(Canvas canvas, Paint paint, int left, int top, int color, float stroke, float radius)
251
    {
252
    paint.setColor(color);
253
    paint.setStyle(Paint.Style.FILL);
254
    canvas.drawRect(left,top,left+TEXTURE_HEIGHT,top+TEXTURE_HEIGHT,paint);
255

    
256
    paint.setColor(COLOR_BLACK);
257
    paint.setStyle(Paint.Style.STROKE);
258
    paint.setStrokeWidth(stroke*TEXTURE_HEIGHT);
259

    
260
    float T  = (0.5f-REX_D)*SQ2*(SQ3-1);
261
    float H1 = T*(1.0f + 2*SQ3/3);
262
    float H2 = T*(0.5f +   SQ3/6);
263
    float D  = H2*SQ3;
264

    
265
    float cx1 = left+ TEXTURE_HEIGHT*0.5f;
266
    float cy1 = top + TEXTURE_HEIGHT*(0.5f+H1);
267
    float cx2 = left+ TEXTURE_HEIGHT*(0.5f-D);
268
    float cy2 = top + TEXTURE_HEIGHT*(0.5f+H2);
269
    float cx3 = left+ TEXTURE_HEIGHT*(0.5f+D);
270
    float cy3 = top + TEXTURE_HEIGHT*(0.5f+H2);
271

    
272
    float R1= TEXTURE_HEIGHT*(1-2*REX_D);
273

    
274
    canvas.drawArc( cx1-R1, cy1-R1, cx1+R1, cy1+R1, 255, 30, false ,paint);
275
    canvas.drawArc( cx2-R1, cy2-R1, cx2+R1, cy2+R1, 315, 30, false ,paint);
276
    canvas.drawArc( cx3-R1, cy3-R1, cx3+R1, cy3+R1, 195, 30, false ,paint);
277
/*
278
    float cx   = left+ TEXTURE_HEIGHT*0.5f;
279
    float cy   = top + TEXTURE_HEIGHT*0.5f;
280
    float R2   = radius*TEXTURE_HEIGHT;
281
    float dist = ( (0.5f-REX_D)*(SQ3-1) - (2*SQ3/3)*radius )*TEXTURE_HEIGHT;
282

    
283
    canvas.drawArc( cx+dist-R2, cy-R2, cx+dist+R2, cy+R2, 330, 60, false ,paint);
284
    canvas.drawArc( cx-R2, cy+dist-R2, cx+R2, cy+dist+R2,  60, 60, false ,paint);
285
    canvas.drawArc( cx-dist-R2, cy-R2, cx-dist+R2, cy+R2, 150, 60, false ,paint);
286
    canvas.drawArc( cx-R2, cy-dist-R2, cx+R2, cy-dist+R2, 240, 60, false ,paint);
287
 */
288
    }
289

    
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291

    
292
  void drawRexFaceSticker(Canvas canvas, Paint paint, int left, int top, int color, float stroke, float radius)
293
    {
294
    paint.setColor(color);
295
    paint.setStyle(Paint.Style.FILL);
296
    canvas.drawRect(left,top,left+TEXTURE_HEIGHT,top+TEXTURE_HEIGHT,paint);
297

    
298
    paint.setColor(COLOR_BLACK);
299
    paint.setStyle(Paint.Style.STROKE);
300
    paint.setStrokeWidth(stroke*TEXTURE_HEIGHT);
301

    
302
    float cx1 = left+ TEXTURE_HEIGHT*REX_D;
303
    float cy1 = top + TEXTURE_HEIGHT*(1-REX_D);
304
    float cx2 = left+ TEXTURE_HEIGHT*REX_D;
305
    float cy2 = top + TEXTURE_HEIGHT*REX_D;
306
    float cx3 = left+ TEXTURE_HEIGHT*(1-REX_D);
307
    float cy3 = top + TEXTURE_HEIGHT*REX_D;
308
    float cx4 = left+ TEXTURE_HEIGHT*(1-REX_D);
309
    float cy4 = top + TEXTURE_HEIGHT*(1-REX_D);
310
    float R1  = TEXTURE_HEIGHT*(1-2*REX_D);
311

    
312
    canvas.drawArc( cx1-R1, cy1-R1, cx1+R1, cy1+R1, 300, 30, false ,paint);
313
    canvas.drawArc( cx2-R1, cy2-R1, cx2+R1, cy2+R1,  30, 30, false ,paint);
314
    canvas.drawArc( cx3-R1, cy3-R1, cx3+R1, cy3+R1, 120, 30, false ,paint);
315
    canvas.drawArc( cx4-R1, cy4-R1, cx4+R1, cy4+R1, 210, 30, false ,paint);
316

    
317
    float cx   = left+ TEXTURE_HEIGHT*0.5f;
318
    float cy   = top + TEXTURE_HEIGHT*0.5f;
319
    float R2   = radius*TEXTURE_HEIGHT;
320
    float dist = ( (0.5f-REX_D)*(SQ3-1) - (2*SQ3/3)*radius )*TEXTURE_HEIGHT;
321

    
322
    canvas.drawArc( cx+dist-R2, cy-R2, cx+dist+R2, cy+R2, 330, 60, false ,paint);
323
    canvas.drawArc( cx-R2, cy+dist-R2, cx+R2, cy+dist+R2,  60, 60, false ,paint);
324
    canvas.drawArc( cx-dist-R2, cy-R2, cx-dist+R2, cy+R2, 150, 60, false ,paint);
325
    canvas.drawArc( cx-R2, cy-dist-R2, cx+R2, cy-dist+R2, 240, 60, false ,paint);
326
    }
327

    
328
///////////////////////////////////////////////////////////////////////////////////////////////////
329
// TODO
330

    
331
  void drawRexEdgeSticker(Canvas canvas, Paint paint, int left, int top, int color, float stroke, float radius)
332
    {
333
    paint.setColor(color);
334
    paint.setStyle(Paint.Style.FILL);
335
    canvas.drawRect(left,top,left+TEXTURE_HEIGHT,top+TEXTURE_HEIGHT,paint);
336
    }
337
  }
(3-3/26)