Skip to content

Commit

Permalink
comments and Metric class
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillermoBlasco committed Jul 10, 2012
1 parent 2e8e8f9 commit 86a60cf
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 26 deletions.
22 changes: 18 additions & 4 deletions src/com/softwsgbj/hexnet/Map.java
Expand Up @@ -19,21 +19,35 @@
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Vector;

/**
*
* @author GuillermoBlascoJimenez
* @version 1.0
* @param <H>
*/
public class Map<H extends Hexagon> implements Serializable, PointFactory{

private static final long serialVersionUID = 177L;

/*table of hexagons*/
private AbstractList<H>[] 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<H>[] 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<H>[] 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;
Expand Down
120 changes: 110 additions & 10 deletions src/com/softwsgbj/hexnet/dw/GeoElements.java
Expand Up @@ -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{
Expand All @@ -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)
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
}
}

}
19 changes: 7 additions & 12 deletions src/com/softwsgbj/hexnet/dw/GeometricAdapter.java
Expand Up @@ -26,20 +26,15 @@ public class GeometricAdapter {

private final Map<?> map;
private LinkedList<HexImage> 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<HexImage> ();
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){

Expand Down Expand Up @@ -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());
Expand Down
35 changes: 35 additions & 0 deletions 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);
}
40 changes: 40 additions & 0 deletions src/com/softwsgbj/hexnet/dw/Metric.java
@@ -0,0 +1,40 @@
package com.softwsgbj.hexnet.dw;

public class Metric implements Comparable<Metric>{

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;
}
}

0 comments on commit 86a60cf

Please sign in to comment.