Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

now working with the latest update of Rust #30

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions button_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
{
"clearCanvas": {
"x": 38,
"y": 79,
"valid": true
},
"saveToDesktop": {
"x": 97,
"y": 81,
"valid": true
},
"saveImage": {
"x": 1501,
"y": 932,
"valid": true
},
"clearRotation": {
"x": 257,
"y": 86,
"valid": true
},
"tool_paintBrush": {
"x": 1558,
"y": 177,
"valid": true
},
"brush_circle": {
"x": 1571,
"y": 245,
"valid": true
},
"brush_square": {
"x": 1558,
"y": 294,
"valid": true
},
"size_1": {
"x": 1568,
"y": 279,
"valid": true
},
"size_32": {
"x": 1667,
"y": 275,
"valid": true
},
"opacity_0": {
"x": 1568,
"y": 325,
"valid": true
},
"opacity_1": {
"x": 1665,
"y": 326,
"valid": true
},
"color_topLeft": {
"x": 1478,
"y": 403,
"valid": true
},
"color_botRight": {
"x": 1698,
"y": 836,
"valid": true
},
"focus": {
"x": 1419,
"y": 941,
"valid": true
},
"colorPreview": {
"x": 1419,
"y": 941,
"valid": true
}
}
1 change: 1 addition & 0 deletions circle_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"CIRCLE_0":8,"CIRCLE_1":16,"CIRCLE_2":26,"CIRCLE_3":36,"CIRCLE_4":46,"CIRCLE_5":56,"CIRCLE_6":70}
137 changes: 90 additions & 47 deletions src/main/java/com/bobrust/generator/CircleCache.java
Original file line number Diff line number Diff line change
@@ -1,82 +1,125 @@
package com.bobrust.generator;

class CircleCache {
private static final Scanline[] CIRCLE_0 = generateCircle(1);
private static final Scanline[] CIRCLE_1 = generateCircle(4);
private static final Scanline[] CIRCLE_2 = generateCircle(8);
private static final Scanline[] CIRCLE_3 = generateCircle(12);
private static final Scanline[] CIRCLE_4 = generateCircle(19);
private static final Scanline[] CIRCLE_5 = generateCircle(26); // 25 // Might be larger

public static final Scanline[][] CIRCLE_CACHE = { CIRCLE_0, CIRCLE_1, CIRCLE_2, CIRCLE_3, CIRCLE_4, CIRCLE_5 };
import com.bobrust.robot.BobRustPainter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

class CircleCache {
private static final String CIRCLE_CONFIG_PATH = "circle_config.json";
private static Map<String, Integer> circleSizes = new TreeMap<>();
private static final Logger LOGGER = LogManager.getLogger(BobRustPainter.class);


private static Scanline[] CIRCLE_0 ;
private static Scanline[] CIRCLE_1 ;
private static Scanline[] CIRCLE_2 ;
private static Scanline[] CIRCLE_3 ;
private static Scanline[] CIRCLE_4 ;
private static Scanline[] CIRCLE_5;
//private static Scanline[] CIRCLE_6;
public static Scanline[][] CIRCLE_CACHE ;
public static final int[] CIRCLE_CACHE_LENGTH;

static {

public static int CIRCLE_0_VALUE = 3;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these should be final

public static int CIRCLE_1_VALUE = 6;
public static int CIRCLE_2_VALUE = 12;
public static int CIRCLE_3_VALUE = 25;
public static int CIRCLE_4_VALUE = 50;
public static int CIRCLE_5_VALUE =100;

static { loadCircleSizes();
CIRCLE_0 = generateCircle(circleSizes.getOrDefault("CIRCLE_0", CIRCLE_0_VALUE));
CIRCLE_1 = generateCircle(circleSizes.getOrDefault("CIRCLE_1", CIRCLE_1_VALUE));
CIRCLE_2 = generateCircle(circleSizes.getOrDefault("CIRCLE_2", CIRCLE_2_VALUE));
CIRCLE_3 = generateCircle(circleSizes.getOrDefault("CIRCLE_3", CIRCLE_3_VALUE));
CIRCLE_4 = generateCircle(circleSizes.getOrDefault("CIRCLE_4", CIRCLE_4_VALUE));
CIRCLE_5 = generateCircle(circleSizes.getOrDefault("CIRCLE_5", CIRCLE_5_VALUE));
//CIRCLE_6 = generateCircle(circleSizes.getOrDefault("CIRCLE_6", 70));
CIRCLE_CACHE = new Scanline[][]{CIRCLE_0, CIRCLE_1, CIRCLE_2, CIRCLE_3, CIRCLE_4, CIRCLE_5
// ,CIRCLE_6
};
CIRCLE_CACHE_LENGTH = new int[CIRCLE_CACHE.length];
for (int i = 0; i < CIRCLE_CACHE.length; i++) {
CIRCLE_CACHE_LENGTH[i] = CIRCLE_CACHE[i].length;
}

}

/*public static void main(String[] args) {
// 1, 4, 8, 12, 19, 25
// 18 full match but 19 matches area
// 24 full match but 25 matches area
generateCircle(24);
}*/


private static Scanline[] generateCircle(int size) {
LOGGER.info("circle size " +size);
boolean[] grid = new boolean[size * size];
for (int i = 0; i < size * size; i++) {
double px = (int) (i % size) + 0.5;
double py = (int) (i / size) + 0.5;
double x = (px / (double) size) * 2.0 - 1;
double y = (py / (double) size) * 2.0 - 1;
double magnitudeSqr = x*x + y*y;

double magnitudeSqr = x * x + y * y;
grid[i] = magnitudeSqr <= 1;
}

/*{
BufferedImage bi = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
int[] pixels = ((DataBufferInt) bi.getRaster().getDataBuffer()).getData();
for (int i = 0; i < grid.length; i++) {
pixels[i] = grid[i] ? 0x000000 : 0xffffff;
}

DebugUtil.debugShowImage(bi, 13);
}*/


Scanline[] scanlines = new Scanline[size];
for (int i = 0; i < size; i++) {
int start = size;
int end = 0;
for (int j = 0; j < size; j++) {
if (grid[i * size + j]) {
start = Math.min(start, j);
end = Math.max(end, j );
end = Math.max(end, j);
}
}

if (start <= end) {
int off = size / 2;
scanlines[i] = new Scanline(i - off, start - off, end - off);
}
}

/*{
int off = size / 2;
BufferedImage bi = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
int[] pixels = ((DataBufferInt) bi.getRaster().getDataBuffer()).getData();
Arrays.fill(pixels, 0xffffff);
for (Scanline line : scanlines) {
for (int x = line.x1; x <= line.x2; x++) {
pixels[(line.y + off) * size + (x + off)] = 0x000000;
return scanlines;
}

private static void loadCircleSizes() {
if (Files.exists(Paths.get(CIRCLE_CONFIG_PATH))) {
try (FileReader reader = new FileReader(CIRCLE_CONFIG_PATH)) {
Gson gson = new Gson();
Type mapType = new TypeToken<Map<String, Integer>>() {}.getType();
circleSizes = gson.fromJson(reader, mapType);
for (Map.Entry<String, Integer> entry : circleSizes.entrySet()) {
LOGGER.info("circle: " + entry.getKey() + ". Value loaded: " + entry.getValue());
}
} catch (IOException e) {
e.printStackTrace();
}
DebugUtil.debugShowImage(bi, 13);
}*/

return scanlines;
} else {
circleSizes.put("CIRCLE_0", CIRCLE_0_VALUE);
circleSizes.put("CIRCLE_1", CIRCLE_1_VALUE);
circleSizes.put("CIRCLE_2", CIRCLE_2_VALUE);
circleSizes.put("CIRCLE_3", CIRCLE_3_VALUE);
circleSizes.put("CIRCLE_4", CIRCLE_4_VALUE);
circleSizes.put("CIRCLE_5", CIRCLE_5_VALUE);
//circleSizes.put("CIRCLE_6", 70);
saveDefaultCircleSizes();
}
}

private static void saveDefaultCircleSizes() {
try (FileWriter writer = new FileWriter(CIRCLE_CONFIG_PATH)) {
Gson gson = new Gson();
gson.toJson(circleSizes, writer);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Loading