Last active
May 21, 2017 16:49
-
-
Save kylepls/e1c28ee4d9773c499aa4cc417aaf0b27 to your computer and use it in GitHub Desktop.
I made a fidget spinner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package loops; | |
import java.awt.*; | |
import java.util.Arrays; | |
import java.util.List; | |
/** | |
* Created by Kyle on 5/20/2017. | |
*/ | |
public class Spinner { | |
private static char spinnerChar = '\u07f7'; | |
private static String spaceChar = " " + '\u2006' + '\u2006'; | |
private static List<String> drawing = Arrays.asList(" - ", | |
" - ", | |
" - ", | |
"-------", | |
" - ", | |
" - ", | |
" - "); | |
public static void main(String[] args) throws InterruptedException { | |
int steps = 12; | |
double step = 180 / steps; | |
int i = 0; | |
while (true) { | |
String spin = spin(step * i); | |
System.out.println(spin); | |
i++; | |
if (i >= steps) { | |
i = 1; | |
} | |
Thread.sleep(100); | |
} | |
} | |
private static String spin(double degreeOff) { | |
boolean[][] booleans = stringToArray(drawing); | |
int[] origin = new int[]{(booleans[0].length - 1) / 2, (booleans.length - 1) / 2}; | |
double radOffset = degreeOff; | |
boolean[][] output = new boolean[booleans.length][booleans[0].length]; | |
for (int y = 0; y < booleans.length; y++) { | |
for (int x = 0; x < booleans[0].length; x++) { | |
if (booleans[y][x]) { | |
int[] detlaOrgin = getDetlaOrgin(x, y, origin); | |
Point spin = spin(radOffset, detlaOrgin[0], detlaOrgin[1]); | |
int[] real = getReal(spin.x, spin.y, origin); | |
output[real[0]][real[1]] = true; | |
} | |
} | |
} | |
return toString(output); | |
} | |
private static int[] getReal(int dx, int dy, int[] origin) { | |
return new int[]{origin[0] + dx, origin[1] + dy}; | |
} | |
private static int[] getDetlaOrgin(int x, int y, int[] origin) { | |
return new int[]{origin[0] - x, origin[1] - y}; | |
} | |
private static Point spin(double radOffset, int x, int y) { | |
double dx = Math.cos(radOffset) * x - Math.sin(radOffset) * y; | |
double dy = Math.sin(radOffset) * x + Math.cos(radOffset) * y; | |
return new Point((int) Math.round(dx), (int) Math.round(dy)); | |
} | |
private static boolean[][] stringToArray(List<String> lines) { | |
boolean[][] bools = new boolean[lines.get(0).length()][lines.size()]; | |
for (int y = 0; y < lines.size(); y++) { | |
String line = lines.get(y); | |
char[] charArray = line.toCharArray(); | |
for (int x = 0; x < charArray.length; x++) { | |
char c = charArray[x]; | |
if (c != ' ') { | |
bools[x][y] = true; | |
} | |
} | |
} | |
return bools; | |
} | |
private static String toString(boolean[][] booleans) { | |
StringBuilder sb = new StringBuilder(); | |
for (int y = 0; y < booleans.length; y++) { | |
for (int x = 0; x < booleans[0].length; x++) { | |
if (booleans[y][x]) { | |
sb.append(spinnerChar); | |
} else { | |
sb.append(spaceChar); | |
} | |
} | |
sb.append("\n"); | |
} | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment