Last active
August 29, 2015 13:56
-
-
Save KKostya/8845691 to your computer and use it in GitHub Desktop.
Lienard -- Wiechert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
svg line {stroke: black; stroke-width: 3;} | |
svg #arrow {stroke: red; stroke-width: 1;} | |
svg circle {fill: red; stroke:black;} | |
svg #image {opacity: 0.5;} | |
.axis { font: 10px sans-serif; -webkit-user-select: none; -moz-user-select: none; user-select: none; } | |
.axis .domain { fill: none; stroke: #000; stroke-opacity: .3; stroke-width: 10px; stroke-linecap: round; } | |
.axis .halo { fill: none; stroke: #ddd; stroke-width: 8px; stroke-linecap: round; } | |
.slider .handle { fill: #fff; stroke: #000; stroke-opacity: .5; stroke-width: 1.25px; pointer-events: none; } | |
</style> | |
<body> | |
<table> | |
<tr><td><svg></svg> | |
<td> | |
<table> | |
<tr><td> β=v/c<td><div id="beta"></div> | |
<tr><td> L/c <td><div id="L"></div> | |
</table> | |
</table> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script src="Slider.js"></script> | |
<script> | |
var width = 500, height = 500; | |
var svg = d3.select("svg") | |
.attr("width", width) | |
.attr("height", height); | |
var beta = 0.8, | |
L = 1; | |
zoom = 2; | |
var x = d3.scale.linear().range( [ 0, width]).domain([ -zoom/5, 9*zoom/5]); | |
var y = d3.scale.linear().range( [height, 0]).domain([ -zoom, zoom]); | |
svg.append('line').attr('id', 'rail1'); | |
svg.append('line').attr('id', 'rail2'); | |
svg.append('circle') | |
.attr('id', 'charge1').attr('r',10) | |
.attr('cx', x(0)).attr('cy', y(0)); | |
svg.append('line').attr('id', 'arrow') | |
.attr('x1', x(0)).attr('y1', y(0)); | |
svg.append('circle') | |
.attr('id', 'charge2').attr('r',10) | |
.attr('cx', x( L)).attr('cy', y(0)); | |
svg.append('circle') | |
.attr('id', 'image').attr('r',10) | |
.attr('cx', x( L)).attr('cy', y(0)); | |
function redraw() | |
{ | |
x = d3.scale.linear().range( [ 0, width]).domain([ -zoom/5, 9*zoom/5]); | |
y = d3.scale.linear().range( [height, 0]).domain([ -zoom, zoom]); | |
svg.select("#rail1") | |
.attr('x1', x( 0)).attr('x2', x( 0)) | |
.attr('y1', y(-zoom)).attr('y2', y( zoom)); | |
svg.select("#rail2") | |
.attr('x1', x( L)).attr('x2', x( L)) | |
.attr('y1', y(-zoom)).attr('y2', y( zoom)); | |
plot(t) | |
} | |
function plot(t) | |
{ | |
var y2 = y(beta * t), | |
T = (t-Math.sqrt(L*L-(L*L-t*t)*beta*beta))/(1-beta*beta); | |
svg.select('#charge2') | |
.attr('cx', x( L)).attr('cy', y(beta*t)); | |
var x2 = L, y2 = beta*T, R = Math.sqrt(x2*x2+y2*y2) | |
nx = x2/R, ny = y2/R, b2 = beta*beta; | |
svg.select('#image') | |
.attr('cx',x(x2)).attr('cy',y(y2)); | |
var efactor = (1-beta*beta)/((1+beta*ny)*(1+beta*ny)*(1+beta*ny)*R*R); | |
svg.select('#arrow') | |
.attr('x2', x(efactor*nx)).attr('y2', y(efactor*(ny+beta))); | |
} | |
var t = -zoom/beta; | |
redraw(); | |
d3.timer(function() { plot(t); t += 0.01; if(t>zoom/beta) t=-zoom/beta;}); | |
// var s1 = slider([0.1,5]); s1.value(0.2); | |
// s1.callback(function(){ | |
// zoom = 1/s1.value(); | |
// redraw(); | |
// }) | |
// d3.select("#zoom").append("svg").call(s1); | |
var s2 = slider([0.2,0.99]); s2.value(0.8); | |
s2.callback(function(){ | |
beta = s2.value(); | |
t = -zoom/beta; | |
redraw(); | |
}) | |
d3.select("#beta").append("svg").call(s2); | |
var s3 = slider([0.5,5]); s3.value(1); | |
s3.callback(function(){ | |
L = s3.value(); | |
t = -zoom/beta; | |
redraw(); | |
}) | |
d3.select("#L").append("svg").call(s3); | |
</script> | |
</body> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function slider(dom) | |
{ | |
var margin = {top: 5, left: 15, right: 10, bottom: 5}, | |
width = 300 - margin.left - margin.right, | |
height = 40 - margin.top - margin.bottom, | |
brush = d3.svg.brush(), | |
handle, slider, | |
value = 0, | |
upd = function(d){value = d;}, | |
cback = function(d){}; | |
var x = d3.scale.linear() | |
.domain(dom) | |
.range ([0,width]) | |
.clamp(true); | |
function chart(el) | |
{ | |
brush.x(x).extent([0,0]) | |
.on("brush", brushed); | |
var svg = el.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/2+")") | |
.call(d3.svg.axis().scale(x).orient("bottom").tickSize(0).tickPadding(12)); | |
slider = svg.append("g") | |
.attr("class","slider") | |
.call(brush); | |
slider.selectAll(".extent,.resize").remove(); | |
slider.select(".background").attr("height",height) | |
handle = slider.append("circle") | |
.attr("class","handle") | |
.attr("transform", "translate(0,"+height/2+")") | |
.attr("cx",x(value)) | |
.attr("r",9); | |
function brushed() | |
{ | |
if (d3.event.sourceEvent) value = x.invert(d3.mouse(this)[0]); | |
upd(value); | |
cback(); | |
} | |
upd = function(v) | |
{ | |
brush.extent([v,v]); | |
value = brush.extent()[0]; | |
handle.attr("cx",x(value)); | |
} | |
} | |
chart.margin = function(_) { if (!arguments.length) return margin; margin = _; return chart; }; | |
chart.callback = function(_) { if (!arguments.length) return cback; cback = _; return chart; }; | |
chart.value = function(_) { if (!arguments.length) return value; upd(_); return chart; }; | |
return chart; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment