Last active
August 3, 2020 16:21
-
-
Save MicrowaveDev/f603da985d592fb281c83ef306e91be0 to your computer and use it in GitHub Desktop.
How to optimize ETH smart contract size
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
contract PolygonChecker { | |
struct PolygonData { | |
int256[2][] points; | |
} | |
mapping(uint256 => PolygonData) polygons; | |
function addPolygonPoint(uint256 polygonId, int256[2] memory point) public { | |
polygons[polygonId].points.push(point); | |
} | |
function isInsidePolygonById(int256[2] memory _point, uint256 _polygonId) public view returns (bool) { | |
return isInsidePolygon(polygons[_polygonId], _point); | |
} | |
function isInsidePolygon(PolygonData storage _polygon, int256[2] memory _point) internal 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