Skip to content

Instantly share code, notes, and snippets.

@tlfrd
Last active December 24, 2017 00:12
Show Gist options
  • Select an option

  • Save tlfrd/0f646e4a6c7da975e63f805d8f43a40b to your computer and use it in GitHub Desktop.

Select an option

Save tlfrd/0f646e4a6c7da975e63f805d8f43a40b to your computer and use it in GitHub Desktop.
Cut Out Tabs
license: mit

A function to draw flaps for a cut out and fold model. Code for parallel coordinates from here.

<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
</style>
</head>
<body>
<script>
var line1 = [{x: 200, y: 200},{x: 300, y: 300}];
var svg = d3.select("body").append("svg")
.attr("width", 960)
.attr("height", 500);
var line = svg.append("line")
.attr("x1", line1[0].x)
.attr("y1", line1[0].y)
.attr("x2", line1[1].x)
.attr("y2", line1[1].y)
.style("stroke", "black");
generateCutOutFlap(line1, 30, 5, 1);
function generateCutOutFlap(line1, outDistance, inDistance, direction) {
var t = calcTranslationExact(outDistance, line1, direction);
var x1 = line1[0].x - t.dx,
y1 = line1[0].y - t.dy,
x2 = line1[1].x - t.dx,
y2 = line1[1].y - t.dy;
svg.append("line")
.attr("x1", x1)
.attr("y1", y1)
.attr("x2", x2)
.attr("y2", y2)
.style("stroke", "red");
}
function calcTranslationExact(distance, line1, direction) {
var x1_x0 = line1[1].x - line1[0].x,
y1_y0 = line1[1].y - line1[0].y,
x2_x0, y2_y0;
if (y1_y0 === 0) {
x2_x0 = 0;
y2_y0 = targetDistance;
} else {
let angle = Math.atan((x1_x0) / (y1_y0));
x2_x0 = (direction*-1)*distance * Math.cos(angle);
y2_y0 = direction*distance * Math.sin(angle);
}
return {
dx: x2_x0,
dy: y2_y0
};
}
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment