Skip to content

Instantly share code, notes, and snippets.

@HarryStevens
Last active February 10, 2019 21:26
Show Gist options
  • Save HarryStevens/2b4c07abe73b9ea5843547d4f47086fe to your computer and use it in GitHub Desktop.
Save HarryStevens/2b4c07abe73b9ea5843547d4f47086fe to your computer and use it in GitHub Desktop.
Polygon Bounds
license: gpl-3.0
<!DOCTYPE html>
<html>
<head>
<style>
body {
margin: 0;
}
.polygon {
fill: steelblue;
}
.bounds {
fill: none;
stroke: black;
stroke-width: 2px;
}
</style>
</head>
<body>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://unpkg.com/geometric@1.0.11/build/geometric.min.js"></script>
<script>
var width = window.innerWidth,
height = window.innerHeight,
center = [width / 2, height / 2],
vertices = [
[center[0] - 100, center[1] - 100],
[center[0], center[1] - 130],
[center[0] + 100, center[1] - 100],
[center[0] + 150, center[1]],
[center[0] + 100, center[1] + 100],
[center[0], center[1] + 130],
[center[0] - 100, center[1] + 100],
[center[0] - 150, center[1]]
];
var input = d3.select("input");
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var polygon = svg.append("polygon")
.attr("class", "polygon")
.attr("points", vertices.join(" "));
var bounds = svg.append("polygon")
.attr("class", "bounds")
.attr("points", geometric.polygonBounds(vertices).join(" "));
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment