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.objectlib.helpers;
|
21
|
|
22
|
import static org.distorted.objectlib.main.TwistyObject.COLOR_STROKE;
|
23
|
import static org.distorted.objectlib.main.TwistyObject.TEXTURE_HEIGHT;
|
24
|
|
25
|
import android.graphics.Canvas;
|
26
|
import android.graphics.Color;
|
27
|
import android.graphics.Paint;
|
28
|
import android.graphics.PorterDuff;
|
29
|
|
30
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
31
|
|
32
|
public class FactorySticker
|
33
|
{
|
34
|
private static FactorySticker mThis;
|
35
|
private static final float PI = (float)Math.PI;
|
36
|
private float mOX, mOY, mR;
|
37
|
|
38
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
39
|
|
40
|
private FactorySticker()
|
41
|
{
|
42
|
|
43
|
}
|
44
|
|
45
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
46
|
|
47
|
public static FactorySticker getInstance()
|
48
|
{
|
49
|
if( mThis==null ) mThis = new FactorySticker();
|
50
|
|
51
|
return mThis;
|
52
|
}
|
53
|
|
54
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
55
|
|
56
|
private float computeAngle(float dx, float dy)
|
57
|
{
|
58
|
double angle = Math.atan2(dy,dx);
|
59
|
float ret = (float)(3*PI/2-angle);
|
60
|
|
61
|
if( ret>2*PI ) ret-= 2*PI;
|
62
|
|
63
|
return ret;
|
64
|
}
|
65
|
|
66
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
67
|
|
68
|
private float getAngle(float[] angles, int index)
|
69
|
{
|
70
|
return angles==null ? 0 : angles[index];
|
71
|
}
|
72
|
|
73
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
74
|
|
75
|
private void computeCircleCoords(float x1,float y1, float x2, float y2, float alpha)
|
76
|
{
|
77
|
float ctg= 1.0f/((float)Math.tan(alpha));
|
78
|
mOX = 0.5f*(x1+x2) - ctg*0.5f*(y1-y2);
|
79
|
mOY = 0.5f*(y1+y2) + ctg*0.5f*(x1-x2);
|
80
|
float dx = mOX-x1;
|
81
|
float dy = mOY-y1;
|
82
|
mR = (float)Math.sqrt(dx*dx+dy*dy);
|
83
|
}
|
84
|
|
85
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
86
|
// circle1: center (x1,y1) radius r1; circle2: center (x2,y2) radius r2.
|
87
|
// Guaranteed to intersect in two points. Find the intersection. Which one? the one that's closer
|
88
|
// to (nearx,neary).
|
89
|
|
90
|
private void findCircleIntersection(float x1,float y1, float r1, float x2, float y2, float r2, float nearx, float neary )
|
91
|
{
|
92
|
float dx = x2-x1;
|
93
|
float dy = y2-y1;
|
94
|
float d = (float)Math.sqrt(dx*dx+dy*dy);
|
95
|
|
96
|
if( d>0 )
|
97
|
{
|
98
|
float Dx = dx/d;
|
99
|
float Dy = dy/d;
|
100
|
float cos = (r1*r1+d*d-r2*r2)/(2*r1*d);
|
101
|
float sin = (float)Math.sqrt(1-cos*cos);
|
102
|
|
103
|
float ox1 = x1 + r1*cos*Dx + r1*sin*Dy;
|
104
|
float oy1 = y1 + r1*cos*Dy - r1*sin*Dx;
|
105
|
float ox2 = x1 + r1*cos*Dx - r1*sin*Dy;
|
106
|
float oy2 = y1 + r1*cos*Dy + r1*sin*Dx;
|
107
|
|
108
|
dx = nearx-ox1;
|
109
|
dy = neary-oy1;
|
110
|
float d1 = dx*dx+dy*dy;
|
111
|
dx = nearx-ox2;
|
112
|
dy = neary-oy2;
|
113
|
float d2 = dx*dx+dy*dy;
|
114
|
|
115
|
if( d1<d2 )
|
116
|
{
|
117
|
mOX = ox1;
|
118
|
mOY = oy1;
|
119
|
}
|
120
|
else
|
121
|
{
|
122
|
mOX = ox2;
|
123
|
mOY = oy2;
|
124
|
}
|
125
|
}
|
126
|
else
|
127
|
{
|
128
|
mOX = x1;
|
129
|
mOY = y1;
|
130
|
}
|
131
|
}
|
132
|
|
133
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
134
|
|
135
|
private void drawCurrSide(Canvas canvas, Paint paint, int left, int bottom, float stroke,
|
136
|
float pX, float pY, float cX, float cY, float pAngle)
|
137
|
{
|
138
|
pX = (0.5f+pX)*TEXTURE_HEIGHT;
|
139
|
pY = (0.5f-pY)*TEXTURE_HEIGHT;
|
140
|
cX = (0.5f+cX)*TEXTURE_HEIGHT;
|
141
|
cY = (0.5f-cY)*TEXTURE_HEIGHT;
|
142
|
|
143
|
if( pAngle==0 )
|
144
|
{
|
145
|
float aX = pX-cX;
|
146
|
float aY = pY-cY;
|
147
|
float aLen = (float)Math.sqrt(aX*aX+aY*aY);
|
148
|
aX /= aLen;
|
149
|
aY /= aLen;
|
150
|
|
151
|
// draw a little more - stroke*(aX,aY) more - so
|
152
|
// that we draw over the rounded corners (Kilominx!)
|
153
|
canvas.drawLine(left+pX,bottom-pY,left+cX-stroke*aX,bottom-cY+stroke*aY,paint);
|
154
|
}
|
155
|
else
|
156
|
{
|
157
|
computeCircleCoords(pX,pY,cX,cY,pAngle);
|
158
|
float ox = mOX;
|
159
|
float oy = mOY;
|
160
|
float r = mR;
|
161
|
|
162
|
float dx = ox-pX;
|
163
|
float dy = oy-pY;
|
164
|
float startA = computeAngle(-dy,dx);
|
165
|
float sweepA = 2*pAngle;
|
166
|
|
167
|
startA *= 180/PI;
|
168
|
sweepA *= 180/PI;
|
169
|
|
170
|
canvas.drawArc( left+ox-r, bottom-oy-r, left+ox+r, bottom-oy+r, startA, sweepA, false, paint);
|
171
|
}
|
172
|
}
|
173
|
|
174
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
175
|
// quotient in (0,1).
|
176
|
// quotient==0 --> ret=curvature; quotient==1 --> ret=0.
|
177
|
|
178
|
private float computeQuotientOfCurvature(float quotient, float curvature)
|
179
|
{
|
180
|
if( curvature!=0 )
|
181
|
{
|
182
|
double sinC = Math.sin(curvature);
|
183
|
float arcsin = (float)Math.asin(quotient*sinC);
|
184
|
return curvature-arcsin;
|
185
|
}
|
186
|
return curvature;
|
187
|
}
|
188
|
|
189
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
190
|
|
191
|
private float computeSideAngle(float vX, float vY, float radius, float curvature)
|
192
|
{
|
193
|
float quotient = radius/(float)Math.sqrt(vX*vX + vY*vY);
|
194
|
float ret = computeAngle(vX,-vY)-computeQuotientOfCurvature(quotient,curvature);
|
195
|
|
196
|
if( ret>=2*PI ) ret -= 2*PI;
|
197
|
if( ret<0 ) ret += 2*PI;
|
198
|
|
199
|
return ret;
|
200
|
}
|
201
|
|
202
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
203
|
|
204
|
private float angleMidpoint(float angle1, float angle2)
|
205
|
{
|
206
|
float diff = angle2-angle1;
|
207
|
if( diff<0 ) diff = -diff;
|
208
|
float avg = (angle1+angle2)/2;
|
209
|
|
210
|
if( diff>PI )
|
211
|
{
|
212
|
avg -= PI;
|
213
|
if( avg<0 ) avg += 2*PI;
|
214
|
}
|
215
|
|
216
|
return avg;
|
217
|
}
|
218
|
|
219
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
220
|
|
221
|
private void drawRoundCorner(Canvas canvas, Paint paint,int color, int left, int bottom, float stroke,
|
222
|
float radius, float cX, float cY, float pA, float cA)
|
223
|
{
|
224
|
cX = (0.5f+cX)*TEXTURE_HEIGHT;
|
225
|
cY = (0.5f-cY)*TEXTURE_HEIGHT;
|
226
|
float R = radius*TEXTURE_HEIGHT + stroke/2;
|
227
|
|
228
|
boolean isConvex = ((pA<cA && cA<pA+PI) || (pA<cA+2*PI && cA+2*PI<pA+PI));
|
229
|
float startA, stopA, centerA, alpha, D;
|
230
|
|
231
|
if( isConvex )
|
232
|
{
|
233
|
startA = cA;
|
234
|
stopA = pA;
|
235
|
if( startA>2*PI ) startA -= 2*PI;
|
236
|
if( stopA >2*PI ) stopA -= 2*PI;
|
237
|
centerA= angleMidpoint(pA,cA) - PI/2;
|
238
|
if( centerA<0 ) centerA += 2*PI;
|
239
|
float diff = cA-centerA;
|
240
|
if( diff<0 ) diff += 2*PI;
|
241
|
alpha = diff> PI/2 ? PI-diff : diff;
|
242
|
D = (float)(R/Math.sin(alpha));
|
243
|
}
|
244
|
else
|
245
|
{
|
246
|
startA = pA + PI;
|
247
|
stopA = cA + PI;
|
248
|
centerA= angleMidpoint(pA,cA) + PI/2;
|
249
|
if( centerA>=2*PI ) centerA -= 2*PI;
|
250
|
float diff = centerA-cA;
|
251
|
if( diff<0 ) diff += 2*PI;
|
252
|
alpha = diff> PI/2 ? PI-diff : diff;
|
253
|
D = (float)((R-stroke)/Math.sin(alpha));
|
254
|
}
|
255
|
|
256
|
float sweepA = startA-stopA;
|
257
|
if( sweepA<0 ) sweepA += 2*PI;
|
258
|
sweepA = -sweepA;
|
259
|
|
260
|
float sinA = (float)(Math.sin(centerA));
|
261
|
float cosA = (float)(Math.cos(centerA));
|
262
|
float oX= cX + D*sinA;
|
263
|
float oY= cY + D*cosA;
|
264
|
|
265
|
startA *= 180/PI;
|
266
|
sweepA *= 180/PI;
|
267
|
|
268
|
if( !isConvex ) paint.setColor(color);
|
269
|
canvas.drawArc( left+oX-R, bottom-oY-R, left+oX+R, bottom-oY+R, startA, sweepA, false, paint);
|
270
|
if( !isConvex ) paint.setColor(COLOR_STROKE);
|
271
|
}
|
272
|
|
273
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
274
|
// PUBLIC
|
275
|
|
276
|
public void drawRoundedPolygon(Canvas canvas, Paint paint, int left, int bottom, int color, ObjectSticker sticker)
|
277
|
{
|
278
|
float stroke = sticker.getStroke();
|
279
|
float[] vertices = sticker.getCoords();
|
280
|
float[] angles = sticker.getCurvature();
|
281
|
float[] radii = sticker.getRadii();
|
282
|
|
283
|
stroke *= TEXTURE_HEIGHT;
|
284
|
|
285
|
paint.setAntiAlias(true);
|
286
|
paint.setStrokeWidth(stroke);
|
287
|
paint.setColor(color);
|
288
|
paint.setStyle(Paint.Style.FILL);
|
289
|
|
290
|
canvas.save();
|
291
|
canvas.clipRect(left,bottom-TEXTURE_HEIGHT,left+TEXTURE_HEIGHT,bottom);
|
292
|
canvas.drawRect(left,bottom-TEXTURE_HEIGHT,left+TEXTURE_HEIGHT,bottom,paint);
|
293
|
|
294
|
paint.setColor(COLOR_STROKE);
|
295
|
paint.setStyle(Paint.Style.STROKE);
|
296
|
|
297
|
int length = vertices.length;
|
298
|
int numVertices = length/2;
|
299
|
|
300
|
float prevX = vertices[length-2];
|
301
|
float prevY = vertices[length-1];
|
302
|
float currX = vertices[0];
|
303
|
float currY = vertices[1];
|
304
|
float nextX = vertices[2];
|
305
|
float nextY = vertices[3];
|
306
|
|
307
|
float prevA = getAngle(angles,numVertices-1);
|
308
|
float currA = getAngle(angles,0);
|
309
|
|
310
|
for(int vert=0; vert<numVertices; vert++)
|
311
|
{
|
312
|
drawCurrSide(canvas,paint,left,bottom,stroke,prevX,prevY,currX,currY,prevA);
|
313
|
|
314
|
prevX = currX;
|
315
|
prevY = currY;
|
316
|
currX = nextX;
|
317
|
currY = nextY;
|
318
|
|
319
|
prevA = currA;
|
320
|
currA = getAngle(angles, vert==numVertices-1 ? 0 : vert+1);
|
321
|
|
322
|
if( 2*(vert+2)+1 < length )
|
323
|
{
|
324
|
nextX = vertices[2*(vert+2) ];
|
325
|
nextY = vertices[2*(vert+2)+1];
|
326
|
}
|
327
|
else
|
328
|
{
|
329
|
nextX = vertices[0];
|
330
|
nextY = vertices[1];
|
331
|
}
|
332
|
}
|
333
|
|
334
|
prevX = vertices[length-2];
|
335
|
prevY = vertices[length-1];
|
336
|
currX = vertices[0];
|
337
|
currY = vertices[1];
|
338
|
nextX = vertices[2];
|
339
|
nextY = vertices[3];
|
340
|
|
341
|
prevA = getAngle(angles,numVertices-1);
|
342
|
currA = getAngle(angles,0);
|
343
|
|
344
|
for(int vert=0; vert<numVertices; vert++)
|
345
|
{
|
346
|
int prev = vert==0 ? numVertices-1 : vert-1;
|
347
|
float prevAngle = computeSideAngle(currX-prevX,currY-prevY,radii[prev],-prevA);
|
348
|
float currAngle = computeSideAngle(nextX-currX,nextY-currY,radii[vert],+currA);
|
349
|
drawRoundCorner(canvas,paint,color,left,bottom,stroke,radii[vert],currX,currY,prevAngle,currAngle);
|
350
|
|
351
|
prevX = currX;
|
352
|
prevY = currY;
|
353
|
currX = nextX;
|
354
|
currY = nextY;
|
355
|
|
356
|
prevA = currA;
|
357
|
currA = getAngle(angles, vert==numVertices-1 ? 0 : vert+1);
|
358
|
|
359
|
if( 2*(vert+2)+1 < length )
|
360
|
{
|
361
|
nextX = vertices[2*(vert+2) ];
|
362
|
nextY = vertices[2*(vert+2)+1];
|
363
|
}
|
364
|
else
|
365
|
{
|
366
|
nextX = vertices[0];
|
367
|
nextY = vertices[1];
|
368
|
}
|
369
|
}
|
370
|
|
371
|
canvas.restore();
|
372
|
}
|
373
|
|
374
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
375
|
|
376
|
public void drawSolidColor(Canvas canvas, Paint paint, int left, int bottom, int color)
|
377
|
{
|
378
|
canvas.save();
|
379
|
canvas.clipRect(left,bottom-TEXTURE_HEIGHT,left+TEXTURE_HEIGHT,bottom);
|
380
|
|
381
|
if( (color>>24) != 0 )
|
382
|
{
|
383
|
paint.setStyle(Paint.Style.FILL);
|
384
|
paint.setColor(color);
|
385
|
canvas.drawRect(left,bottom-TEXTURE_HEIGHT,left+TEXTURE_HEIGHT,bottom,paint);
|
386
|
}
|
387
|
else
|
388
|
{
|
389
|
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
|
390
|
}
|
391
|
|
392
|
canvas.restore();
|
393
|
}
|
394
|
}
|