Last active
December 19, 2015 20:48
-
-
Save KKostya/6015508 to your computer and use it in GitHub Desktop.
Proof of principle: z^2 conformal map with d3js
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> | |
.line { fill: none; stroke: steelblue; stroke-width: 1.5px; } | |
</style> | |
<body> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script> | |
function Complex(re, im) { this.re = re; this.im = im; }; | |
Complex.prototype.clone = function() { return new Complex(this.re,this.im); }; | |
Complex.prototype.add = function(z) { this.re += z.re; this.im += z.im; }; | |
Complex.prototype.sub = function(z) { this.re -= z.re; this.im -= z.im; }; | |
Complex.prototype.mul = function(z) | |
{ | |
var tmp = this.re; | |
this.re = this.re*z.re - this.im*z.im; | |
this.im = tmp*z.im + this.im*z.re; | |
}; | |
Complex.prototype.div = function(z) | |
{ | |
var tmp = this.re, | |
n = z.re*z.re + z.im*z.im; | |
this.re = (this.re*z.re + this.im*z.im)/n; | |
this.im = ( -tmp*z.im + this.im*z.re)/n; | |
}; | |
var width = 960, height = 500; | |
var svg = d3.select("body") | |
.append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
var x = d3.scale.linear() | |
.range( [ 0, width]) | |
.domain([-1, 1]); | |
var y = d3.scale.linear() | |
.range( [height, 0]) | |
.domain([ -1, 1]); | |
var line = d3.svg.line() | |
.x(function(d) { return x(d.re); }) | |
.y(function(d) { return y(d.im); }); | |
var data = d3.range(0,1,0.1).map(function(r){return d3.range(-1,1,0.01).map(function(i){return new Complex(r,i);})}) | |
data.forEach(function(d){d.forEach(function(z) {z.mul(z.clone());})}); | |
svg.selectAll('path').data(data).enter().append('path') | |
.attr('class','line') | |
.attr('d',line); | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment