Skip to content

Instantly share code, notes, and snippets.

@MicrowaveDev
Last active August 3, 2020 16:21
Show Gist options
  • Save MicrowaveDev/f603da985d592fb281c83ef306e91be0 to your computer and use it in GitHub Desktop.
Save MicrowaveDev/f603da985d592fb281c83ef306e91be0 to your computer and use it in GitHub Desktop.
How to optimize ETH smart contract size
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