From 86a60cf5316bb1337c3fc4a565642b51d27171cd Mon Sep 17 00:00:00 2001 From: GuillermoBlasco Date: Tue, 10 Jul 2012 10:44:19 +0200 Subject: [PATCH] comments and Metric class --- src/com/softwsgbj/hexnet/Map.java | 22 +++- src/com/softwsgbj/hexnet/dw/GeoElements.java | 120 ++++++++++++++++-- .../softwsgbj/hexnet/dw/GeometricAdapter.java | 19 +-- src/com/softwsgbj/hexnet/dw/MapRender.java | 35 +++++ src/com/softwsgbj/hexnet/dw/Metric.java | 40 ++++++ 5 files changed, 210 insertions(+), 26 deletions(-) create mode 100644 src/com/softwsgbj/hexnet/dw/MapRender.java create mode 100644 src/com/softwsgbj/hexnet/dw/Metric.java diff --git a/src/com/softwsgbj/hexnet/Map.java b/src/com/softwsgbj/hexnet/Map.java index a24f168..53ff9d2 100644 --- a/src/com/softwsgbj/hexnet/Map.java +++ b/src/com/softwsgbj/hexnet/Map.java @@ -19,21 +19,35 @@ import java.util.AbstractList; import java.util.ArrayList; import java.util.Vector; - +/** + * + * @author GuillermoBlascoJimenez + * @version 1.0 + * @param + */ public class Map implements Serializable, PointFactory{ private static final long serialVersionUID = 177L; - + /*table of hexagons*/ private AbstractList[] table; - + /** + * Builds map with a vector. Use this constructor if you need a synchronized + * access to data. + * @param table Vector that contains data. + */ public Map(Vector[] table){ this.table = table; } + /** + * Builds map with a array list. Use this constructor if you don't care + * about synchronization. + * @param table ArrayList that contains data. + */ public Map(ArrayList[] table){ this.table = table; } public Relations.PointInMap relation(HexPoint p){ - if(p.x() >= getBoundX() || p.y() >= getBoundY() || p.x() < 0 || p.y() < 0) + if(p== null || p.x() >= getBoundX() || p.y() >= getBoundY() || p.x() < 0 || p.y() < 0) return Relations.PointInMap.IS_OUT_OF_BOUNDS; else if(this.getH(p.x(),p.y()) == null) return Relations.PointInMap.IS_OUT_OF_EXISTENCE; diff --git a/src/com/softwsgbj/hexnet/dw/GeoElements.java b/src/com/softwsgbj/hexnet/dw/GeoElements.java index 78a3206..5230363 100644 --- a/src/com/softwsgbj/hexnet/dw/GeoElements.java +++ b/src/com/softwsgbj/hexnet/dw/GeoElements.java @@ -17,11 +17,14 @@ public class GeoElements { + + private static final double cos_30 = Math.cos(Math.PI/6.0); interface NotableElement{ } interface NotablePoint extends NotableElement{ } interface NotableSegment extends NotableElement{ + public double getRadiusProportion(); } interface GeoElement{ @@ -42,11 +45,11 @@ public enum NotablePoints implements NotablePoint{ CENTER, APEX, MIDPOINT; } public enum NotableSegments implements NotableSegment{ - SIDE (1 , Math.cos(Math.PI/6) ), - DIAMETER(0.5 , 0.5*Math.cos(Math.PI/6)), - RADIUS (1 , Math.cos(Math.PI/6) ), - APOTHEM (1/Math.cos(Math.PI/6) , 1 ), - HEIGH (0.5/Math.cos(Math.PI/6), 0.5 ); + SIDE (1 , cos_30 ), + DIAMETER(0.5 , 0.5*cos_30), + RADIUS (1 , cos_30 ), + APOTHEM (1/cos_30 , 1 ), + HEIGH (0.5/cos_30 , 0.5 ); private double k_radius; private double k_apothem; NotableSegments(double k_radius, double k_apothem) @@ -180,7 +183,17 @@ public enum NotablePoints implements NotablePoint{ CENTER, APEX, MIDPOINT; } public enum NotableSegments implements NotableSegment { - SIDE, DIAGONAL; + TOP_SIDE(2.0), + LATERAL_SIDE(2.0*cos_30), + DIAGONAL(Math.sqrt(4.0*(1+cos_30*cos_30))); + private double r_prop; + NotableSegments(double r){ + this.r_prop = r; + } + public double getRadiusProportion() { + return r_prop; + } + } public enum Points implements Point { CENTER (NotablePoints.CENTER, 1.0 ,1.0), @@ -215,16 +228,16 @@ public double getYApothemsToA() { } public enum Segments implements Segment { - SIDE_N (NotableSegments.SIDE, + SIDE_N (NotableSegments.TOP_SIDE, Points.APEX_NW, Points.APEX_NE), - SIDE_E (NotableSegments.SIDE, + SIDE_E (NotableSegments.LATERAL_SIDE, Points.APEX_NE, Points.APEX_SE), - SIDE_S (NotableSegments.SIDE, + SIDE_S (NotableSegments.TOP_SIDE, Points.APEX_SE, Points.APEX_SW), - SIDE_W (NotableSegments.SIDE, + SIDE_W (NotableSegments.LATERAL_SIDE, Points.APEX_SW, Points.APEX_NW), DIAGONAL_NE_SW (NotableSegments.DIAGONAL, @@ -253,5 +266,92 @@ public OfRectangle.NotableSegments getNotable(){ } } } + + public static class OfSquare { + + public enum NotablePoints implements NotablePoint{ + CENTER, APEX, MIDPOINT; + } + public enum NotableSegments implements NotableSegment { + SIDE (2.0), DIAGONAL(Math.sqrt(8.0)); + private double r_prop; + NotableSegments(double r){ + this.r_prop = r; + } + public double getRadiusProportion() { + return r_prop; + } + } + public enum Points implements Point { + CENTER (NotablePoints.CENTER, 1.0 ,1.0 ), + APEX_NE (NotablePoints.APEX, 2.0, 0.0 ), + APEX_SE (NotablePoints.APEX, 2.0, 2.0/cos_30 ), + APEX_SW (NotablePoints.APEX, 0.0, 2.0/cos_30 ), + APEX_NW (NotablePoints.APEX, 0.0, 0.0 ), + MIDPOINT_NE_SE (NotablePoints.MIDPOINT,2.0, 1.0/cos_30 ), + MIDPOINT_SE_SW (NotablePoints.MIDPOINT,1.0, 2.0/cos_30 ), + MIDPOINT_SW_NW (NotablePoints.MIDPOINT,0.0, 1.0/cos_30 ), + MIDPOINT_NW_NE (NotablePoints.MIDPOINT,1.0, 0.0 ); + + private OfSquare.NotablePoints notable; + private double x_radius; + private double y_apothems; + Points(NotablePoints notable,double x_radius, double y_apothems){ + this.notable = notable; + this.x_radius =x_radius; + this.y_apothems = y_apothems; + } + public NotablePoint getNotable(){ + return this.notable; + } + @Override + public double getXRadiusToA() { + return x_radius; + } + @Override + public double getYApothemsToA() { + return y_apothems; + } + } + public enum Segments implements Segment { + + SIDE_N (NotableSegments.SIDE, + Points.APEX_NW, Points.APEX_NE), + + SIDE_E (NotableSegments.SIDE, + Points.APEX_NE, Points.APEX_SE), + + SIDE_S (NotableSegments.SIDE, + Points.APEX_SE, Points.APEX_SW), + + SIDE_W (NotableSegments.SIDE, + Points.APEX_SW, Points.APEX_NW), + + DIAGONAL_NE_SW (NotableSegments.DIAGONAL, + Points.APEX_NE, Points.APEX_SW), + + DIAGONAL_SE_NW (NotableSegments.DIAGONAL, + Points.APEX_NW, Points.APEX_SE); + + private OfSquare.Points origin; + private OfSquare.Points destiny; + private OfSquare.NotableSegments notable; + Segments(NotableSegments notable, Points origin, Points destiny){ + this.notable = notable; + this.origin = origin; + this.destiny = destiny; + } + public OfSquare.Points getOrigin(){ + return this.origin; + } + public OfSquare.Points getDestiny(){ + return this.destiny; + } + public OfSquare.NotableSegments getNotable(){ + return this.notable; + } + } + } + } diff --git a/src/com/softwsgbj/hexnet/dw/GeometricAdapter.java b/src/com/softwsgbj/hexnet/dw/GeometricAdapter.java index 0660119..4d86f2b 100644 --- a/src/com/softwsgbj/hexnet/dw/GeometricAdapter.java +++ b/src/com/softwsgbj/hexnet/dw/GeometricAdapter.java @@ -26,20 +26,15 @@ public class GeometricAdapter { private final Map map; private LinkedList childs; - - private GeoElements.OfHexagon.NotableSegments reference_vector; - private double reference_vector_size; + private Metric metric; public GeometricAdapter(Map m){ this.map = m; childs = new LinkedList (); - reference_vector = GeoElements.OfHexagon.NotableSegments.RADIUS; - reference_vector_size = 1.0; + metric = new Metric(GeoElements.OfHexagon.NotableSegments.RADIUS, 1.0); } - public void setMetric(GeoElements.OfHexagon.NotableSegments ref, double value){ - reference_vector = ref; - if(value > 0) - reference_vector_size = value; + public void setMetric(Metric metric){ + this.metric = metric; } public GeoPoint getPointOfHexagon(GeoElements.OfHexagon.Points which, HexPoint p){ @@ -114,10 +109,10 @@ protected double getYof(int x, int y){ return ((y)*2 +1.0)*getApothem(); } protected double getApothem(){ - return this.reference_vector.getApothemProportion()*this.reference_vector_size; + return this.metric.getApothemSize(); } - protected double getRadius(){ - return this.reference_vector.getRadiusProportion()*this.reference_vector_size; + protected double getRadius(){ + return this.metric.getRadiusSize(); } protected void actualizeChildRefernce(HexImage child){ GeoPoint p = this.getPoint(child.getPoint().getReferedElement(), child.getHexagon()); diff --git a/src/com/softwsgbj/hexnet/dw/MapRender.java b/src/com/softwsgbj/hexnet/dw/MapRender.java new file mode 100644 index 0000000..78d9404 --- /dev/null +++ b/src/com/softwsgbj/hexnet/dw/MapRender.java @@ -0,0 +1,35 @@ +package com.softwsgbj.hexnet.dw; + +import com.softwsgbj.hexnet.HexPoint; +import com.softwsgbj.hexnet.Hexagon; +import com.softwsgbj.hexnet.Map; + +public abstract class MapRender { + + private Map m; + private GeometricAdapter geo; + + public MapRender(Map m){ + this.m = m; + this.geo = new GeometricAdapter(m); + } + + public void repaintAll(){ + + } + public void paint(int x, int y){ + HexPoint p = m.buildPoint(x, y); + if(p!= null){ + Hexagon h = m.getHexagonIn(p); + if(h != null){ + + } else { + /*not exists*/ + /*paint default hexagon*/ + } + + } else { /* out of bounds*/ } + } + + protected abstract void paint(HexDraw draw, int x, int y); +} diff --git a/src/com/softwsgbj/hexnet/dw/Metric.java b/src/com/softwsgbj/hexnet/dw/Metric.java new file mode 100644 index 0000000..0752c5d --- /dev/null +++ b/src/com/softwsgbj/hexnet/dw/Metric.java @@ -0,0 +1,40 @@ +package com.softwsgbj.hexnet.dw; + +public class Metric implements Comparable{ + + private GeoElements.NotableSegment reference_segment; + private double value; + + public Metric(GeoElements.NotableSegment reference_segment, double value){ + changeMetric(reference_segment, value); + } + + public double getRadiusSize(){ + return value*reference_segment.getRadiusProportion(); + } + public double getApothemSize(){ + return getRadiusSize()*Math.cos(Math.PI/6.0); + } + public double getSizeOf(GeoElements.NotableSegment reference_segment){ + double prev = getRadiusSize(); + return prev / reference_segment.getRadiusProportion(); + } + public void changeMetric(GeoElements.NotableSegment reference_segment, double value){ + if(value <= 0.0) + throw new IllegalArgumentException("value must be positive."); + this.reference_segment = reference_segment; + this.value = value; + } + public boolean equals(Object o){ + if(o!=null && o instanceof Metric){ + Metric m = (Metric) o; + return this.getRadiusSize() == m.getRadiusSize(); + } else return false; + } + public int compareTo(Metric m){ + double dif = getRadiusSize() - m.getRadiusSize(); + if(dif > 0) return (int)Math.ceil(dif); + else if(dif < 0) return (int) Math.ceil(-dif); + else return 0; + } +}