119 lines
2.8 KiB
Plaintext
119 lines
2.8 KiB
Plaintext
final String imgPath = "IMAGE.png";
|
|
final int gridLength = 3;
|
|
|
|
PImage source;
|
|
int sectionLength;
|
|
int originalLength;
|
|
|
|
ArrayList<Position> positions;
|
|
PVector previous;
|
|
|
|
void settings() {
|
|
source = loadImage(imgPath);
|
|
size(source.width, source.height);
|
|
|
|
originalLength = floor(source.width / gridLength);
|
|
sectionLength = floor(width / gridLength);
|
|
|
|
positions = new ArrayList<Position>();
|
|
for (int i = 0; i < pow(gridLength,2); i++) {
|
|
positions.add(new Position(i % gridLength, floor(i / gridLength)));
|
|
}
|
|
int counter = 0;
|
|
while (counter < 10000) {
|
|
final boolean m = move(
|
|
new PVector(random(width), random(height)),
|
|
new PVector(random(width), random(height))
|
|
);
|
|
if (m) {
|
|
counter++;
|
|
}
|
|
}
|
|
}
|
|
|
|
void draw() {
|
|
background(0);
|
|
|
|
for (int i = 0; i < positions.size(); i++) {
|
|
final Position original = positions.get(i);
|
|
if (!(original.x == 0 && original.y == 0)) {
|
|
final Position current = new Position(i % gridLength, floor(i / gridLength));
|
|
copy(
|
|
source,
|
|
|
|
original.x*originalLength,
|
|
original.y*originalLength,
|
|
originalLength,
|
|
originalLength,
|
|
|
|
current.x*sectionLength,
|
|
current.y*sectionLength,
|
|
sectionLength,
|
|
sectionLength
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
void mousePressed() {
|
|
previous = new PVector(mouseX, mouseY);
|
|
}
|
|
|
|
void mouseReleased() {
|
|
move(new PVector(mouseX, mouseY), previous);
|
|
}
|
|
|
|
boolean move(PVector a, PVector b) {
|
|
final PVector distance = PVector.sub(a, b);
|
|
int dx = 0, dy = 0;
|
|
|
|
if (abs(distance.x) > abs(distance.y))
|
|
dx = floor(distance.x / abs(distance.x));
|
|
else
|
|
dy = floor(distance.y / abs(distance.y));
|
|
|
|
final Position selected = new Position(
|
|
floor(((float)b.x*gridLength) / width),
|
|
floor(((float)b.y*gridLength) / height)
|
|
);
|
|
|
|
final int nx = selected.x + dx, ny = selected.y + dy;
|
|
final boolean xBounds = nx < gridLength && nx >= 0;
|
|
final boolean yBounds = ny < gridLength && ny >= 0;
|
|
final boolean wasMoved = xBounds && yBounds;
|
|
|
|
if (wasMoved) {
|
|
final Position next = new Position(nx, ny);
|
|
final int sIndex = selected.x + selected.y * gridLength;
|
|
final int nIndex = next.x + next.y * gridLength;
|
|
// println(sIndex, selected.x, selected.y, nx, ny);
|
|
final Position regular = positions.get(sIndex).copy();
|
|
final Position duplicate = positions.get(nIndex).copy();
|
|
final boolean isReg = (regular.x == 0 && regular.y == 0);
|
|
final boolean isDup = (duplicate.x == 0 && duplicate.y == 0);
|
|
if (isReg || isDup) {
|
|
positions.set(nIndex, regular);
|
|
positions.set(sIndex, duplicate);
|
|
}
|
|
}
|
|
|
|
return wasMoved;
|
|
}
|
|
|
|
class Position {
|
|
int x, y;
|
|
|
|
Position(int x_, int y_) {
|
|
x = x_;
|
|
y = y_;
|
|
}
|
|
|
|
Position copy() {
|
|
return new Position(x, y);
|
|
}
|
|
|
|
boolean isEqual(Position other) {
|
|
return (x == other.x) && (y == other.y);
|
|
}
|
|
}
|