Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate the scale's result?

Tags:

math

d3.js

I am new to d3.js library, and I am trying to understand the concept of domain and ranges.

I read Scott Murray's book, but I don't know how to calculate it.

I have this:

var scale = d3.scale.linear()
    .domain([100, 500])
    .range([10, 350]);

scale(100);  //Returns 10
scale(300);  //Returns 180
scale(500);  //Returns 350

I know that 100 units of my input represent 10 in my output, and the same with 500 and 350... but why 180 is returned for 300 as input? How can I calculate this value?

I read this tutorial too, which explains how to calculate it, but when I follow the method, I don't get the same value, i.e., scale(300) doesn't return 180.

Any idea why?

like image 317
Stone Avatar asked Nov 10 '16 19:11

Stone


People also ask

How do I calculate my grading scale?

First, add the number of standard points the student earned. Then, divide that sum by the total number of points possible. Once you have that percentage, you can convert it using a traditional grading scale as pictured above. This method works for the common 1-4 scale as well as for 1-3 or 1-5 scales.

How do you calculate the scale?

Divide the real life dimension of either length or width by that of the model. So, say the real life object had a length of 55m, and the model had a length of 50 cm, or 0.5m, then do 55/0.5. This is equal to 110.

How do you calculate scale ratio?

To find the scale factor, locate two corresponding sides, one on each figure. Write the ratio of one length to the other to find the scale factor from one figure to the other. In this example, the scale factor from the blue figure to the red figure is 1.6 : 3.2, or 1 : 2.

How do you use a 1 200 scale?

This can be interpreted as follows: 1 centimetre (0.01 metre) measured with a ruler on the plan would need to be multiplied by 100 to give the actual size of 1 metre. So on a 1:200 scale plan, if you measured a wall length as 1 centimetre the actual length of the wall would be 2 metres.


2 Answers

THE EQUATION OF THE LINE

To get the equation used in a D3 linear scale you only need to understand that such scale uses a linear interpolation. In simple words, all you need is the equation of the line created by the two points you're passing to D3 scale generator.

In your example...

.domain([100, 500])
.range([10, 350]);

...will give us two points, using the Cartesian coordinates system (x, y):

  • The first point is 100,10 (x = 100 and y = 10)
  • The second point is 500,350 (x = 500 and y = 350)

Keep in mind that in my explanation the x axis represents what in math is called the domain (in D3 lingo, also domain), that is, the set of input values, while the y axis represents what in math is called the image (in D3 lingo, the range), that is, the set of output values.

So, using those two points, this is the line you get:

enter image description here

Now let's explain visually what a linear scale like this one does:

Choose any point in the x axis (it can even be outside the domain). This is your input value (in the domain). Go up (or down) until you cross the red line. The y coordinate of the point where you cross the red line corresponds to your output value (in the image, or range).

Now, back to the equation:

Having our 2 points, we can now calculate the equation of the line.

The general equation of the line is:

enter image description here

Where y (also known as f(x) in most math books) is the range, and x is the domain.

The first step is finding m, which we can do using our 4 points:

enter image description here

Remember that:

  • x1 is the first value in the domain array (=100)
  • x2 is the last value in the domain array (=500)
  • y1 is the first value in the range array (=10)
  • y2 is the last value in the range array (=350)

Putting all these four values in the equation, it gives us that m is 17/20.

Now, solving the equation for b (using any of the two points)...

enter image description here

... we have that b is -75, which gives us our final equation:

enter image description here

And that's it. Using this equation, you can get any point in the image (range), relative to any domain input.

Example:

Let's calculate the output (range) for 125 (as in your comment). Very easy:

enter image description here

And that gives us... 31.25!

like image 90
Gerardo Furtado Avatar answered Jan 04 '23 13:01

Gerardo Furtado


Yes I will try to explain why you get 180.

We have that 300 is the value that ideally divide your domain into two subdomains of the same size, so we can do: 500 - 100 / 2 = 200 (this is the subdomain size), this implies that the middle value is located at:
200 + 100 = 300 (note that we add the starting point 100)

So with the range we do the same:
350 - 10 / 2 = 170 and then we add the starting point 170 + 10 = 180

like image 40
Daviz Avatar answered Jan 04 '23 13:01

Daviz