Rev | Line | |
---|
[76] | 1 | <!DOCTYPE html> |
---|
| 2 | <html> |
---|
| 3 | <head> |
---|
| 4 | <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> |
---|
| 5 | <title>Convex Hull</title> |
---|
| 6 | <script type="text/javascript" src="../../d3.js"></script> |
---|
| 7 | <script type="text/javascript" src="../../d3.geom.js"></script> |
---|
| 8 | <style type="text/css"> |
---|
| 9 | |
---|
| 10 | svg { |
---|
| 11 | border: solid 1px #aaa; |
---|
| 12 | background: #eee; |
---|
| 13 | } |
---|
| 14 | |
---|
| 15 | path { |
---|
| 16 | fill: lightsteelblue; |
---|
| 17 | stroke: #000; |
---|
| 18 | } |
---|
| 19 | |
---|
| 20 | circle { |
---|
| 21 | fill: #fff; |
---|
| 22 | stroke: #000; |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | </style> |
---|
| 26 | </head> |
---|
| 27 | <body> |
---|
| 28 | <script type="text/javascript"> |
---|
| 29 | |
---|
| 30 | var w = 960, |
---|
| 31 | h = 500; |
---|
| 32 | |
---|
| 33 | var vertices = d3.range(15).map(function(d) { |
---|
| 34 | return [ |
---|
| 35 | w / 4 + Math.random() * w / 2, |
---|
| 36 | h / 4 + Math.random() * h / 2 |
---|
| 37 | ]; |
---|
| 38 | }); |
---|
| 39 | |
---|
| 40 | var svg = d3.select("body") |
---|
| 41 | .append("svg:svg") |
---|
| 42 | .attr("width", w) |
---|
| 43 | .attr("height", h) |
---|
| 44 | .attr("pointer-events", "all") |
---|
| 45 | .on("mousemove", move) |
---|
| 46 | .on("click", click); |
---|
| 47 | |
---|
| 48 | update(); |
---|
| 49 | |
---|
| 50 | function update() { |
---|
| 51 | svg.selectAll("path") |
---|
| 52 | .data([d3.geom.hull(vertices)]) |
---|
| 53 | .attr("d", function(d) { return "M" + d.join("L") + "Z"; }) |
---|
| 54 | .enter().append("svg:path") |
---|
| 55 | .attr("d", function(d) { return "M" + d.join("L") + "Z"; }); |
---|
| 56 | |
---|
| 57 | svg.selectAll("circle") |
---|
| 58 | .data(vertices.slice(1)) |
---|
| 59 | .enter().append("svg:circle") |
---|
| 60 | .attr("transform", function(d) { return "translate(" + d + ")"; }) |
---|
| 61 | .attr("r", 3); |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | function move() { |
---|
| 65 | vertices[0] = d3.svg.mouse(this); |
---|
| 66 | update(); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | function click() { |
---|
| 70 | vertices.push(d3.svg.mouse(this)); |
---|
| 71 | update(); |
---|
| 72 | } |
---|
| 73 | </script> |
---|
| 74 | </body> |
---|
| 75 | </html> |
---|
Note: See
TracBrowser
for help on using the repository browser.