下面列出了java.awt.Point#Double ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Selects a person if any person is at the given x and y pixel position.
*
* @param xPixel the x pixel position on the displayed map.
* @param yPixel the y pixel position on the displayed map.
* @return selectedPerson;
*/
public Person selectPersonAt(int xPixel, int yPixel) {
double range = WIDTH / scale;
Point.Double settlementPosition = convertToSettlementLocation(xPixel, yPixel);
Person selectedPerson = null;
Iterator<Person> i = CollectionUtils.getPeopleToDisplay(settlement).iterator();
while (i.hasNext()) {
Person person = i.next();
double distanceX = person.getXLocation() - settlementPosition.getX();
double distanceY = person.getYLocation() - settlementPosition.getY();
double distance = Math.hypot(distanceX, distanceY);
if (distance <= range) {
selectedPerson = person;
}
}
if (selectedPerson != null) {
selectPerson(selectedPerson);
}
return selectedPerson;
}
@Test
public void testWayPoints() {
int width = 1000;
int height = 500;
int foodCreationRate = 50;
WorldModel model = new WorldModel(width, height, foodCreationRate);
Creature creature = new Creature(new Point.Double(100, 100), Gender.MALE);
BasicAI basicAI = new BasicAI();
basicAI.setCreature(creature);
basicAI.setWorldModel(model);
basicAI.init();
for (Point.Double p : basicAI.getWayPoints()) {
assertEquals(true, p.x <= width);
assertEquals(true, p.x >= 0);
assertEquals(true, p.y <= height);
assertEquals(true, p.y >= 0);
}
}
private void drawPlayerArrow() {
GL11.glPushMatrix();
double scale = this.mapView.getDimensionScaling(this.mw.playerDimension);
Point.Double p = this.mapMode.getClampedScreenXY(this.mapView, this.mw.playerX * scale, this.mw.playerZ * scale);
this.playerArrowScreenPos.setLocation(p.x + this.mapMode.xTranslation, p.y + this.mapMode.yTranslation);
// the arrow only needs to be rotated if the map is NOT rotated
GL11.glTranslated(p.x, p.y, 0.0);
if (!this.mapMode.rotate) {
GL11.glRotated(-this.mw.mapRotationDegrees, 0.0f, 0.0f, 1.0f);
}
double arrowSize = this.mapMode.playerArrowSize;
Render.setColour(0xffffffff);
this.mw.mc.renderEngine.bindTexture(this.playerArrowTexture);
Render.drawTexturedRect(
-arrowSize, -arrowSize, arrowSize * 2, arrowSize * 2,
0.0, 0.0, 1.0, 1.0
);
GL11.glPopMatrix();
}
@Test
@DisplayName("triangles, from top left to bottom down")
void draw_Type_TRIANGLES_top_left_to_bottom_down() {
// orientation: 0 (diagonal line from top left to bottom down)
var rect = new Rectangle2D.Double(0, 0, 10, 10);
compositionGuide.setType(TRIANGLES);
compositionGuide.setOrientation(0);
compositionGuide.draw(rect, g2);
Point.Double p = new Point.Double(5, 5);
Line2D[] lines = new Line2D[3];
lines[0] = new Line2D.Double(0, 0, 10, 10);
lines[1] = new Line2D.Double(0, 10, p.x, p.y);
lines[2] = new Line2D.Double(10, 0, p.x, p.y);
verify(guidesRenderer).draw(refEq(g2), argThat(new DrawMatcherLine2D(Arrays.asList(lines))));
}
@Override
public void update() {
//Move random
if (creature.getTarget() == null) {
Point.Double point = wayPoints.get((int) (Math.random() * wayPoints.size()));
creature.setTarget(point);
}
Food nearestFood = getNearestFood();
//Goto food
if (nearestFood != null && creature.getEnergy() + nearestFood.getValue() <= creature.getMaxEnergy()) {
creature.setTarget(nearestFood.getPosition());
}
//Goto mate
Creature nearestMate = getNearestMate();
if (nearestMate != null) {
creature.setTarget(nearestMate.getPosition());
}
}
private static void paintChunk(MapMode mapMode, MapView mapView, IMwChunkOverlay overlay){
int chunkX = overlay.getCoordinates().x;
int chunkZ = overlay.getCoordinates().y;
float filling = overlay.getFilling();
Point.Double topCorner = mapMode.blockXZtoScreenXY(mapView, chunkX << 4, chunkZ << 4);
Point.Double botCorner = mapMode.blockXZtoScreenXY(mapView, (chunkX + 1) << 4, (chunkZ + 1 << 4));
topCorner.x = Math.max(mapMode.x, topCorner.x);
topCorner.x = Math.min(mapMode.x + mapMode.w, topCorner.x);
topCorner.y = Math.max(mapMode.y, topCorner.y);
topCorner.y = Math.min(mapMode.y + mapMode.h, topCorner.y);
botCorner.x = Math.max(mapMode.x, botCorner.x);
botCorner.x = Math.min(mapMode.x + mapMode.w, botCorner.x);
botCorner.y = Math.max(mapMode.y, botCorner.y);
botCorner.y = Math.min(mapMode.y + mapMode.h, botCorner.y);
double sizeX = (botCorner.x - topCorner.x) * filling;
double sizeY = (botCorner.y - topCorner.y) * filling;
double offsetX = ((botCorner.x - topCorner.x) - sizeX) / 2;
double offsetY = ((botCorner.y - topCorner.y) - sizeY) / 2;
if (overlay.hasBorder()) {
Render.setColour(overlay.getBorderColor());
Render.drawRectBorder(topCorner.x + 1, topCorner.y + 1, botCorner.x - topCorner.x - 1, botCorner.y - topCorner.y - 1, overlay.getBorderWidth());
}
Render.setColour(overlay.getColor());
Render.drawRect(topCorner.x + offsetX + 1, topCorner.y + offsetY + 1, sizeX - 1, sizeY - 1);
}
public static WorldModel createTestWorld() {
WorldModel world = new WorldModel(1000, 800, 50);
Creature[] creatures = {new Creature(new Point.Double(100, 100), Gender.MALE),
new Creature(new Point.Double(200, 100), Gender.FEMALE)};
//Creature[] creatures = {new Creature(new Point.Double(100, 100), Gender.MALE)};
CreatureAI ai = new BasicAI();
creatures[0].setAi(ai);
ai.setWorldModel(world);
ai.setCreature(creatures[0]);
ai.init();
Food[] foods = {new Food(new Point.Double(150, 150), 100),
new Food(new Point.Double(230, 80), 100)
};
for (Creature c : creatures) {
world.addCreature(c);
}
for (Food f : foods) {
world.addFood(f);
}
return world;
}
private void handleMoving(double delta, Creature creature) {
//TODO Maybe only compute stuff if target changes, else let speed be the same
Point.Double target = creature.getTarget();
Point.Double position = creature.getPosition();
//Move nearer to target
if (target != null) {
if (target.distance(position) > (creature.getSpeed() * delta * WorldModel.speedFactor * 1.25)) {
double x = target.x - position.x;
double y = target.y - position.y;
double speed = creature.getSpeed() * delta * WorldModel.speedFactor;
double alpha = speed / Math.sqrt(x*x + y*y);
double speedX = alpha * x;
double speedY = alpha * y;
creature.getPosition().x += speedX;
creature.getPosition().y += speedY;
}
else {
creature.setPosition(new Point.Double(creature.getTarget().x, creature.getTarget().y));
creature.setTarget(null);
}
}
}
private void handleFoodCreation() {
for (int i = 0; i < WorldModel.speedFactor; i++) {
if (worldModel.getFoods().size() < WorldModel.maxFoodAmount &&
(int) (Math.random() * 100) < worldModel.getFoodCreationRate()) {
double xPos = worldModel.getWidth() * Math.random();
double yPos = worldModel.getHeight() * Math.random();
Food food = new Food(new Point.Double(xPos, yPos), (int) (Math.random() * WorldModel.maxFoodEnergy));
worldModel.addFood(food);
}
}
}
public void setPosition(Point.Double position) {
this.position = position;
}
public void setPosition(Point.Double position) {
this.position = position;
}
/**
* Selects a construction site
*
* @param xPixel the x pixel position on the displayed map.
* @param yPixel the y pixel position on the displayed map.
* @return selected construction site
*/
public ConstructionSite selectConstructionSiteAt(int xPixel, int yPixel) {
Point.Double clickPosition = convertToSettlementLocation(xPixel, yPixel);
ConstructionSite site = null;
Iterator<ConstructionSite> j = settlement.getConstructionManager().getConstructionSites().iterator();
while (j.hasNext()) {
ConstructionSite s = j.next();
if (!LabelMapLayer.getConstructionLabel(s).equals(Msg.getString("LabelMapLayer.noConstruction"))) {
double width = s.getWidth();
double length = s.getLength();
int facing = (int) s.getFacing();
double x = s.getXLocation();
double y = s.getYLocation();
double xx = 0;
double yy = 0;
if (facing == 0) {
xx = width / 2D;
yy = length / 2D;
} else if (facing == 90) {
yy = width / 2D;
xx = length / 2D;
}
// Loading Dock Garage
if (facing == 180) {
xx = width / 2D;
yy = length / 2D;
} else if (facing == 270) {
yy = width / 2D;
xx = length / 2D;
}
// Note: Both ERV Base and Starting ERV Base have 45 / 135 deg facing
// Fortunately, they both have the same width and length
else if (facing == 45) {
yy = width / 2D;
xx = length / 2D;
} else if (facing == 135) {
yy = width / 2D;
xx = length / 2D;
}
double distanceX = Math.abs(x - clickPosition.getX());
double distanceY = Math.abs(y - clickPosition.getY());
if (distanceX <= xx && distanceY <= yy) {
site = s;
break;
}
}
}
return site;
}
/**
* Convert a pixel X,Y position to a X,Y (meter) position local to the
* settlement in view.
*
* @param xPixel the pixel X position.
* @param yPixel the pixel Y position.
* @return the X,Y settlement position.
*/
public Point.Double convertToSettlementLocation(int xPixel, int yPixel) {
Point.Double result = new Point.Double(0D, 0D);
double xDiff1 = (getWidth() / 2) - xPixel;
double yDiff1 = (getHeight() / 2) - yPixel;
double xDiff2 = xDiff1 / scale;
double yDiff2 = yDiff1 / scale;
// Correct due to rotation of map.
double xDiff3 = (Math.cos(rotation) * xDiff2) + (Math.sin(rotation) * yDiff2);
double yDiff3 = (Math.cos(rotation) * yDiff2) - (Math.sin(rotation) * xDiff2);
double newXPos = xPos + xDiff3;
double newYPos = yPos + yDiff3;
result.setLocation(newXPos, newYPos);
return result;
}
/**
* Moves the site to a new position via the mouse's right drag
* @param b
* @param xPixel
* @param yPixel
*/
public void moveNewBuildingTo(Building b, double xPixel, double yPixel) {
Point.Double pixel = mapPanel.convertToSettlementLocation((int)xPixel, (int)yPixel);
double xDiff = xPixel - xLast;
double yDiff = yPixel - yLast;
if (xDiff < mapPanel.getWidth() && yDiff < mapPanel.getHeight()) {
//System.out.println("xPixel : " + xPixel
// + " yPixel : " + yPixel
// + " xLast : " + xLast
// + " yLast : " + yLast
// + " xDiff : " + xDiff
// + " yDiff : " + yDiff);
double width = b.getWidth();
double length = b.getLength();
int facing = (int) b.getFacing();
double x = b.getXLocation();
double y = b.getYLocation();
double xx = 0;
double yy = 0;
if (facing == 0) {
xx = width/2D;
yy = length/2D;
}
else if (facing == 90){
yy = width/2D;
xx = length/2D;
}
// Loading Dock Garage
if (facing == 180) {
xx = width/2D;
yy = length/2D;
}
else if (facing == 270){
yy = width/2D;
xx = length/2D;
}
// Note: Both ERV Base and Starting ERV Base have 45 / 135 deg facing
// Fortunately, they both have the same width and length
else if (facing == 45){
yy = width/2D;
xx = length/2D;
}
else if (facing == 135){
yy = width/2D;
xx = length/2D;
}
double distanceX = Math.abs(x - pixel.getX());
double distanceY = Math.abs(y - pixel.getY());
//System.out.println("distanceX : " + distanceX + " distanceY : " + distanceY);
if (distanceX <= xx && distanceY <= yy) {
Point.Double last = mapPanel.convertToSettlementLocation((int)xLast, (int)yLast);
double new_x = Math.round(x + pixel.getX() - last.getX());
b.setXLocation(new_x);
double new_y = Math.round(y + pixel.getY() - last.getY());
b.setYLocation(new_y);
xLast = xPixel;
yLast = yPixel;
}
}
}
public void moveNewBuildingTo(Building b, double xPixel, double yPixel, double turnAngle) {
Point.Double pixel = mapPanel.convertToSettlementLocation((int)xPixel, (int)yPixel);
double xDiff = xPixel - xLast;
double yDiff = yPixel - yLast;
if (xDiff < mapPanel.getWidth() && yDiff < mapPanel.getHeight()) {
//System.out.println("xPixel : " + xPixel
// + " yPixel : " + yPixel
// + " xLast : " + xLast
// + " yLast : " + yLast
// + " xDiff : " + xDiff
// + " yDiff : " + yDiff);
double width = b.getWidth();
double length = b.getLength();
int facing = (int) b.getFacing();
double x = b.getXLocation();
double y = b.getYLocation();
double xx = 0;
double yy = 0;
if (facing == 0) {
xx = width/2D;
yy = length/2D;
}
else if (facing == 90){
yy = width/2D;
xx = length/2D;
}
// Loading Dock Garage
if (facing == 180) {
xx = width/2D;
yy = length/2D;
}
else if (facing == 270){
yy = width/2D;
xx = length/2D;
}
// Note: Both ERV Base and Starting ERV Base have 45 / 135 deg facing
// Fortunately, they both have the same width and length
else if (facing == 45){
yy = width/2D;
xx = length/2D;
}
else if (facing == 135){
yy = width/2D;
xx = length/2D;
}
double distanceX = Math.abs(x - pixel.getX());
double distanceY = Math.abs(y - pixel.getY());
//System.out.println("distanceX : " + distanceX + " distanceY : " + distanceY);
if (distanceX <= xx && distanceY <= yy) {
Point.Double last = mapPanel.convertToSettlementLocation((int)xLast, (int)yLast);
double new_x = Math.round(x + pixel.getX() - last.getX());
b.setXLocation(new_x);
double new_y = Math.round(y + pixel.getY() - last.getY());
b.setYLocation(new_y);
xLast = xPixel;
yLast = yPixel;
}
}
}
/**
* Moves the site to a new position via the mouse's left drag
* @param s
* @param xPixel
* @param yPixel
*/
public void moveSite(ConstructionSite s, double xPixel, double yPixel) {
Point.Double pixel = mapPanel.convertToSettlementLocation((int)xPixel, (int)yPixel);
//double dx = xPixel - xLast;
//double dy = yPixel - yLast;
double width = s.getWidth();
double length = s.getLength();
int facing = (int) s.getFacing();
double x = s.getXLocation();
double y = s.getYLocation();
double xx = 0;
double yy = 0;
if (facing == 0) {
xx = width/2D;
yy = length/2D;
}
else if (facing == 90){
yy = width/2D;
xx = length/2D;
}
// Loading Dock Garage
if (facing == 180) {
xx = width/2D;
yy = length/2D;
}
else if (facing == 270){
yy = width/2D;
xx = length/2D;
}
// Note: Both ERV Base and Starting ERV Base have 45 / 135 deg facing
// Fortunately, they both have the same width and length
else if (facing == 45){
yy = width/2D;
xx = length/2D;
}
else if (facing == 135){
yy = width/2D;
xx = length/2D;
}
double distanceX = Math.abs(x - pixel.getX());
double distanceY = Math.abs(y - pixel.getY());
/*
System.out.println("xPixel : " + xPixel
+ " yPixel: " + yPixel
+ " xLast: " + xLast
+ " yLast: " + yLast
+ " dx: " + dx
+ " dy: " + dy
+ " xx: " + xx
+ " yy: " + yy
+ " x: " + Math.round(x*10.0)/10.0
+ " y: " + Math.round(y*10.0)/10.0
+ " distanceX: " + Math.round(distanceX*10.0)/10.0
+ " distanceY: " + Math.round(distanceY*10.0)/10.0
+ " pixel.getX(): " + Math.round(pixel.getX()*10.0)/10.0
+ " pixel.getY(): " + Math.round(pixel.getY()*10.0)/10.0
);
*/
if (distanceX <= 2 * xx && distanceY <= 2 * yy) {
s.setMousePicked(true);
//System.out.println("xx and yy is within range");
mapPanel.setCursor(new Cursor(Cursor.HAND_CURSOR));
Point.Double last = mapPanel.convertToSettlementLocation((int)xLast, (int)yLast);
double new_x = Math.round(x + pixel.getX() - last.getX());
double new_y = Math.round(y + pixel.getY() - last.getY());
double w0 = s.getWidth();
double l0 = s.getLength();
double f0 = s.getFacing();
BoundedObject b0 = new BoundedObject(new_x, new_y, w0, l0, f0);
if (!isCollided(b0)) {
s.setYLocation(new_y);
s.setXLocation(new_x);
}
else
s.setMousePicked(false);
}
else {
mapPanel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
//}
}
public BasicAI() {
wayPoints = new Vector<Point.Double>();
}
public boolean isPlayerNearScreenPos(int x, int y) {
Point.Double p = this.map.playerArrowScreenPos;
return p.distanceSq(x, y) < 9.0;
}
public Vector<Point.Double> getWayPoints() {
return wayPoints;
}