我为自己编写的游戏创建了一个坐标系统。这里提供CoordinateSystem类作为示例。
以下是CoordinateSystem类的内容。
package com.ggl.game.utilities;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.geom.Point2D;
public class CoordinateSystem {
private double conversionFactor;
private double xLimit;
private double yLimit;
private int height;
private int width;
public CoordinateSystem(double limit, int width, int height) {
this.width = width;
this.height = height;
if (width > height) {
this.xLimit = limit * width / height;
this.yLimit = limit;
this.conversionFactor = (limit + limit) / (double) height;
} else if (width < height) {
this.xLimit = limit;
this.yLimit = limit * height / width;
this.conversionFactor = (limit + limit) / (double) width;
} else {
this.xLimit = limit;
this.yLimit = limit;
this.conversionFactor = (limit + limit) / (double) width;
}
}
public void setDrawingSize(Dimension dimension) {
setDrawingSize(dimension.width, dimension.height);
}
public void setDrawingSize(int width, int height) {
xLimit = xLimit / this.width * width;
yLimit = yLimit / this.height * height;
this.width = width;
this.height = height;
}
public double getxLimit() {
return xLimit;
}
public double getyLimit() {
return yLimit;
}
public Point convertPolarToPixels(double distance, double theta) {
return convertToPixels(new Polar2D.Double(distance, theta));
}
public Point convertPointToPixels(double x, double y) {
return convertToPixels(new Point2D.Double(x, y));
}
public Point convertToPixels(Polar2D polar) {
double x = polar.getDistance() * Math.cos(polar.getTheta());
double y = polar.getDistance() * Math.sin(polar.getTheta());
return convertToPixels(new Point2D.Double(x, y));
}
public Point convertToPixels(Point2D cartesian) {
int x = (int) Math
.round((cartesian.getX() + xLimit) / conversionFactor);
int y = (int) Math.round((-cartesian.getY() + yLimit)
/ conversionFactor);
return new Point(x, y);
}
public Point2D convertToCartesian(int x, int y) {
return convertToCartesian(new Point(x, y));
}
public Point2D convertToCartesian(Point point) {
double x = (double) point.x * conversionFactor - xLimit;
double y = (double) -point.y * conversionFactor + yLimit;
return new Point2D.Double(x, y);
}
public Polar2D convertToPolar(int x, int y) {
return convertToPolar(new Point(x, y));
}
public Polar2D convertToPolar(Point point) {
double x = (double) point.x * conversionFactor - xLimit;
double y = (double) -point.y * conversionFactor + yLimit;
double distance = Math.sqrt(x * x + y * y);
double theta = Math.atan2(y, x);
return new Polar2D.Double(distance, theta);
}
}
这里是Polar2D类。它是Point2D类的克隆。
package com.ggl.game.utilities;
public abstract class Polar2D implements Cloneable {
public static class Float extends Polar2D {
public float distance;
public float theta;
public Float() {
}
public Float(float distance, float theta) {
this.distance = distance;
this.theta = theta;
}
@Override
public double getDistance() {
return (double) distance;
}
@Override
public double getTheta() {
return (double) theta;
}
@Override
public double getThetaInDegrees() {
double degrees = 180D / Math.PI * theta;
return (degrees < 0D) ? degrees + 360D : degrees;
}
@Override
public void setLocation(double distance, double theta) {
this.distance = (float) distance;
this.theta = (float) theta;
}
public void setLocation(float distance, float theta) {
this.distance = distance;
this.theta = theta;
}
@Override
public String toString() {
return "Polar2D.Float[" + distance + ", " + theta + "]";
}
}
public static class Double extends Polar2D {
public double distance;
public double theta;
public Double() {
}
public Double(double distance, double theta) {
this.distance = distance;
this.theta = theta;
}
@Override
public double getDistance() {
return distance;
}
@Override
public double getTheta() {
return theta;
}
@Override
public double getThetaInDegrees() {
double degrees = 180D / Math.PI * theta;
return (degrees < 0D) ? degrees + 360D : degrees;
}
@Override
public void setLocation(double distance, double theta) {
this.distance = distance;
this.theta = theta;
}
@Override
public String toString() {
return "Polar2D.Double[" + distance + ", " + theta + "]";
}
}
protected Polar2D() {
}
public abstract double getDistance();
public abstract double getTheta();
public abstract double getThetaInDegrees();
public abstract void setLocation(double distance, double theta);
public void setLocation(Polar2D p) {
setLocation(p.getDistance(), p.getTheta());
}
public static double distanceSq(double distance1, double theta1,
double distance2, double theta2) {
double x1 = distance1 * Math.cos(theta1);
double y1 = distance1 * Math.sin(theta1);
double x2 = distance2 * Math.cos(theta2);
double y2 = distance2 * Math.sin(theta2);
return (x1 * x2 + y1 * y2);
}
public static double distance(double distance1, double theta1,
double distance2, double theta2) {
double x1 = distance1 * Math.cos(theta1);
double y1 = distance1 * Math.sin(theta1);
double x2 = distance2 * Math.cos(theta2);
double y2 = distance2 * Math.sin(theta2);
return Math.sqrt(x1 * x2 + y1 * y2);
}
public double distanceSq(double distance, double theta) {
double x1 = distance * Math.cos(theta);
double y1 = distance * Math.sin(theta);
double x2 = getDistance() * Math.cos(getTheta());
double y2 = getDistance() * Math.sin(getTheta());
return (x1 * x2 + y1 * y2);
}
public double distanceSq(Polar2D pt) {
double x1 = pt.getDistance() * Math.cos(pt.getTheta());
double y1 = pt.getDistance() * Math.sin(pt.getTheta());
double x2 = getDistance() * Math.cos(getTheta());
double y2 = getDistance() * Math.sin(getTheta());
return (x1 * x2 + y1 * y2);
}
public double distance(double distance, double theta) {
double x1 = distance * Math.cos(theta);
double y1 = distance * Math.sin(theta);
double x2 = getDistance() * Math.cos(getTheta());
double y2 = getDistance() * Math.sin(getTheta());
return Math.sqrt(x1 * x2 + y1 * y2);
}
public double distance(Polar2D pt) {
double x1 = pt.getDistance() * Math.cos(pt.getTheta());
double y1 = pt.getDistance() * Math.sin(pt.getTheta());
double x2 = getDistance() * Math.cos(getTheta());
double y2 = getDistance() * Math.sin(getTheta());
return Math.sqrt(x1 * x2 + y1 * y2);
}
@Override
public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
throw new InternalError();
}
}
@Override
public int hashCode() {
long bits = java.lang.Double.doubleToLongBits(getDistance());
bits ^= java.lang.Double.doubleToLongBits(getTheta()) * 31;
return (((int) bits) ^ ((int) (bits >> 32)));
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Polar2D) {
Polar2D p2d = (Polar2D) obj;
return (getDistance() == p2d.getDistance())
&& (getTheta() == p2d.getTheta());
}
return super.equals(obj);
}
}