Jump to content

What is the best way to find the center of a tile ? or area ?


Recommended Posts

Posted
5 minutes ago, Stimpack said:

Try averaging out the x and y points of the tile's polygon and use the result as your new point. If the polygon isn't too complex i think it should be close to what you need.

 

https://en.m.wikipedia.org/wiki/Centroid#Of_a_finite_set_of_points

I figured it out using this 

int mouseSpotX = Math.toIntExact((long) hoverTile.getPolygon(getBot()).getBounds().getCenterX());
		int mouseSpotY = Math.toIntExact((long) hoverTile.getPolygon(getBot()).getBounds().getCenterY());

mouse.move really needs to have both x and y but also sometimes z ): 

 

Posted (edited)
49 minutes ago, LoudPacks said:

You can do some math and then do:

int x = x + width / 2

int y = y + height / 2 (or subtract depending on if up is -y or +y)

Ill give that a shot as well, I have found that different camera angles play a part, thats why I want mouse.move to contain z axis as this will help with the height why do I divide by 2 ? just asking 

 

Edited by whipz
Posted

Here is finding the center using Centroid finite set of points. I passed in Area#getPolygon(), not sure how it'd react to Position#getPolygon()

The function output

 Point2D.Double[6703.75, 3367.75]

 

In my use I will always be rounding up, but you may choose to do either way.

 

public Point2D.Double calcPolygonCenter(Polygon polygon) {
		double x = 0;
		double y = 0;
		int points = polygon.npoints;
		for (int i = 0; i < polygon.xpoints.length; i++) {
			x += polygon.xpoints[i];
		}
		for (int i = 0; i < polygon.ypoints.length; i++) {
			y += polygon.ypoints[i];
		}

		x = x / points;
		y = y / points;

		return new Point2D.Double(x, y);
	}

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...