Permalink
Browse files

New number system, started implementation of decorations.

  • Loading branch information...
1 parent f167ac5 commit 02d81654731adfdd9e8a7c366dd6344aaec518ac @scooby committed May 15, 2011
@@ -1,26 +1,15 @@
-package veb;
+package space;
import java.util.Iterator;
+import space.Vector.cardinal;
/**
* @author ben
- *
- */
-/**
- * @author ben
- *
- */
-/**
- * @author ben
- *
- */
-/**
- * @author ben
*
*/
public final class Area implements Iterable<Area>, Comparable<Area> {
/**
- * The column the area starts on.
+ * The column the area starts on; also the westernmost column.
*/
final public int sx;
/**
@@ -29,7 +18,7 @@
*/
final public int ex;
/**
- * The row the area starts on.
+ * The row the area starts on; also the "southernmost" row.
*/
final public int sy;
/**
@@ -246,7 +235,14 @@ public boolean coveredBy(final Iterable<Area> ax) {
return cells == 0;
}
/**
- *
+ * Example of extended by:
+ * <pre>
+ * +------+---+
+ * | | A | 'this' is extended by {A, B}.
+ * | this +---+
+ * | | B |
+ * +------+---+
+ * </pre>
* @return whether or not the iterable of areas extend this area
*/
public boolean extendedBy(final Iterable<Area> ax) {
@@ -257,6 +253,30 @@ public boolean extendedBy(final Iterable<Area> ax) {
|| ey == ext.sy && sx == ext.sx && ex == ext.ex;
}
/**
+ *
+ * @return the direction in which this area can merge
+ *
+ */
+ public cardinal canMergeWith(Area a) {
+ if(sy == a.sy && ey == a.ey) {
+ // Cases: East/west
+ if(sx == a.ex)
+ return cardinal.west;
+ if(ex == a.sx)
+ return cardinal.east;
+ return null;
+ } else if(sx == a.sx && ex == a.ex) {
+ // North/South
+ if(sy == a.ey)
+ return cardinal.south;
+ if(ey == a.sy)
+ return cardinal.north;
+ return null;
+ } else
+ return null;
+ }
+
+ /**
* Order by rows, then columns, then area, then width.
*/
@Override
View
@@ -0,0 +1,61 @@
+package space;
+public final class Double extends RationalImpl {
+ final double val;
+ public Double(double val) { this.val = val; }
+ @Override
+ public long longVal() { return (long) val;}
+ @Override
+ public double doubleVal() { return val; }
+ @Override
+ public Num abs() { return val < 0.0d ? new Double(-val) : this; }
+ @Override
+ public int sign() {
+ return java.lang.Double.compare(doubleVal(), 0.0d);
+ }
+ @Override
+ public Num factor(int i) { return new Double(i); }
+ @Override
+ protected NumImpl boxRat(double val) {
+ return new Double(val);
+ }
+ @Override
+ protected Integral boxInt(long v) {
+ return new Long(v);
+ }
+ /* (non-Javadoc)
+ * @see java.lang.Object#hashCode()
+ */
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ long temp;
+ temp = java.lang.Double.doubleToLongBits(val);
+ result = prime * result + (int) (temp ^ (temp >>> 32));
+ return result;
+ }
+ /* (non-Javadoc)
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (!(obj instanceof Double))
+ return false;
+ Double other = (Double) obj;
+ if (java.lang.Double.doubleToLongBits(val) != java.lang.Double
+ .doubleToLongBits(other.val))
+ return false;
+ return true;
+ }
+ /* (non-Javadoc)
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString() {
+ return String.format("Double [val=%s]", val);
+ }
+}
View
@@ -0,0 +1,60 @@
+package space;
+
+public final class Float extends RationalImpl {
+ final float val;
+ public Float(float val) { this.val = val; }
+ @Override
+ public long longVal() { return (long) val;}
+ @Override
+ public double doubleVal() { return val; }
+ @Override
+ public Num abs() { return val < 0.0f ? new Float(-val) : this; }
+ @Override
+ public int sign() {
+ return java.lang.Float.compare(val, 0.0f);
+ }
+ @Override
+ public Num factor(int i) { return new Float(i); }
+ @Override
+ protected NumImpl boxRat(double val) {
+ return new Float((float) val);
+ }
+ @Override
+ protected Integral boxInt(long v) {
+ return new Long(v);
+ }
+ /* (non-Javadoc)
+ * @see java.lang.Object#hashCode()
+ */
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + java.lang.Float.floatToIntBits(val);
+ return result;
+ }
+ /* (non-Javadoc)
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (!(obj instanceof Float))
+ return false;
+ Float other = (Float) obj;
+ if (java.lang.Float.floatToIntBits(val) != java.lang.Float
+ .floatToIntBits(other.val))
+ return false;
+ return true;
+ }
+ /* (non-Javadoc)
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString() {
+ return String.format("Float [val=%s]", val);
+ }
+}
View
@@ -0,0 +1,28 @@
+package space;
+
+public final class Int extends IntegralImpl {
+ final private int val;
+ public Int(int val) { this.val = val; }
+ @Override
+ public NumImpl boxInt(long val) { return new Int((int) val); }
+ @Override
+ public NumImpl boxRat(double val) { return new Float((float) val); }
+ @Override
+ public long longVal() { return val; }
+ @Override
+ public double doubleVal() { return (double) val; }
+ @Override
+ public Num abs() { return val < 0 ? new Int(-val) : this; }
+ @Override
+ public Num factor(int i) { return new Int(i); }
+ /* (non-Javadoc)
+ * @see java.lang.Object#hashCode()
+ */
+ /* (non-Javadoc)
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString() {
+ return String.format("Int [val=%s]", val);
+ }
+}
View
@@ -0,0 +1,8 @@
+package space;
+
+interface Integral extends Num {
+ public Integral addInt(Num v);
+ public Integral subInt(Num v);
+ public Integral multInt(Num v);
+ public Integral divInt(Num v);
+}
@@ -0,0 +1,86 @@
+package space;
+import java.util.Iterator;
+
+
+public abstract class IntegralImpl extends NumImpl {
+ /* (non-Javadoc)
+ * @see space.Num#add(space.Num)
+ */
+ @Override
+ public Num add(Num v) {
+ return boxInt(longVal() + v.longVal());
+ }
+ /* (non-Javadoc)
+ * @see space.Num#sub(space.Num)
+ */
+ @Override
+ public Num sub(Num v) {
+ return boxInt(longVal() - v.longVal());
+ }
+ /* (non-Javadoc)
+ * @see space.Num#mult(space.Num)
+ */
+ @Override
+ public Num mult(Num v) {
+ return boxInt(longVal() * v.longVal());
+ }
+ /* (non-Javadoc)
+ * @see space.NumImpl#boxInt(long)
+ */
+ @Override
+ public int sign() {
+ final long lv = longVal();
+ return lv > 0 ? 1 : (lv == 0 ? 0 : -1);
+ }
+ @Override
+ protected Iterable<Num> _spread(final int o) {
+ assert o > 0; // Positive
+ return new Iterable<Num>() {
+ @Override
+ public Iterator<Num> iterator() {
+ return new Iterator<Num>() {
+ final long v = longVal();
+ int i = 0;
+ @Override
+ public boolean hasNext() { return i < o; }
+ @Override
+ public Num next() {
+ i++;
+ return boxInt(v * i / o
+ - v * (i-1) / o);
+ }
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+ };
+ }
+ };
+ }
+ /* (non-Javadoc)
+ * @see java.lang.Object#hashCode()
+ */
+ @Override
+ final public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + (int) (longVal() ^ (longVal() >>> 32));
+ return result;
+ }
+ /* (non-Javadoc)
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
+ @Override
+ final public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (!(obj instanceof IntegralImpl))
+ return false;
+ IntegralImpl other = (IntegralImpl) obj;
+ if (longVal() != other.longVal())
+ return false;
+ return true;
+ }
+}
View
@@ -0,0 +1,24 @@
+package space;
+public final class Long extends IntegralImpl {
+ final private long val;
+ public Long(long val) { this.val = val; }
+ @Override
+ public NumImpl boxInt(long val) { return new Long(val); }
+ @Override
+ public NumImpl boxRat(double val) { return new Double(val); }
+ @Override
+ public long longVal() { return val; }
+ @Override
+ public double doubleVal() { return (double) val; }
+ @Override
+ public Num abs() { return val < 0 ? new Long(-val) : this; }
+ @Override
+ public Num factor(int i) { return new Long(i); }
+ /* (non-Javadoc)
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString() {
+ return String.format("Long [val=%s]", val);
+ }
+}
View
@@ -0,0 +1,18 @@
+package space;
+
+public interface Num {
+ public long longVal();
+ public double doubleVal();
+ public Num abs();
+ public Num factor(int i);
+ public Num scale(int s);
+ public Iterable<Num> spread(int i);
+ public Num add(Num v);
+ public Num sub(Num v);
+ public Num mult(Num v);
+ public int sign();
+ @Override
+ public boolean equals(Object o);
+ @Override
+ public int hashCode();
+}
Oops, something went wrong.

0 comments on commit 02d8165

Please sign in to comment.