Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

java code toPoint(int) Return the (x, y) center of the hexagon at this coordinate using the given width. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ package edu.uwm.cs351; import java.awt.Point; import java.awt.Polygon;

java code

toPoint(int) Return the (x, y) center of the hexagon at this coordinate using the given

width.

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

package edu.uwm.cs351; import java.awt.Point; import java.awt.Polygon; /** * Coordinates on a hexagon-filled game board. * 
*
a
left to right (0 = left edge, moving left half a hex each line down) *
b
top to bottom (0 = top edge) *
c
left to right (0 = top edge, moving right half a hex each line down) *
* The {@link #c()} coordinate is always the difference of the first two. */ public class HexCoordinate { private final int a, b, c; /** * Create a hexagonal coordinate by specifying the first two coordinates * and computing the third. * @param a first coordinate * @param b second coordinate */ public HexCoordinate(int a, int b) { // TODO: Very easy. See handout. this.a = a; this.b = b; } /** * Create a hexagonal coordinate by specifying all three coordinates, * which must be consistent. * @param a * @param b * @param c * @exception IllegalArgumentException if the coordinates are not consistent. */ public HexCoordinate(int a, int b, int c) throws IllegalArgumentException { // TODO: Check consistency and then assign fields. HexCoordinate(a, b); this.c = c; } /// three simple accessors /** Return the first coordinate (how far from left * plus more every line). * @return the first coordinate */ public int a() { return a; } /** * Return the second coordinate (how far from top). * @return the second coordinate */ public int b() { return b; } /** * Return the third coordinate (how far from left * minus more very line). * @return the third coordinate */ public int c() { return c; } /// Overrides // no need to give a documentation comment if overridden documentation still is valid. // return true iff x is another HexCoordinate with the same value @Override public boolean equals(Object x) { // TODO HexCoordinate tester = (HexCoordinate)x; if(a != tester.a()) return false; if(b != tester.a()) return false; if(c != tester.a()) return false; } @Override public int hashCode() { return a ^ (b << 8); //return some combination of a, b and c that distinguishes similar coordinates } @Override public String toString() { // TODO: return a string of the form <3,2,1> return "<" + a + "," + b + "," + c + ">"; } /** * Return the closest hex coordinate to this point. * If two are equally close, either may be returned. * @param p * @param width width of grid (must NOT be negative or zero) * @return closest hex coordinate */ public static HexCoordinate fromPoint(Point p, int width) { float height = width * HEIGHT_RATIO; float db = p.y/height; float da = (float)p.x/width + db/2.0f; float dc = da - db; int ac = (int)Math.floor((da+dc)); int ab = (int)Math.floor((da+db)); int bc = (int)Math.floor((db-dc)); int a = (int)Math.ceil((ab+ac)/3.0); int b = (int)Math.ceil((ab+bc)/3.0); return new HexCoordinate(a,b); } /// Other accessors //define HEIGHT_RATIO is the ration of height to width public static final float HEIGHT_RATIO = (float) (Math.sqrt(3.0)/2.0); // height of a row, given width = 1.0 private static final float THIRD = 1.0f/3.0f; private static final float TWOTHIRD = 2.0f/3.0f; /** * Return center of hexagon as a point on the two-dimensional AWT plane. * @param width width of hexagon * @return Point in the center of the hexagon */ public Point toPoint(int width) { // TODO: call the other toPoint method with the correct coordinates } /** * A generalization of {@link #toPoint(int)} that takes two floats, to permit * fractions into the coordinate space. * @param width width of hexagon in grid * @param a first coordinate * @param b second coordinate * @return [x,y] point for this location. */ private static Point toPoint(int width, float a, float b) { float height; // TODO: compute the height using the width and the height ratio return new Point(Math.round(width*(a-b/2.0f)),Math.round(height*b)); } /** * Create a polygon (for rendering in AWT) for the hexagon around this * hex coordinate. The hexagons so creates tile the plane. * @param width width of hexagon in pixels * @return polygon for hexagon */ public Polygon toPolygon(int width) { Point[] ps = { toPoint(width,a-THIRD,b-TWOTHIRD), toPoint(width,a+THIRD,b-THIRD), toPoint(width,a+TWOTHIRD,b+THIRD), toPoint(width,a+THIRD,b+TWOTHIRD), toPoint(width,a-THIRD,b+THIRD), toPoint(width,a-TWOTHIRD,b-THIRD) }; Polygon result = new Polygon(); //TODO: add the points in ps to this Polygon // you may want to look up Oracle's page on Polygon for(int i =0; i < ps.length; i++) { result.addPoint(ps[i].x, ps[i].y); } return result; } /** * Return the number of steps to get from one hex to another. * We can use the smallest distance traveling along just two of the coordinates. * Thus we can add all the differences and remove the largest (not used). * Alternately, we can return the largest of the differences directly, * which (do the algebra!) is the same value. * @param other * @return number of steps from one hex to another */ public int distance(HexCoordinate other) { // TODO: return the distance between this hexagon and the other // see the handout for details // you can use Math.max(x, y) and Math.abs(x) int min = Integer.MAX_VALUE; for (int i = 0; i < 3; i++) { if(i == 1) { min = Math.min(min, Math.abs(b - c)); } else if (i ==2) { min = Math.min(min, Math.abs(c -a)); } else { min = Math.min(min, Math.abs(b -a)); } } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Filing And Computer Database Projects

Authors: Jeffrey Stewart

2nd Edition

007822781X, 9780078227813

More Books

Students also viewed these Databases questions

Question

=+ Would that make the recession more or less severe?

Answered: 1 week ago

Question

2. Explain how the role of training is changing.

Answered: 1 week ago

Question

7. General Mills

Answered: 1 week ago