Speed up development with full-stack environments for every branch.Learn More
Linear Scales in C# like d3.scale.linear [d3.js]
Forked from ASP.net Form Submission.664 Runs 1538 Views 89 Copies
An example of how C# can support other framework idioms such as d3.js. In this example, I show how to create a linear scale functor that can be used to convert screen coordinates into a range from -1 to 1.
Linear Scales in C# like d3.scale.linear
This is an experiment to get linear scales in C# similar to the way d3.js defines and uses them.
In d3.js you can define a functor (borrowed from Scott Murray's tutorial on scales):
var scale = d3.scale.linear() .domain([100, 500]) .range([10, 350]);
to create a conversion function:
scale(100); //Returns 10 scale(300); //Returns 180 scale(500); //Returns 350
Can we do something similar in C#?
scale.Conversion linscale = new scale.Linear() .domain(100f, 200f) .range(-1.0f, 1.0f); linscale(100f); //Returns -1.0 linscale(150f); //Returns 0 linscale(200f); //Returns 1.0
This is just a quick experiment to show it can be done. I'm not sure if the language could be bent further to match d3's semantics. There may already be C# libraries that do this, but if not, maybe this serves as an example of how idioms from one language can be applied in another.