Project

General

Profile

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

magiccube / src / main / java / org / distorted / object / RubikCube.java @ 3c4a326c

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
///////////////////////////////////////////////////////////////////////////////////////////////////
34

    
35
class RubikCube extends RubikObject
36
{
37
  private static final Static4D mapFront, mapBack, mapLeft, mapRight, mapTop, mapBottom, mapBlack;
38

    
39
  static
40
    {
41
    // 3x2 bitmap = 6 squares:
42
    //
43
    // RED     GREEN   BLUE
44
    // YELLOW  WHITE   BROWN
45

    
46
    final float ze = 0.0f;
47
    final float ot = 1.0f/3.0f;
48
    final float tt = 2.0f/3.0f;
49
    final float oh = 1.0f/2.0f;
50
    final float of = 1.0f/40.0f;
51

    
52
    mapFront = new Static4D(ze,oh, ze+ot,oh+oh);
53
    mapBack  = new Static4D(tt,ze, tt+ot,ze+oh);
54
    mapLeft  = new Static4D(ot,ze, ot+ot,ze+oh);
55
    mapRight = new Static4D(ze,ze, ze+ot,ze+oh);
56
    mapTop   = new Static4D(tt,oh, tt+ot,oh+oh);
57
    mapBottom= new Static4D(ot,oh, ot+ot,oh+oh);
58

    
59
    mapBlack = new Static4D(ze,ze, ze+of,ze+of);
60
    }
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63
// i.e. size^3 - (size-2)^3 - number of cubits in the outside wall of the Cube (we don't create or
64
// render the inside)
65

    
66
  int getNumCubits(int size)
67
    {
68
    return size>1 ? 6*size*size - 12*size + 8 : 1;
69
    }
70

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

    
73
  int[][] getCubitPositions(int size)
74
    {
75
    int[][] tmp = new int[getNumCubits(size)][3];
76

    
77
    int currentPosition = 0;
78

    
79
    for(int x = 0; x<size; x++)
80
      for(int y = 0; y<size; y++)
81
        for(int z = 0; z<size; z++)
82
          {
83
          if( x==0 || x==size-1 || y==0 || y==size-1 || z==0 || z==size-1 )
84
            {
85
            tmp[currentPosition][0] = x;
86
            tmp[currentPosition][1] = y;
87
            tmp[currentPosition][2] = z;
88

    
89
            currentPosition++;
90
            }
91
          }
92

    
93
    return tmp;
94
    }
95

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97
// All legal rotation quats of a RubikCube of any size must have all four of their components
98
// equal to either 0, 1, -1, 0.5, -0.5 or +-sqrt(2)/2.
99

    
100
  float[] getLegalQuats()
101
    {
102
    final float SQ2 = 0.5f*((float)Math.sqrt(2));
103
    return new float[] { 0.0f , 0.5f , -0.5f , 1.0f , -1.0f , SQ2 , -SQ2 };
104
    }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

    
108
  boolean belongsToRotation( Static3D currentPosition, Static3D axis, int row)
109
    {
110
    if( axis.get0()!=0 ) return currentPosition.get0()==row;
111
    if( axis.get1()!=0 ) return currentPosition.get1()==row;
112
    if( axis.get2()!=0 ) return currentPosition.get2()==row;
113

    
114
    return false;
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
  RubikCube(int size, Static4D quatCur, Static4D quatAcc, DistortedTexture texture, MeshRectangles mesh, DistortedEffects effects)
120
    {
121
    super(size,quatCur,quatAcc,texture,mesh,effects);
122
    }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125
// PUBLIC API
126

    
127
  public void createTexture()
128
    {
129
    Bitmap bitmap;
130

    
131
    final int S = 128;
132
    final int W = 3*S;
133
    final int H = 2*S;
134
    final int R = S/10;
135
    final int M = S/20;
136

    
137
    Paint paint = new Paint();
138
    bitmap = Bitmap.createBitmap(W,H, Bitmap.Config.ARGB_8888);
139
    Canvas canvas = new Canvas(bitmap);
140

    
141
    paint.setAntiAlias(true);
142
    paint.setTextAlign(Paint.Align.CENTER);
143
    paint.setStyle(Paint.Style.FILL);
144

    
145
    // 3x2 bitmap = 6 squares:
146
    //
147
    // RED     GREEN   BLUE
148
    // YELLOW  WHITE   BROWN
149

    
150
    paint.setColor(0xff000000);                                  // BLACK BACKGROUND
151
    canvas.drawRect(0, 0, W, H, paint);                          //
152

    
153
    paint.setColor(0xffff0000);                                  // RED
154
    canvas.drawRoundRect(    M,   M,   S-M,   S-M, R, R, paint); //
155
    paint.setColor(0xff00ff00);                                  // GREEN
156
    canvas.drawRoundRect(  S+M,   M, 2*S-M,   S-M, R, R, paint); //
157
    paint.setColor(0xff0000ff);                                  // BLUE
158
    canvas.drawRoundRect(2*S+M,   M, 3*S-M,   S-M, R, R, paint); //
159
    paint.setColor(0xffffff00);                                  // YELLOW
160
    canvas.drawRoundRect(    M, S+M,   S-M, 2*S-M, R, R, paint); //
161
    paint.setColor(0xffffffff);                                  // WHITE
162
    canvas.drawRoundRect(  S+M, S+M, 2*S-M, 2*S-M, R, R, paint); //
163
    paint.setColor(0xffb5651d);                                  // BROWN
164
    canvas.drawRoundRect(2*S+M, S+M, 3*S-M, 2*S-M, R, R, paint); //
165

    
166
    mTexture.setTexture(bitmap);
167
    }
168

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

    
171
  MeshCubes createMesh(int vertices,int x, int y, int z)
172
    {
173
    Static4D tmpLeft  = (x==       0 ? mapLeft  :mapBlack);
174
    Static4D tmpRight = (x== mSize-1 ? mapRight :mapBlack);
175
    Static4D tmpFront = (z== mSize-1 ? mapFront :mapBlack);
176
    Static4D tmpBack  = (z==       0 ? mapBack  :mapBlack);
177
    Static4D tmpTop   = (y== mSize-1 ? mapTop   :mapBlack);
178
    Static4D tmpBottom= (y==       0 ? mapBottom:mapBlack);
179

    
180
    return new MeshCubes(vertices,vertices,vertices, tmpFront, tmpBack, tmpLeft, tmpRight, tmpTop, tmpBottom);
181
    }
182
}
(2-2/6)