Project

General

Profile

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

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

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.MeshBase;
29
import org.distorted.library.mesh.MeshCubes;
30
import org.distorted.library.mesh.MeshRectangles;
31
import org.distorted.library.type.Static3D;
32
import org.distorted.library.type.Static4D;
33

    
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35

    
36
class RubikCube extends RubikObject
37
{
38
  int[][] getCubitPositions(int size)
39
    {
40
    int[][] tmp = new int[getNumCubits(size)][3];
41

    
42
    int currentPosition = 0;
43

    
44
    for(int x = 0; x<size; x++)
45
      for(int y = 0; y<size; y++)
46
        for(int z = 0; z<size; z++)
47
          {
48
          if( x==0 || x==size-1 || y==0 || y==size-1 || z==0 || z==size-1 )
49
            {
50
            tmp[currentPosition][0] = x;
51
            tmp[currentPosition][1] = y;
52
            tmp[currentPosition][2] = z;
53

    
54
            currentPosition++;
55
            }
56
          }
57

    
58
    return tmp;
59
    }
60

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

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

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71
// All legal rotation quats of a RubikCube of any size must have all four of their components
72
// equal to either 0, 1, -1, 0.5, -0.5 or +-sqrt(2)/2.
73

    
74
  float[] getLegalQuats()
75
    {
76
    final float SQ2 = 0.5f*((float)Math.sqrt(2));
77
    return new float[] { 0.0f , 0.5f , -0.5f , 1.0f , -1.0f , SQ2 , -SQ2 };
78
    }
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81

    
82
  boolean belongsToRotation( Static3D currentPosition, Static3D axis, int row)
83
    {
84
    if( axis.get0()!=0 ) return currentPosition.get0()==row;
85
    if( axis.get1()!=0 ) return currentPosition.get1()==row;
86
    if( axis.get2()!=0 ) return currentPosition.get2()==row;
87

    
88
    return false;
89
    }
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

    
93
  RubikCube(int size, Static4D quatCur, Static4D quatAcc, DistortedTexture texture, MeshRectangles mesh, DistortedEffects effects)
94
    {
95
    super(size,quatCur,quatAcc,texture,mesh,effects);
96
    }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99
// PUBLIC API
100

    
101
  public void createTexture()
102
    {
103
    Bitmap bitmap;
104

    
105
    final int S = 128;
106
    final int W = 3*S;
107
    final int H = 2*S;
108
    final int R = S/10;
109
    final int M = S/20;
110

    
111
    Paint paint = new Paint();
112
    bitmap = Bitmap.createBitmap(W,H, Bitmap.Config.ARGB_8888);
113
    Canvas canvas = new Canvas(bitmap);
114

    
115
    paint.setAntiAlias(true);
116
    paint.setTextAlign(Paint.Align.CENTER);
117
    paint.setStyle(Paint.Style.FILL);
118

    
119
    // 3x2 bitmap = 6 squares:
120
    //
121
    // RED     GREEN   BLUE
122
    // YELLOW  WHITE   BROWN
123

    
124
    paint.setColor(0xff000000);                                  // BLACK BACKGROUND
125
    canvas.drawRect(0, 0, W, H, paint);                          //
126

    
127
    paint.setColor(0xffff0000);                                  // RED
128
    canvas.drawRoundRect(    M,   M,   S-M,   S-M, R, R, paint); //
129
    paint.setColor(0xff00ff00);                                  // GREEN
130
    canvas.drawRoundRect(  S+M,   M, 2*S-M,   S-M, R, R, paint); //
131
    paint.setColor(0xff0000ff);                                  // BLUE
132
    canvas.drawRoundRect(2*S+M,   M, 3*S-M,   S-M, R, R, paint); //
133
    paint.setColor(0xffffff00);                                  // YELLOW
134
    canvas.drawRoundRect(    M, S+M,   S-M, 2*S-M, R, R, paint); //
135
    paint.setColor(0xffffffff);                                  // WHITE
136
    canvas.drawRoundRect(  S+M, S+M, 2*S-M, 2*S-M, R, R, paint); //
137
    paint.setColor(0xffb5651d);                                  // BROWN
138
    canvas.drawRoundRect(2*S+M, S+M, 3*S-M, 2*S-M, R, R, paint); //
139

    
140
    mTexture.setTexture(bitmap);
141
    }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

    
145
  private static final Static4D mapFront, mapBack, mapLeft, mapRight, mapTop, mapBottom, mapBlack;
146

    
147
  static
148
    {
149
    // 3x2 bitmap = 6 squares:
150
    //
151
    // RED     GREEN   BLUE
152
    // YELLOW  WHITE   BROWN
153

    
154
    final float ze = 0.0f;
155
    final float ot = 1.0f/3.0f;
156
    final float tt = 2.0f/3.0f;
157
    final float oh = 1.0f/2.0f;
158
    final float of = 1.0f/40.0f;
159

    
160
    mapFront = new Static4D(ze,oh, ze+ot,oh+oh);
161
    mapBack  = new Static4D(tt,ze, tt+ot,ze+oh);
162
    mapLeft  = new Static4D(ot,ze, ot+ot,ze+oh);
163
    mapRight = new Static4D(ze,ze, ze+ot,ze+oh);
164
    mapTop   = new Static4D(tt,oh, tt+ot,oh+oh);
165
    mapBottom= new Static4D(ot,oh, ot+ot,oh+oh);
166

    
167
    mapBlack = new Static4D(ze,ze, ze+of,ze+of);
168
    }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

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

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