1

I have min and max value of the scale and i want it to divide the scale in N partitions.

Let i have Min = 0 and Max = 200.

That i have tried to get the interval for getting equal distance between two points as:

void Main()
{
    double s = 012345678;
    double e = 123456789;
    double fx = (e - s)/10;
    double index = s;
    while(index <= e)
    {
    Console.WriteLine(index);
    index += fx;
    }
}

which does some trick and somewhat divided it as i want, but not the solid solution. Sometimes the Min and Max values are too much high as:

Max: 564094825.8374691 Min: 544792373.69823289
Interval = (Max/ Min)/20;

Now it causing problem.

The Result distribution of scale result should be as:

Let xMin= 0 and xMax = 10, I want this scale to divide in 5 intervals
Then it should return 
0,2,4,6,8,10

Min and Max value could be negative as possible while drawing a graph for -5 to 5 x scale or -10 to -5.

I went through this- Take set of values and change scale , is it somewhat relevant to my problem???

Please suggest me the way to achieve this..

thanks in Advance.

2 Answers2

3

Suppose that the minimum possible value is $a$, and the maximum possible value is $b$. We want to map the interval $[a,b]$ to the interval $[0,200]$.

There are a great many possibilities. One of the simplest is a linear mapping, where $x$ is mapped to $f(x)$, with $f(x)=px+q$.

We want $f(a)=0$, so $pa+q=0$. We want $f(b)=200$, so $pb+q=200$.

Now solve the system of equations $pa+q=0$, $\,pb+q=200$ for $p$ and $q$. We get $$p=\frac{200}{b-a},\qquad q=-\frac{200a}{b-a},$$ and therefore $$f(x)=\frac{200}{b-a}\left(x-a\right).$$ You may want to modify this so that the scaled values are integers. If so, then a sensible modification is to use, instead of $f(x)$, the function $g(x)$, where $g(x)$ is the nearest integer to $f(x)$.

Added: If you want to have only $N$ values, equally spaced with bottom at $0$ and top at $200$, then $N-1$ should divide $200$. Let $D=\frac{200}{N-1}$. Take the integer nearest to $\frac{f(x)}{D}$, and multiply the result by $D$. For example, if you want $26$ different values $0, 8, 16, 24, \dots, 200$, then $N=26$, so $N-1=25$ and $D=8$. We find the nearest integer to $\frac{f(x)}{8}$ and multiply the result by $8$.

Remark: One important real world example is when you want to convert the temperature $x$, in degrees Fahrenheit, to temperature in degrees Celsius. In the Fahrenheit scale, the freezing point of water is $32$ degrees, and the boiling point is $212$. In degrees Celsius it is $0$ and $100$. So we want to transform the interval $[32,212]$ to the interval $[0,100]$. the relevant $f(x)$ is equal to $\frac{100}{212-32}(x-32)$, which simplifies to $f(x)=\frac{5}{9}(x-32)$.

André Nicolas
  • 507,029
  • Thanks @Andre for your nice explanation. please review the updated question modification and suggest me about that.. i want equal intervals between min and max values. i know only min and max value, not mapping some external scale. i am not doing exact that done in link specified in my question. – Niranjan Singh Jun 19 '12 at 05:03
  • Min and max look like all you need. If you only want $N$ equally spaced values, like $N=11$ (we want $N-1$ to divide $200$, the final rounding should be a little different from the one I described. I will add something about that to the post. – André Nicolas Jun 19 '12 at 05:16
  • +1:thanks @Andre for reply. i will check and wait for your edits.. – Niranjan Singh Jun 19 '12 at 05:23
  • @NiranjanKala: The first time I wrote out the modification, I made a mistake. It is now right. – André Nicolas Jun 19 '12 at 05:29
  • well, it's alright i will try to check @Gerry answer and your will also help others.. have a nice time.. – Niranjan Singh Jun 19 '12 at 05:35
1

If your Min is $m$ and your Max is $M$ and you want to divide the range between them into $N$ equal pieces, then let $h=(M-m)/N$ and let your division points be $$m,m+h,m+2h,m+3h,\dots,m+(Nh)=M$$

Gerry Myerson
  • 179,216