Rewrote the zero vector handling logic. #16.

This commit is contained in:
Rob Greene 2018-06-24 12:21:47 -05:00
parent 965d6be891
commit 580af51716

View File

@ -183,39 +183,26 @@ public class VectorShape implements Shape {
VectorCommand vector2 = work.poll(); VectorCommand vector2 = work.poll();
int section2 = Optional.ofNullable(vector2).map(VectorCommand::ordinal).orElse(0); int section2 = Optional.ofNullable(vector2).map(VectorCommand::ordinal).orElse(0);
VectorCommand vector3 = work.poll(); VectorCommand vector3 = work.poll();
if (vector3 != null && vector3.plot) { // Remove all invalid encodings 100, 101, 110, 111, and 000.
if (vector3 != null && (vector3.plot || vector3 == VectorCommand.MOVE_UP)) {
work.addFirst(vector3); work.addFirst(vector3);
vector3 = null; vector3 = null;
} }
int section3 = Optional.ofNullable(vector3).map(VectorCommand::ordinal).orElse(0); int section3 = Optional.ofNullable(vector3).map(VectorCommand::ordinal).orElse(0);
if ((section1 + section2 + section3) == 0) { if (section3 == 0 && section2 == 0 && !work.isEmpty()) {
// Can only write a 0 byte to terminate shape vector3 = VectorCommand.MOVE_LEFT;
if (vector2 == null) { section3 = vector3.ordinal();
vector2 = VectorCommand.MOVE_LEFT; // If we have a series of MOVE_UP, we'll end up with a "uul", "rul", "rul", etc.
section2 = vector2.ordinal(); // It can be compressed a bit by stretching that right out a bit to get "uul", "uur", "uul, "uur", etc.
} else if (vector3 == null) { int moveUpCount = 0;
vector3 = VectorCommand.MOVE_LEFT; for (VectorCommand test : work) {
section3 = vector3.ordinal(); if (test == VectorCommand.MOVE_UP) {
if (!work.isEmpty()) { moveUpCount += 1;
work.addFirst(VectorCommand.MOVE_RIGHT); } else {
} break;
} else {
work.addFirst(vector3);
vector3 = VectorCommand.MOVE_LEFT;
section3 = vector3.ordinal();
if (!work.isEmpty()) {
work.addFirst(VectorCommand.MOVE_RIGHT);
} }
} }
} else if (vector3 == VectorCommand.MOVE_UP) { work.add(Math.min(moveUpCount,2), VectorCommand.MOVE_RIGHT);
// section 3 cannot be 0
work.addFirst(vector3);
vector3 = null;
if (vector2 == VectorCommand.MOVE_UP) {
// section 2 and 3 cannot be 0
work.addFirst(vector2);
vector2 = null;
}
} }
outputStream.write(section3 << 6 | section2 << 3 | section1); outputStream.write(section3 << 6 | section2 << 3 | section1);
} }