Jump to content
whipz

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

Recommended Posts

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 ): 

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Recently Browsing   0 members

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