Click the Reset
button to generate new points.
Click the Next
button to move to the next iteration.
Note: The x axis and y axis are not currently on the same scale.
forked from lwthatcher's block: Random Points II
xxxxxxxxxx
<meta charset="utf-8">
<style> /* set the CSS */
.point {
fill: black;
}
.t_old {
fill: darkgrey
}
.toolTip {
pointer-events: none;
position: absolute;
display: none;
width: 120px;
height: auto;
background: none repeat scroll 0 0 #ffffff;
padding: 9px 14px 6px 14px;
border-radius: 2px;
text-align: center;
line-height: 1.3;
color: #5B6770;
box-shadow: 0px 3px 9px rgba(0, 0, 0, .15);
}
.toolTip:after {
content: "";
width: 0;
height: 0;
border-left: 12px solid transparent;
border-right: 12px solid transparent;
border-top: 12px solid white;
position: absolute;
bottom: -10px;
left: 50%;
margin-left: -12px;
}
.toolTip span {
font-weight: 500;
color: #081F2C;
}
</style>
<body>
<!-- load the d3.js library -->
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://www.numericjs.com/lib/numeric-1.2.6.js"></script>
<script>
// === Setup SVG ===
// set the dimensions and margins of the graph
var margin = {top: 20, right: 20, bottom: 30, left: 50},
width = 900 - margin.left - margin.right,
height = 450 - margin.top - margin.bottom;
// set the ranges
var x = d3.scaleLinear().range([0, width]);
var y = d3.scaleLinear().range([height, 0]);
// append the svg obgect to the body of the page
// appends a 'group' element to 'svg'
// moves the 'group' element to the top left margin
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
// === Tooltip display functions ===
var tooltip = d3.select("body").append("div").attr("class", "toolTip");
var mouseover = (d) => {
tooltip.style("opacity", 1.0)
.style("left", x(d.x) - 15 + "px")
.style("top", y(d.y) - 25 + "px")
.style("display", "inline-block")
.html("(" + d.x.toFixed(3) + ", " + d.y.toFixed(3) + ")" )
}
var mouseout = (d) => {
tooltip.style("opacity", 0.0);
}
// === Consensus Dynamics Variables ===
// Graph Laplacian
var L = [[ 1,-1, 0],
[-1, 2,-1],
[ 0,-1, 1]];
// change rate
var dt = 0.09;
// === Diffusion Functions ===
// random point generator
var generate_random = function(num_agents) {
var dataset = []
for (var i = 0; i < num_agents; i++) {
var x = d3.randomUniform(-50,50)();
var y = d3.randomUniform(-50,50)();
dataset.push({"x": x, "y": y});
}
return dataset
}
// gets next position
var update_position = function(Xt) {
// convert to vector format
var xt = Xt.map((n) => n["x"]);
var yt = Xt.map((n) => n["y"]);
// get derivaive
var x_dot = numeric.dot(L, xt);
var y_dot = numeric.dot(L, yt);
// IMPORTANT: scale results by dt!
x_dot = numeric.mul(x_dot, dt);
y_dot = numeric.mul(y_dot, dt);
// new point position
var x_new = numeric.sub(xt, x_dot);
var y_new = numeric.sub(yt, y_dot);
// format result
var result = []
for(var i = 0; i < Xt.length; i++) {
result[i] = {"x": x_new[i], "y":y_new[i]}
}
return result;
}
// === Drawing Stuff ===
var initialize = function() {
// scale the range of the data
x.domain([-50, 50]);
y.domain([-50, 50]);
// add the X Axis
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
// add the Y Axis
svg.append("g")
.call(d3.axisLeft(y));
}
var draw_points = function(data) {
svg.selectAll()
.data(data)
.enter().append("circle")
.attr("r", 5)
.attr("class", "point t_current")
.attr("cx", function(d) { return x(d.x); })
.attr("cy", function(d) { return y(d.y); })
.on("mouseover", mouseover)
.on("mouseout", mouseout)
}
// clears board and draws new random points
var reset = function() {
// clear previous points
svg.selectAll(".point").remove();
// generate one point for each agent
var data = generate_random(L.length);
// format the data
data.forEach((d) => {
d.x = +d.x;
d.y = +d.y;
});
// draw points
draw_points(data);
}
// draws next iteration of convergence
var next = function() {
old_points = svg.selectAll(".t_current");
// fade old points
old_points
.attr("class", "point t_old")
.attr("r", 4);
// draw new points
new_points = update_position(old_points.data());
draw_points(new_points);
}
// === Run This on page-load ===
initialize();
reset();
</script>
<button onclick="reset()">Reset</button>
<button onclick="next()">Next</button>
</body>
Modified http://www.numericjs.com/lib/numeric-1.2.6.js to a secure url
https://d3js.org/d3.v4.min.js
https://www.numericjs.com/lib/numeric-1.2.6.js