-3

This formula kinda don't work

I tried to use this formula but it looks like it won't give me right answers, I hope that I can use all values for a linear mapping I hope you guys can help me :) if I need to rewrite this post I would not mind

All of the input is: map(200,1, value,0,1); It never gives between 0 1. Thank you again, guys, here is code

function map(max_value, first_bottom, first_top, second_top, second_bottom){
    y=(max_value - first_bottom) / (first_top - first_bottom) * 
    (second_top - second_bottom) + second_bottom;
}
Matija
  • 46
  • 8
  • 4
    why it has C# tag? this is not C# code ... – Selvin Sep 06 '19 at 13:37
  • This looks to be JavaScript code, and the formula you're using isn't correct. Also, this has several answers already. [Here](https://stackoverflow.com/questions/5294955/how-to-scale-down-a-range-of-numbers-with-a-known-min-and-max-value) is the highest rated I could find. – Hazel へいぜる Sep 06 '19 at 14:28
  • Possible duplicate of [How to scale down a range of numbers with a known min and max value](https://stackoverflow.com/questions/5294955/how-to-scale-down-a-range-of-numbers-with-a-known-min-and-max-value) – Hazel へいぜる Sep 06 '19 at 14:29
  • @Selvin I test code on JS so it is kinda faster but implementing in c# sorry hahaha – Matija Sep 06 '19 at 18:03

1 Answers1

0

There are several great answers for this on the web (here on SO and there Stats.SE are two examples in SE networks alone).

However, it looks as though you've written the formula incorrectly. Below is a correct implementation broken down to better visualize the math that is happening.

function linearMap(currentValue, currentMin, currentMax, newMin, newMax) {
    var newSize = newMax - newMin;
    var oldSize = currentMax - currentMin;
    var oldScale = currentValue - currentMin;
    return (newSize * oldScale / oldSize) + newMin;
}

Feel free to give this a try over on JSFiddle or .NET Fiddle. I've also included it in a snippet below in case either of those links die out.

function linearMap(currentValue, currentMin, currentMax, newMin, newMax) {
    var newSize = newMax - newMin;
    var oldSize = currentMax - currentMin;
    var oldScale = currentValue - currentMin;
    return (newSize * oldScale / oldSize) + newMin;
}
var val = 8.3;
var oldMin = 0;
var oldMax = 10;
var newMin = 0;
var newMax = 1;
var result = linearMap(val, oldMin, oldMax, newMin, newMax).toFixed(2);
document.getElementById("result").innerText = 'Mapping a value of ' + val +                           ' in an existing range of ' + oldMin +
                                              ' - ' + oldMax +
                                              ' to a new range of ' + newMin +
                                              ' - ' + newMax +
                                              ' yields a result of ' + result;
html, body {
  margin: 0;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: #555;
  background-color: #fc0;
  background-image: linear-gradient(45deg, #fc0, #ff0);
  background-image: -webkit-linear-gradient(45deg, #fc0, #ff0);
}
<h1 id="result"></h1>
Hazel へいぜる
  • 2,751
  • 1
  • 12
  • 44