Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / FactorySticker.java @ 9e5b990e

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

    
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30

    
31
class FactorySticker
32
  {
33
  private static FactorySticker mThis;
34

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

    
37
  private FactorySticker()
38
    {
39

    
40
    }
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

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

    
48
    return mThis;
49
    }
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
  private float computeAngle(float dx, float dy)
54
    {
55
    float PI = (float)Math.PI;
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 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)
67
    {
68
    pX = (0.5f+pX)*TEXTURE_HEIGHT;
69
    pY = (0.5f-pY)*TEXTURE_HEIGHT;
70
    cX = (0.5f+cX)*TEXTURE_HEIGHT;
71
    cY = (0.5f-cY)*TEXTURE_HEIGHT;
72
    nX = (0.5f+nX)*TEXTURE_HEIGHT;
73
    nY = (0.5f-nY)*TEXTURE_HEIGHT;
74

    
75
    canvas.drawLine(left+pX,top+pY,left+cX,top+cY,paint);
76

    
77
    float aX = pX-cX;
78
    float aY = pY-cY;
79
    float bX = cX-nX;
80
    float bY = cY-nY;
81

    
82
    float aLen = (float)Math.sqrt(aX*aX+aY*aY);
83
    float bLen = (float)Math.sqrt(bX*bX+bY*bY);
84

    
85
    aX /= aLen;
86
    aY /= aLen;
87
    bX /= bLen;
88
    bY /= bLen;
89

    
90
    float sX = (aX-bX)/2;
91
    float sY = (aY-bY)/2;
92
    float sLen = (float)Math.sqrt(sX*sX+sY*sY);
93
    sX /= sLen;
94
    sY /= sLen;
95

    
96
    float startAngle = computeAngle(bX,-bY);
97
    float endAngle   = computeAngle(aX,-aY);
98
    float sweepAngle = endAngle-startAngle;
99
    if( sweepAngle<0 ) sweepAngle += 2*Math.PI;
100

    
101
    float R = r*TEXTURE_HEIGHT+stroke/2;
102

    
103
    float A = (float)(R/(Math.cos(sweepAngle/2)));
104

    
105
    float rX = cX + A*sX;
106
    float rY = cY + A*sY;
107

    
108
    startAngle *= 180/(Math.PI);
109
    sweepAngle *= 180/(Math.PI);
110

    
111
    canvas.drawArc( left+rX-R, top+rY-R, left+rX+R, top+rY+R, startAngle, sweepAngle, false, paint);
112
    }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

    
116
  void drawRoundedPolygon(Canvas canvas, Paint paint, int left, int top, float[] vertices, float stroke, int color, float r)
117
    {
118
    stroke *= TEXTURE_HEIGHT;
119

    
120
    paint.setAntiAlias(true);
121
    paint.setStrokeWidth(stroke);
122
    paint.setColor(color);
123
    paint.setStyle(Paint.Style.FILL);
124

    
125
    canvas.drawRect(left,top,left+TEXTURE_HEIGHT,top+TEXTURE_HEIGHT,paint);
126

    
127
    paint.setColor(COLOR_BLACK);
128
    paint.setStyle(Paint.Style.STROKE);
129

    
130
    int length = vertices.length;
131
    int numVertices = length/2;
132

    
133
    float prevX = vertices[length-2];
134
    float prevY = vertices[length-1];
135
    float currX = vertices[0];
136
    float currY = vertices[1];
137
    float nextX = vertices[2];
138
    float nextY = vertices[3];
139

    
140
    for(int vert=0; vert<numVertices; vert++)
141
      {
142
      drawCurrVertex(canvas, paint, left, top, r, stroke, prevX,prevY,currX,currY,nextX,nextY);
143

    
144
      prevX = currX;
145
      prevY = currY;
146
      currX = nextX;
147
      currY = nextY;
148

    
149
      if( 2*(vert+2)+1 < length )
150
        {
151
        nextX = vertices[2*(vert+2)  ];
152
        nextY = vertices[2*(vert+2)+1];
153
        }
154
      else
155
        {
156
        nextX = vertices[0];
157
        nextY = vertices[1];
158
        }
159
      }
160
    }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163

    
164
  void drawIvyCornerSticker(Canvas canvas, Paint paint, int left, int top, int color, float stroke, float radius)
165
    {
166
    paint.setAntiAlias(true);
167
    paint.setColor(color);
168
    paint.setStyle(Paint.Style.FILL);
169
    canvas.drawRect(left,top,left+TEXTURE_HEIGHT,top+TEXTURE_HEIGHT,paint);
170

    
171
    float CORR = 0.5f;
172
    float DIST = 0.4f;
173

    
174
    paint.setColor(COLOR_BLACK);
175
    paint.setStyle(Paint.Style.STROKE);
176
    paint.setStrokeWidth(CORR*stroke*TEXTURE_HEIGHT);
177

    
178
    float tmp1 = ((IVY_D-0.5f)-DIST)*CORR;
179
    float cx1 = TEXTURE_HEIGHT*(tmp1 + 0.5f);
180
    float cy1 = TEXTURE_HEIGHT*(0.5f - tmp1);
181

    
182
    float halfL1 = CORR*TEXTURE_HEIGHT*(1.0f-2*IVY_D);
183

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

    
186
    float tmp2 = (+0.5f-DIST)*CORR;
187
    float tmp3 = (-0.5f-DIST)*CORR;
188

    
189
    float x0 = TEXTURE_HEIGHT*(+tmp2 + 0.5f);
190
    float y0 = TEXTURE_HEIGHT*(-tmp3 + 0.5f);
191
    float x1 = TEXTURE_HEIGHT*(+tmp2 + 0.5f);
192
    float y1 = TEXTURE_HEIGHT*(-tmp2 + 0.5f);
193
    float x2 = TEXTURE_HEIGHT*(+tmp3 + 0.5f);
194
    float y2 = TEXTURE_HEIGHT*(-tmp2 + 0.5f);
195

    
196
    canvas.drawLine(left+x0,top+y0,left+x1,top+y1,paint);
197
    canvas.drawLine(left+x1,top+y1,left+x2,top+y2,paint);
198

    
199
    float tmp4 = ((0.5f-stroke/2-radius/2)-DIST)*CORR;
200
    float cx2 = TEXTURE_HEIGHT*(tmp4 + 0.5f);
201
    float cy2 = TEXTURE_HEIGHT*(0.5f - tmp4);
202

    
203
    float halfL2 = CORR*TEXTURE_HEIGHT*radius;
204

    
205
    paint.setStrokeWidth(CORR*radius*TEXTURE_HEIGHT);
206
    canvas.drawArc( left+cx2-halfL2, top+cy2-halfL2, left+cx2+halfL2, top+cy2+halfL2, 270, 90, false, paint);
207
    }
208

    
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210

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

    
218
    paint.setColor(COLOR_BLACK);
219
    paint.setStyle(Paint.Style.STROKE);
220
    paint.setStrokeWidth(stroke*TEXTURE_HEIGHT);
221

    
222
    float cx1 = TEXTURE_HEIGHT*IVY_D;
223
    float cy1 = TEXTURE_HEIGHT*(1-IVY_D);
224
    float cx2 = TEXTURE_HEIGHT*(1.0f-IVY_D);
225
    float cy2 = TEXTURE_HEIGHT*IVY_D;
226

    
227
    float halfL = TEXTURE_HEIGHT*(1.0f - 2*IVY_D);
228

    
229
    canvas.drawArc( left+cx1-halfL, top+cy1-halfL, left+cx1+halfL, top+cy1+halfL, 270, 90, false, paint);
230
    canvas.drawArc( left+cx2-halfL, top+cy2-halfL, left+cx2+halfL, top+cy2+halfL,  90, 90, false, paint);
231
    }
232
  }
(3-3/24)