Created
August 3, 2020 16:18
-
-
Save MicrowaveDev/e0a79388fbe5cd57ad88d62161271f00 to your computer and use it in GitHub Desktop.
PolygonUtils
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library PolygonUtils { | |
struct PolygonData { | |
int256[2][] points; | |
} | |
function isInsidePolygon(PolygonData storage _polygon, int256[2] memory _point) public view returns (bool) { | |
bool inside = false; | |
uint256 j = _polygon.points.length - 1; | |
for (uint256 i = 0; i < _polygon.points.length; i++) { | |
bool intersect = ((_polygon.points[i][1] > _point[1]) != (_polygon.points[j][1] > _point[1])) && (_point[0] < (_polygon.points[j][0] - _polygon.points[i][0]) * (_point[1] - _polygon.points[i][1]) / (_polygon.points[j][1] - _polygon.points[i][1]) + _polygon.points[i][0]); | |
if (intersect) { | |
inside = !inside; | |
} | |
j = i; | |
} | |
return inside; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment