N
N
Network20202020-11-19 18:43:36
Mathematics
Network2020, 2020-11-19 18:43:36

How to check if a geographic point falls in a regular hexagon?

It is necessary to check whether a point on the map is included in a polygon in the form of a regular hexagon, for which the center and side lengths are known. The point, respectively, has a latitude and longitude, the center of the polygon too. The length of the sides in meters.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
W
Wataru, 2020-11-19
@Network2020

Subtract the center coordinates from the point coordinates. The hexagon is now centered at (0, 0).
Write the 6 equations for the sides of the hexagon as Ax+By+C=0 so that C is positive (if not, multiply by -1). Substitute the coordinates of the point into these 6 equations, they should all give positive values ​​(or zeros if the point on the border is considered to be inside in your problem).
Why does it work? We simply check that the point lies on the same side of all 6 lines as the center (0, 0).
How to write equations? Find the coordinates of 6 corners. If side = a, then coordinates of points (a, 0), (a/2, sqrt(3)*a/2), (-a/2, sqrt(3)*a/2), (-a, 0 ) ...
For the equation of one of the sides, take in the form A the difference in y for neighboring points, and in the form B the difference in x (but with a different sign). Then substitute the coordinates of one of the points there and take C so that it is 0.
You can compose all 6 equations on a piece of paper and encode in the program.
Example for the first side:
A = sqrt(3)*a/2-0 = sqrt(3)*a/2
B = a - a/2 = a/2
C = -A*a - B*0 = -sqrt (3)*a*a/2.
Since C is negative, we change the signs:
A = -sqrt(3)*a/2
B = -a/2
C = sqrt(3)*a*a/2
For the second line, it will be 0*xa*y+sqrt(3) *a*a/2 = 0
And yes, it works assuming the hexagon is small or flat. if the curvature of the earth plays a role in you, then everything becomes much more complicated.

A
AVKor, 2020-11-19
@AVKor

Here .

O
Ocelot, 2020-11-19
@Ocelot

The first rough check: if the distance from the point to the center is greater than the side of the hexagon, then the point does not even fall into the circumscribed circle, the answer is negative. If the distance is less than sqrt(3)/2 sides, then the point is in the inscribed circle, the answer is yes.
If between these values, an exact check is needed. Most likely, it will be optimal to find the side of the polygon closest to the point, knowing the angle of the vector between the point and the center; and then check the intersection of this vector with the selected side. Don't forget about the edge case when the vector passes through the top of the polygon.

H
habrspec, 2021-04-15
@habrspec

1. Convert latitude and longitude in degrees to UTM projection coordinates in meters. Now calculations can be done on a plane.
2. Implement an algorithm for checking whether a point hits a polygon.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question