Project

General

Profile

Download (10.2 KB) Statistics
| Branch: | Revision:

distorted-objectlib / src / main / java / org / distorted / objectlib / helpers / FactorySticker.java @ cc70f525

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.Paint;
27

    
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29

    
30
public class FactorySticker
31
  {
32
  private static FactorySticker mThis;
33
  private static final float PI = (float)Math.PI;
34
  private float mOX, mOY, mR;
35

    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37

    
38
  private FactorySticker()
39
    {
40

    
41
    }
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
  public static FactorySticker getInstance()
46
    {
47
    if( mThis==null ) mThis = new FactorySticker();
48

    
49
    return mThis;
50
    }
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

    
54
  private float computeAngle(float dx, float dy)
55
    {
56
    double angle = Math.atan2(dy,dx);
57
    float ret = (float)(3*PI/2-angle);
58

    
59
    if( ret>2*PI ) ret-= 2*PI;
60

    
61
    return ret;
62
    }
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
  private float getAngle(float[] angles, int index)
67
    {
68
    return angles==null ? 0 : angles[index];
69
    }
70

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

    
73
  private void computeCircleCoords(float x1,float y1, float x2, float y2, float alpha)
74
    {
75
    float ctg= 1.0f/((float)Math.tan(alpha));
76
    mOX = 0.5f*(x1+x2) - ctg*0.5f*(y1-y2);
77
    mOY = 0.5f*(y1+y2) + ctg*0.5f*(x1-x2);
78
    float dx = mOX-x1;
79
    float dy = mOY-y1;
80
    mR = (float)Math.sqrt(dx*dx+dy*dy);
81
    }
82

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84
// circle1: center (x1,y1) radius r1; circle2: center (x2,y2) radius r2.
85
// Guaranteed to intersect in two points. Find the intersection. Which one? the one that's closer
86
// to (nearx,neary).
87

    
88
  private void findCircleIntersection(float x1,float y1, float r1, float x2, float y2, float r2, float nearx, float neary )
89
    {
90
    float dx = x2-x1;
91
    float dy = y2-y1;
92
    float d = (float)Math.sqrt(dx*dx+dy*dy);
93

    
94
    if( d>0 )
95
      {
96
      float Dx = dx/d;
97
      float Dy = dy/d;
98
      float cos = (r1*r1+d*d-r2*r2)/(2*r1*d);
99
      float sin = (float)Math.sqrt(1-cos*cos);
100

    
101
      float ox1 = x1 + r1*cos*Dx + r1*sin*Dy;
102
      float oy1 = y1 + r1*cos*Dy - r1*sin*Dx;
103
      float ox2 = x1 + r1*cos*Dx - r1*sin*Dy;
104
      float oy2 = y1 + r1*cos*Dy + r1*sin*Dx;
105

    
106
      dx = nearx-ox1;
107
      dy = neary-oy1;
108
      float d1 = dx*dx+dy*dy;
109
      dx = nearx-ox2;
110
      dy = neary-oy2;
111
      float d2 = dx*dx+dy*dy;
112

    
113
      if( d1<d2 )
114
        {
115
        mOX = ox1;
116
        mOY = oy1;
117
        }
118
      else
119
        {
120
        mOX = ox2;
121
        mOY = oy2;
122
        }
123
      }
124
    else
125
      {
126
      mOX = x1;
127
      mOY = y1;
128
      }
129
    }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132

    
133
  private void drawCurrSide(Canvas canvas, Paint paint, int left, int top, float stroke,
134
                            float pX, float pY, float cX, float cY, float pAngle)
135
    {
136
    pX = (0.5f+pX)*TEXTURE_HEIGHT;
137
    pY = (0.5f-pY)*TEXTURE_HEIGHT;
138
    cX = (0.5f+cX)*TEXTURE_HEIGHT;
139
    cY = (0.5f-cY)*TEXTURE_HEIGHT;
140

    
141
    if( pAngle==0 )
142
      {
143
      float aX = pX-cX;
144
      float aY = pY-cY;
145
      float aLen = (float)Math.sqrt(aX*aX+aY*aY);
146
      aX /= aLen;
147
      aY /= aLen;
148

    
149
      // draw a little more - stroke*(aX,aY) more - so
150
      // that we draw over the rounded corners (Kilominx!)
151
      canvas.drawLine(left+pX,top+pY,left+cX-stroke*aX,top+cY-stroke*aY,paint);
152
      }
153
    else
154
      {
155
      computeCircleCoords(pX,pY,cX,cY,pAngle);
156
      float ox = mOX;
157
      float oy = mOY;
158
      float r  = mR;
159

    
160
      float dx = ox-pX;
161
      float dy = oy-pY;
162
      float startA = computeAngle(dy,dx);
163
      float sweepA = -2*pAngle;
164

    
165
      startA *= 180/PI;
166
      sweepA *= 180/PI;
167

    
168
      canvas.drawArc( left+ox-r, top+oy-r, left+ox+r, top+oy+r, startA, sweepA, false, paint);
169
      }
170
    }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

    
174
  private float computeSideAngle(float vX, float vY, float curvature)
175
    {
176
    float ret = computeAngle(vX,vY)+curvature;
177

    
178
    if( ret>=2*PI ) ret -= 2*PI;
179
    if( ret<0     ) ret += 2*PI;
180

    
181
    return ret;
182
    }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

    
186
  private float angleMidpoint(float angle1, float angle2)
187
    {
188
    float diff = angle2-angle1;
189
    if( diff<0 ) diff = -diff;
190
    float avg = (angle1+angle2)/2;
191

    
192
    if( diff>PI )
193
      {
194
      avg -= PI;
195
      if( avg<0 ) avg += 2*PI;
196
      }
197

    
198
    return avg;
199
    }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

    
203
  private void drawRoundCorner(Canvas canvas, Paint paint,int color, int left, int top, float stroke,
204
                              float radius, float currX, float currY, float pA, float cA)
205
    {
206
    currX = (0.5f+currX)*TEXTURE_HEIGHT;
207
    currY = (0.5f-currY)*TEXTURE_HEIGHT;
208
    float R = radius*TEXTURE_HEIGHT + stroke/2;
209

    
210
    boolean isConvex = (!(pA<cA && cA<pA+PI) && !(pA<cA+2*PI && cA+2*PI<pA+PI));
211
    float startA, stopA, centerA, alpha, D;
212

    
213
    if( isConvex )
214
      {
215
      startA = cA + PI;
216
      stopA  = pA + PI;
217
      if( startA>2*PI ) startA -= 2*PI;
218
      if( stopA >2*PI ) stopA  -= 2*PI;
219
      centerA= angleMidpoint(pA,cA) - PI/2;
220
      if( centerA<0 ) centerA += 2*PI;
221
      float diff = cA-centerA;
222
      if( diff<0 ) diff += 2*PI;
223
      alpha = diff> PI/2 ? PI-diff : diff;
224
      D = (float)(R/Math.sin(alpha));
225
      }
226
    else
227
      {
228
      startA = pA;
229
      stopA  = cA;
230
      centerA= angleMidpoint(pA,cA) + PI/2;
231
      if( centerA>=2*PI ) centerA -= 2*PI;
232
      float diff = centerA-cA;
233
      if( diff<0 ) diff += 2*PI;
234
      alpha = diff> PI/2 ? PI-diff : diff;
235
      D = (float)((R-stroke)/Math.sin(alpha));
236
      }
237

    
238
    float sweepA = stopA-startA;
239
    if( sweepA<0 ) sweepA += 2*PI;
240

    
241
    float sinA = (float)(Math.sin(centerA));
242
    float cosA = (float)(Math.cos(centerA));
243
    float oX= currX - D*sinA;
244
    float oY= currY + D*cosA;
245

    
246
    startA *= 180/PI;
247
    sweepA *= 180/PI;
248

    
249
    if( !isConvex ) paint.setColor(color);
250
    canvas.drawArc( left+oX-R, top+oY-R, left+oX+R, top+oY+R, startA, sweepA, false, paint);
251
    if( !isConvex ) paint.setColor(COLOR_STROKE);
252
    }
253

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255
// PUBLIC
256

    
257
  public void drawRoundedPolygon(Canvas canvas, Paint paint, int left, int top, int color, ObjectSticker sticker)
258
    {
259
    float stroke = sticker.getStroke();
260
    float[] vertices = sticker.getCoords();
261
    float[] angles = sticker.getCurvature();
262
    float[] radii = sticker.getRadii();
263

    
264
    stroke *= TEXTURE_HEIGHT;
265

    
266
    paint.setAntiAlias(true);
267
    paint.setStrokeWidth(stroke);
268
    paint.setColor(color);
269
    paint.setStyle(Paint.Style.FILL);
270

    
271
    canvas.save();
272
    canvas.clipRect(left,top,left+TEXTURE_HEIGHT,top+TEXTURE_HEIGHT);
273
    canvas.drawRect(left,top,left+TEXTURE_HEIGHT,top+TEXTURE_HEIGHT,paint);
274

    
275
    paint.setColor(COLOR_STROKE);
276
    paint.setStyle(Paint.Style.STROKE);
277

    
278
    int length = vertices.length;
279
    int numVertices = length/2;
280

    
281
    float prevX = vertices[length-2];
282
    float prevY = vertices[length-1];
283
    float currX = vertices[0];
284
    float currY = vertices[1];
285
    float nextX = vertices[2];
286
    float nextY = vertices[3];
287

    
288
    float prevA = getAngle(angles,numVertices-1);
289
    float currA = getAngle(angles,0);
290

    
291
    for(int vert=0; vert<numVertices; vert++)
292
      {
293
      drawCurrSide(canvas,paint,left,top,stroke,prevX,prevY,currX,currY,prevA);
294

    
295
      float prevAngle = computeSideAngle(currX-prevX,currY-prevY,-prevA);
296
      float currAngle = computeSideAngle(nextX-currX,nextY-currY,+currA);
297
      drawRoundCorner(canvas,paint,color,left,top,stroke,radii[vert],currX,currY,prevAngle,currAngle);
298

    
299
      prevX = currX;
300
      prevY = currY;
301
      currX = nextX;
302
      currY = nextY;
303

    
304
      prevA = currA;
305
      currA = getAngle(angles, vert==numVertices-1 ? 0 : vert+1);
306

    
307
      if( 2*(vert+2)+1 < length )
308
        {
309
        nextX = vertices[2*(vert+2)  ];
310
        nextY = vertices[2*(vert+2)+1];
311
        }
312
      else
313
        {
314
        nextX = vertices[0];
315
        nextY = vertices[1];
316
        }
317
      }
318

    
319
    canvas.restore();
320
    }
321
  }
(4-4/10)