Skip to content

Instantly share code, notes, and snippets.

@jacksonhenry3
Created July 10, 2012 20:33
Show Gist options
  • Save jacksonhenry3/3086073 to your computer and use it in GitHub Desktop.
Save jacksonhenry3/3086073 to your computer and use it in GitHub Desktop.
lissajous figure plotter
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path, .axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.area {
fill: lightsteelblue;
}
.line {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
.dot {
fill: green;
stroke: steelblue;
stroke-width: 1.5px;
}
</style>
<body>
<script src="http://d3js.org/d3.v2.js"></script>
<script>
xcoef = prompt("choose the 'a' in sin(a*t)",1);
ycoef = prompt("choose the 'b' in cos(b*t)",1)
var data = d3.range(0,10*Math.PI,.001).map(function(i) {
return {x: Math.sin(xcoef*i) , y: (Math.cos(ycoef*i))};
});
var margin = {top: 10, right: 10, bottom: 20, left: 40},
width = 600 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.linear()
.domain([-1.5, 1.5])
.range([0, width]);
var y = d3.scale.linear()
.domain([-1.5, 1.5])
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var line = d3.svg.line()
.x(function(d) { return x(d.x); })
.y(function(d) { return y(d.y); });
var svg = d3.select("body").append("svg")
.datum(data)
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
svg.append("path")
.attr("class", "line")
.attr("d", line);
svg.selectAll(".dot")
.data(data.filter(function(d) { return d.y; }))
.attr("r", 3.5);
d3.select(self.frameElement).style("height", "500px");
d3.select(self.frameElement).style("width", "600px");
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment