Project

General

Profile

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

magiccube / src / main / java / org / distorted / object / RubikCube.java @ a10ada2a

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 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.object;
21

    
22
import android.graphics.Bitmap;
23
import android.graphics.Canvas;
24
import android.graphics.Paint;
25

    
26
import org.distorted.library.main.DistortedEffects;
27
import org.distorted.library.main.DistortedTexture;
28
import org.distorted.library.mesh.MeshCubes;
29
import org.distorted.library.mesh.MeshRectangles;
30
import org.distorted.library.type.Static3D;
31
import org.distorted.library.type.Static4D;
32

    
33
import static org.distorted.object.RubikObjectList.VECTX;
34
import static org.distorted.object.RubikObjectList.VECTY;
35
import static org.distorted.object.RubikObjectList.VECTZ;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

    
39
class RubikCube extends RubikObject
40
{
41
  private static final Static4D mapFront, mapBack, mapLeft, mapRight, mapTop, mapBottom, mapBlack;
42

    
43
  static
44
    {
45
    // 3x2 bitmap = 6 squares:
46
    //
47
    // RED     GREEN   BLUE
48
    // YELLOW  WHITE   BROWN
49

    
50
    final float ze = 0.0f;
51
    final float ot = 1.0f/3.0f;
52
    final float tt = 2.0f/3.0f;
53
    final float oh = 1.0f/2.0f;
54
    final float of = 1.0f/40.0f;
55

    
56
    mapFront = new Static4D(ze,oh, ze+ot,oh+oh);
57
    mapBack  = new Static4D(tt,ze, tt+ot,ze+oh);
58
    mapLeft  = new Static4D(ot,ze, ot+ot,ze+oh);
59
    mapRight = new Static4D(ze,ze, ze+ot,ze+oh);
60
    mapTop   = new Static4D(tt,oh, tt+ot,oh+oh);
61
    mapBottom= new Static4D(ot,oh, ot+ot,oh+oh);
62

    
63
    mapBlack = new Static4D(ze,ze, ze+of,ze+of);
64
    }
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67
// i.e. size^3 - (size-2)^3 - number of cubits in the outside wall of the Cube (we dont create or
68
// render the inside0
69

    
70
  int getNumCubits(int size)
71
    {
72
    return 6*size*size - 12*size + 8;
73
    }
74

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

    
77
  int[][] getCubitPositions(int size)
78
    {
79
    int[][] tmp = new int[getNumCubits(size)][3];
80

    
81
    int currentPosition = 0;
82

    
83
    for(int x = 0; x<size; x++)
84
      for(int y = 0; y<size; y++)
85
        for(int z = 0; z<size; z++)
86
          {
87
          if( x==0 || x==size-1 || y==0 || y==size-1 || z==0 || z==size-1 )
88
            {
89
            tmp[currentPosition][0] = x;
90
            tmp[currentPosition][1] = y;
91
            tmp[currentPosition][2] = z;
92

    
93
            currentPosition++;
94
            }
95
          }
96

    
97
    return tmp;
98
    }
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101
// All legal rotation quats of a RubikCube of any size must have all four of their components
102
// equal to either 0, 1, -1, 0.5, -0.5 or +-sqrt(2)/2.
103

    
104
  float[] getLegalQuats()
105
    {
106
    final float SQ2 = 0.5f*((float)Math.sqrt(2));
107
    return new float[] { 0.0f , 0.5f , -0.5f , 1.0f , -1.0f , SQ2 , -SQ2 };
108
    }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

    
112
  boolean belongsToRotation( Static3D currentPosition, int vector, int row)
113
    {
114
    switch(vector)
115
      {
116
      case VECTX: return currentPosition.get0()==row;
117
      case VECTY: return currentPosition.get1()==row;
118
      case VECTZ: return currentPosition.get2()==row;
119
      }
120

    
121
    return false;
122
    }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

    
126
  RubikCube(int size, Static4D quatCur, Static4D quatAcc, DistortedTexture texture, MeshRectangles mesh, DistortedEffects effects)
127
    {
128
    super(size,quatCur,quatAcc,texture,mesh,effects);
129
    }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
// PUBLIC API
133

    
134
  public void createTexture()
135
    {
136
    Bitmap bitmap;
137

    
138
    final int S = 128;
139
    final int W = 3*S;
140
    final int H = 2*S;
141
    final int R = S/10;
142
    final int M = S/20;
143

    
144
    Paint paint = new Paint();
145
    bitmap = Bitmap.createBitmap(W,H, Bitmap.Config.ARGB_8888);
146
    Canvas canvas = new Canvas(bitmap);
147

    
148
    paint.setAntiAlias(true);
149
    paint.setTextAlign(Paint.Align.CENTER);
150
    paint.setStyle(Paint.Style.FILL);
151

    
152
    // 3x2 bitmap = 6 squares:
153
    //
154
    // RED     GREEN   BLUE
155
    // YELLOW  WHITE   BROWN
156

    
157
    paint.setColor(0xff000000);                                  // BLACK BACKGROUND
158
    canvas.drawRect(0, 0, W, H, paint);                          //
159

    
160
    paint.setColor(0xffff0000);                                  // RED
161
    canvas.drawRoundRect(    M,   M,   S-M,   S-M, R, R, paint); //
162
    paint.setColor(0xff00ff00);                                  // GREEN
163
    canvas.drawRoundRect(  S+M,   M, 2*S-M,   S-M, R, R, paint); //
164
    paint.setColor(0xff0000ff);                                  // BLUE
165
    canvas.drawRoundRect(2*S+M,   M, 3*S-M,   S-M, R, R, paint); //
166
    paint.setColor(0xffffff00);                                  // YELLOW
167
    canvas.drawRoundRect(    M, S+M,   S-M, 2*S-M, R, R, paint); //
168
    paint.setColor(0xffffffff);                                  // WHITE
169
    canvas.drawRoundRect(  S+M, S+M, 2*S-M, 2*S-M, R, R, paint); //
170
    paint.setColor(0xffb5651d);                                  // BROWN
171
    canvas.drawRoundRect(2*S+M, S+M, 3*S-M, 2*S-M, R, R, paint); //
172

    
173
    mTexture.setTexture(bitmap);
174
    }
175

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

    
178
  MeshCubes createMesh(int vertices,int x, int y, int z)
179
    {
180
    Static4D tmpLeft  = (x==       0 ? mapLeft  :mapBlack);
181
    Static4D tmpRight = (x== mSize-1 ? mapRight :mapBlack);
182
    Static4D tmpFront = (z== mSize-1 ? mapFront :mapBlack);
183
    Static4D tmpBack  = (z==       0 ? mapBack  :mapBlack);
184
    Static4D tmpTop   = (y== mSize-1 ? mapTop   :mapBlack);
185
    Static4D tmpBottom= (y==       0 ? mapBottom:mapBlack);
186

    
187
    return new MeshCubes(vertices,vertices,vertices, tmpFront, tmpBack, tmpLeft, tmpRight, tmpTop, tmpBottom);
188
    }
189
}
(2-2/6)