Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Because I have written something similar over and over again, I first wrote a class containing functions like:</p> <pre><code> /// &lt;summary&gt; /// Calculate the position, of a given value, within a certain range/size /// depending on the minimum and maximum (scale) for the value. /// &lt;/summary&gt; /// &lt;param name="data"&gt;Value for which to calculate the position.&lt;/param&gt; /// &lt;param name="minimum_value"&gt;Minimum value of the scale on which the value must lie.&lt;/param&gt; /// &lt;param name="maximum_value"&gt;Maximum value of the scale on which the value must lie.&lt;/param&gt; /// &lt;param name="size_begin"&gt;The begin value of the size on which the scale must be spread.&lt;/param&gt; /// &lt;param name="size_end"&gt;The end value of the size on which the scale must be spread.&lt;/param&gt; /// &lt;returns&gt;The respectively positions of the data value.&lt;/returns&gt; static public double Position_In_Size(double data, double minimum_value, double maximum_value, double size_begin, double size_end) { return size_begin + (((size_end - size_begin) / (maximum_value - minimum_value)) * (data - minimum_value)); } </code></pre> <p>This quickly became a really big class, in order to support all kind of types. C# doesn't support generics for value types out of the box.</p> <p>Recently I found a library which implements generics in .NET with a workaround.</p> <p>Using this library I now have a cleaner implementation. Using a generic interval class, I can 'map' values from one range to another, independant of type.</p> <pre><code>// Result will be 0. Interval&lt;int&gt; interval = new Interval&lt;int&gt;( 0, 10 ); double result = interval.Map&lt;double&gt;( 5, new Interval&lt;double&gt;( -100, 100 ) ); </code></pre> <p>I wrote this as part of some interpolation classes I was working on: <a href="http://whathecode.wordpress.com/2011/01/24/interpolation-no-more/" rel="nofollow">http://whathecode.wordpress.com/2011/01/24/interpolation-no-more/</a></p> <p>The source code can be found in my <a href="http://whathecode.wordpress.com/fcl-extension/" rel="nofollow">FCL Extension library</a>, in the namespace <em>Whathecode.System.Arithmetic.Range</em>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload