Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / FactorySticker.java @ 234a7582

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 b89898c5 Leszek Koltunski
31
///////////////////////////////////////////////////////////////////////////////////////////////////
32
33
class FactorySticker
34
  {
35
  private static FactorySticker mThis;
36
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38
39
  private FactorySticker()
40
    {
41
42
    }
43
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45
46
  public static FactorySticker getInstance()
47
    {
48
    if( mThis==null ) mThis = new FactorySticker();
49
50
    return mThis;
51
    }
52
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54
55
  private float computeAngle(float dx, float dy)
56
    {
57
    float PI = (float)Math.PI;
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 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)
69 b89898c5 Leszek Koltunski
    {
70
    pX = (0.5f+pX)*TEXTURE_HEIGHT;
71
    pY = (0.5f-pY)*TEXTURE_HEIGHT;
72
    cX = (0.5f+cX)*TEXTURE_HEIGHT;
73
    cY = (0.5f-cY)*TEXTURE_HEIGHT;
74
    nX = (0.5f+nX)*TEXTURE_HEIGHT;
75
    nY = (0.5f-nY)*TEXTURE_HEIGHT;
76
77 ae755eda Leszek Koltunski
    canvas.drawLine(left+pX,top+pY,left+cX,top+cY,paint);
78 b89898c5 Leszek Koltunski
79
    float aX = pX-cX;
80
    float aY = pY-cY;
81
    float bX = cX-nX;
82
    float bY = cY-nY;
83
84
    float aLen = (float)Math.sqrt(aX*aX+aY*aY);
85
    float bLen = (float)Math.sqrt(bX*bX+bY*bY);
86
87
    aX /= aLen;
88
    aY /= aLen;
89
    bX /= bLen;
90
    bY /= bLen;
91
92
    float sX = (aX-bX)/2;
93
    float sY = (aY-bY)/2;
94
    float sLen = (float)Math.sqrt(sX*sX+sY*sY);
95
    sX /= sLen;
96
    sY /= sLen;
97
98
    float startAngle = computeAngle(bX,-bY);
99
    float endAngle   = computeAngle(aX,-aY);
100
    float sweepAngle = endAngle-startAngle;
101
    if( sweepAngle<0 ) sweepAngle += 2*Math.PI;
102
103
    float R = r*TEXTURE_HEIGHT+stroke/2;
104
105
    float A = (float)(R/(Math.cos(sweepAngle/2)));
106
107
    float rX = cX + A*sX;
108
    float rY = cY + A*sY;
109
110
    startAngle *= 180/(Math.PI);
111
    sweepAngle *= 180/(Math.PI);
112
113 ae755eda Leszek Koltunski
    canvas.drawArc( left+rX-R, top+rY-R, left+rX+R, top+rY+R, startAngle, sweepAngle, false, paint);
114 b89898c5 Leszek Koltunski
    }
115
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117
118 ae755eda Leszek Koltunski
  void drawRoundedPolygon(Canvas canvas, Paint paint, int left, int top, float[] vertices, float stroke, int color, float r)
119 b89898c5 Leszek Koltunski
    {
120
    stroke *= TEXTURE_HEIGHT;
121
122
    paint.setAntiAlias(true);
123
    paint.setStrokeWidth(stroke);
124
    paint.setColor(color);
125
    paint.setStyle(Paint.Style.FILL);
126
127 ae755eda Leszek Koltunski
    canvas.drawRect(left,top,left+TEXTURE_HEIGHT,top+TEXTURE_HEIGHT,paint);
128 b89898c5 Leszek Koltunski
129
    paint.setColor(COLOR_BLACK);
130
    paint.setStyle(Paint.Style.STROKE);
131
132
    int length = vertices.length;
133
    int numVertices = length/2;
134
135
    float prevX = vertices[length-2];
136
    float prevY = vertices[length-1];
137
    float currX = vertices[0];
138
    float currY = vertices[1];
139
    float nextX = vertices[2];
140
    float nextY = vertices[3];
141
142
    for(int vert=0; vert<numVertices; vert++)
143
      {
144 ae755eda Leszek Koltunski
      drawCurrVertex(canvas, paint, left, top, r, stroke, prevX,prevY,currX,currY,nextX,nextY);
145 b89898c5 Leszek Koltunski
146
      prevX = currX;
147
      prevY = currY;
148
      currX = nextX;
149
      currY = nextY;
150
151
      if( 2*(vert+2)+1 < length )
152
        {
153
        nextX = vertices[2*(vert+2)  ];
154
        nextY = vertices[2*(vert+2)+1];
155
        }
156
      else
157
        {
158
        nextX = vertices[0];
159
        nextY = vertices[1];
160
        }
161
      }
162
    }
163 49cd8581 Leszek Koltunski
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165
166 9e5b990e Leszek Koltunski
  void drawIvyCornerSticker(Canvas canvas, Paint paint, int left, int top, int color, float stroke, float radius)
167 49cd8581 Leszek Koltunski
    {
168 9e5b990e Leszek Koltunski
    paint.setAntiAlias(true);
169 49cd8581 Leszek Koltunski
    paint.setColor(color);
170
    paint.setStyle(Paint.Style.FILL);
171
    canvas.drawRect(left,top,left+TEXTURE_HEIGHT,top+TEXTURE_HEIGHT,paint);
172
173 6a224bdc Leszek Koltunski
    paint.setColor(COLOR_BLACK);
174
    paint.setStyle(Paint.Style.STROKE);
175 886d1ebb Leszek Koltunski
    paint.setStrokeWidth(IVY_C*stroke*TEXTURE_HEIGHT);
176 9e5b990e Leszek Koltunski
177 886d1ebb Leszek Koltunski
    float tmp1 = ((IVY_D-0.5f)-IVY_M)*IVY_C;
178 9e5b990e Leszek Koltunski
    float cx1 = TEXTURE_HEIGHT*(tmp1 + 0.5f);
179
    float cy1 = TEXTURE_HEIGHT*(0.5f - tmp1);
180
181 886d1ebb Leszek Koltunski
    float halfL1 = IVY_C*TEXTURE_HEIGHT*(1.0f-2*IVY_D);
182 9e5b990e Leszek Koltunski
183
    canvas.drawArc( left+cx1-halfL1, top+cy1-halfL1, left+cx1+halfL1, top+cy1+halfL1, 270, 90, false, paint);
184
185 886d1ebb Leszek Koltunski
    float tmp2 = (+0.5f-IVY_M)*IVY_C;
186
    float tmp3 = (-0.5f-IVY_M)*IVY_C;
187 9e5b990e Leszek Koltunski
188
    float x0 = TEXTURE_HEIGHT*(+tmp2 + 0.5f);
189
    float y0 = TEXTURE_HEIGHT*(-tmp3 + 0.5f);
190
    float x1 = TEXTURE_HEIGHT*(+tmp2 + 0.5f);
191
    float y1 = TEXTURE_HEIGHT*(-tmp2 + 0.5f);
192
    float x2 = TEXTURE_HEIGHT*(+tmp3 + 0.5f);
193
    float y2 = TEXTURE_HEIGHT*(-tmp2 + 0.5f);
194
195
    canvas.drawLine(left+x0,top+y0,left+x1,top+y1,paint);
196
    canvas.drawLine(left+x1,top+y1,left+x2,top+y2,paint);
197
198 886d1ebb Leszek Koltunski
    float tmp4 = ((0.5f-stroke/2-radius/2)-IVY_M)*IVY_C;
199 9e5b990e Leszek Koltunski
    float cx2 = TEXTURE_HEIGHT*(tmp4 + 0.5f);
200
    float cy2 = TEXTURE_HEIGHT*(0.5f - tmp4);
201
202 886d1ebb Leszek Koltunski
    float halfL2 = IVY_C*TEXTURE_HEIGHT*radius;
203 9e5b990e Leszek Koltunski
204 886d1ebb Leszek Koltunski
    paint.setStrokeWidth(IVY_C*radius*TEXTURE_HEIGHT);
205 9e5b990e Leszek Koltunski
    canvas.drawArc( left+cx2-halfL2, top+cy2-halfL2, left+cx2+halfL2, top+cy2+halfL2, 270, 90, false, paint);
206 49cd8581 Leszek Koltunski
    }
207
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209
210 9e5b990e Leszek Koltunski
  void drawIvyCenterSticker(Canvas canvas, Paint paint, int left, int top, int color, float stroke, float radius)
211 49cd8581 Leszek Koltunski
    {
212 9e5b990e Leszek Koltunski
    paint.setAntiAlias(true);
213 49cd8581 Leszek Koltunski
    paint.setColor(color);
214
    paint.setStyle(Paint.Style.FILL);
215
    canvas.drawRect(left,top,left+TEXTURE_HEIGHT,top+TEXTURE_HEIGHT,paint);
216 6a224bdc Leszek Koltunski
217
    paint.setColor(COLOR_BLACK);
218
    paint.setStyle(Paint.Style.STROKE);
219
    paint.setStrokeWidth(stroke*TEXTURE_HEIGHT);
220
221 9e5b990e Leszek Koltunski
    float cx1 = TEXTURE_HEIGHT*IVY_D;
222
    float cy1 = TEXTURE_HEIGHT*(1-IVY_D);
223
    float cx2 = TEXTURE_HEIGHT*(1.0f-IVY_D);
224
    float cy2 = TEXTURE_HEIGHT*IVY_D;
225 6a224bdc Leszek Koltunski
226 9e5b990e Leszek Koltunski
    float halfL = TEXTURE_HEIGHT*(1.0f - 2*IVY_D);
227 6a224bdc Leszek Koltunski
228 9e5b990e Leszek Koltunski
    canvas.drawArc( left+cx1-halfL, top+cy1-halfL, left+cx1+halfL, top+cy1+halfL, 270, 90, false, paint);
229
    canvas.drawArc( left+cx2-halfL, top+cy2-halfL, left+cx2+halfL, top+cy2+halfL,  90, 90, false, paint);
230 886d1ebb Leszek Koltunski
231
    float tmp = TEXTURE_HEIGHT*(IVY_D+stroke*0.5f+radius*0.5f);
232
    float cx3 = tmp;
233
    float cy3 = tmp;
234
    float cx4 = TEXTURE_HEIGHT - cx3;
235
    float cy4 = TEXTURE_HEIGHT - cy3;
236
    float halfR = TEXTURE_HEIGHT*radius;
237
238
    paint.setStrokeWidth(radius*TEXTURE_HEIGHT);
239
    canvas.drawArc( left+cx3-halfR, top+cy3-halfR, left+cx3+halfR, top+cy3+halfR, 180, 90, false, paint);
240
    canvas.drawArc( left+cx4-halfR, top+cy4-halfR, left+cx4+halfR, top+cy4+halfR,   0, 90, false, paint);
241 49cd8581 Leszek Koltunski
    }
242 b89898c5 Leszek Koltunski
  }