Jump to content

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

## Recommended Posts

I have no idea on how to do it; everything I have tried is always just slightly off, its driving me insane ):

will post what I got if needed but obviously mines wrong lol

##### Share on other sites

isnt area like

position x = area.getcentertile(); or getcenter?

##### Share on other sites

13 minutes ago, raijin said:

isnt area like

position x = area.getcentertile(); or getcenter?

i dunno ill try and report back xD

15 minutes ago, raijin said:

isnt area like

position x = area.getcentertile(); or getcenter?

Nope that doesnt work there is no getCenter

##### Share on other sites

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.

##### Share on other sites

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.

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

##### Share on other sites

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)

##### 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
##### Share on other sites

Areas can be polygons, not rectangles. Not sure if you specifically meant rectangle areas.

##### Share on other sites

Edit: Nevermind

@whipz What exactly are you trying to achieve here?

Edited by Explv
##### Share on other sites

4 hours ago, Explv said:

Edit: Nevermind

@whipz What exactly are you trying to achieve here?

i want to get the mouse to move on the z axis on a tile ):

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

## Join the conversation

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

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...