Math - Percentage to Range Conversion - math

I'm trying to solve a problem where I know a percentage (set by the user) but I need to find where that percentage is on a range of numbers, so for instance if the user sets it to 50% and the value range is -10 to 10 the value would be 0.
It seems simple in my head but I'm tripping up on this one.

This should work
pos = low + (high - low) * percentage

Related

Adding DBps scale from 0-100 within min/max values

I have a project going where i use a vue-slider for adjusting volume, and at the same time i also use vue-slider`s process to show current level on the actual output, however i have a mathematical problem thats above my head of thinking.
Lets say i have read a level of -15db, now my scale is going from -40db to +20db, however vue-slider`s process does not care about this value, it value is based on percentage, from 0 - 100.
So the question is, what equation can i use to get percentage(between 0-100%) of value(-15) within min(-40) and max(20)?
Example of the slider:
Solution i used are following:
let percent = 0
if (level > chGainMin) {
const translator = Math.abs(chGainMin)
const newMin = chGainMin + translator
const newMax = chGainMax + translator
const newLevel = level + translator
percent = ((newLevel-newMin)*100) / (newMax-newMin)
}
Using abs to turn negative minimum to a positive number, and append this number to min, max and value. Then calculate the percentage as usual.

Trilateration x-component is less than half the actual position

I'm using an array of ultrasonic receivers to estimate the location of an object, but the total length of the array is about 1 meter (all of them are on the same line).
By using the distance obtained, I tried using trilateration to see if I could estimate the position.
The problem starts here:
While the y-axis component of the position is reasonably close to the actual position, the x-axis component of the position is about half the actual position. In other words, if the actual position is (5,10)m, the obtained position is about (2.5,9.8)m.
The equation I used is
x = (r1^2 - r2^2 - x1^2 + x2^2) / (2*(x2-x1))
where r1 and r2 are the distances obtained for receiver 1 and receiver 2, and x1 and x2 are the x-coordinates of the receiver position.
I'm wondering if this is due to the small baseline, or if there's something about the math that I'm missing. Also, how should I go about fixing this problem?
I thought about just multiplying the x-component by 2, but it may just be a coincidence that it's about half the actual x-component right now.

How to compare two Ratios of Rectangles / Sizes to determine their percentages of differences?

I have a ractangle whose ratio is 80:40 and other rectangles with similar aspect ratios within a humbral for example 80:35, 85:45, that unbral is a decimal or integer.
My problem is that I need to compare other rectangles with the first one and determine a difference in their respective aspect ratios in percentages, for example: 80:30 is 20% different in aspect ratio than 80:40.
(20% is not a calculated data, it is an example idea, because I don't know how to do it).
Could it be that a totally opposite aspect ratio is 100% different? For example: 80:40 is 100% different than 40:80
Imagine that you have a collection of rectangles and a target rectangle and you have to filter the collection leaving only those rectangles that have an aspect ratio similar to the target rectangle.
Sample:
private float GetDiffRatio(FloatSize size1, FloatSize size2) {
float fixedR1 = size1.Width / size1.Height;
if (fixedR1 >= 0)
// Invert and negative.
fixedR1 = -(size1.Height / size1.Width);
float fixedR2 = size2.Width / size2.Height;
if (fixedR2 >= 0)
// Invert and negative.
fixedR2 = -(size2.Height / size1.Width);
float rDiff = fixedR1 - fixedR2;
return rDiff * 100f;
}
Test:
float diffRatio = GetDiffRatio(new FloatSize(100f, 50f), new FloatSize(50f, 100f));
Results = -100f
Test2 (inverted order of parameters):
float diffRatio = GetDiffRatio(new FloatSize(50f, 100f), new FloatSize(100f, 50f));
Results = 100f
I am not sure that this is a valid or correct form, I do not know if it can generate any condition that returns a wrong percentage of similarity.
The answer to this question depends a lot on what exactly you're trying to do or you're planning to uses this similarity function for. I find it very unintuitive to say that opposite aspect ratio leads to 0% similarity. I think comparing two rectangles r1=(2.1,2) and r2=(2,2.1) should be a lot more similar to each other then for example r3=(1,5) r4=(5,1).
This is not me saying that it couldn't be useful in some case to have a similarity function like your's, I just want to explain that it depends a lot on what you're doing ...
I would say a very obvious solution would be to just divide width by height of every rectangle and take as similarity-function s1 the absolute value from both values subtracted. So for my provided examples the result would be:
s1(r1,r2) = | 2.1/2 - 2/2.1 | = 0.0976...
s1(r2,r3) = | 1/5 - 5/1 | = 4.8
If it is also important that you have values between 0 and 1 you could additionally plug this values in for example this function ...
where
b must be smaller than 0 and is a parameter with what you can controll how fast the funciton converges to 1. YOu can play around with it here: https://www.desmos.com/calculator/nwlq3ujouq
In the case you really want something as you suggested, i would simply do the following:
You take your constraint that every rectangle is between the ratio a:b and c:d. Than you calculate x1=a/b and x2=c/d and then you interpolate the value from zero to one between those values so:
h(x1) = 0
h(x2) = 1
if you need more details on how to do this look here https://en.wikipedia.org/wiki/Linear_interpolation but i think it's very straight forward.
The similarity function s3 can then be build again with the absolute value of the difference
s3=| h(r1)-h(r2) |

How do you scale game difficulty with a curve?

I would just like to start by saying my calculus is terrible and I have next to no experience with using it.
I am trying to find an algorithm to help scaling in my game. Specifically it should scale the amount of waves that spawn per level. Ideally it will take any number as a level up to the max integer value. There would also be a minimum value and a maximum value that would be the minimum waves and maximum waves. So:
level = 0 to infinity
minValue = 3
maxValue = 40
result = an algorithm that will have a max curvature of the max value and shouldnt exceed it no matter what value the level is. I'm not sure how to calculate this but I think it would also need some kind of threshold that i could control to dictate the curvature based on the the level.
Try the next approach:
mult = Min(1, (level/MaxLevel)**Somepower))
minValue + (maxValue - minValue) * mult
Choose Somepower value suitable for your tasks. For example, value 2 gives parabola (note that value might be less than 1)
If you want more complex curve, show a picture of desired form.
Edit:
For the case when curve tends but does not become above some level, you can choose some function with horizontal asymptote. For example:
max * x /(x+1)
or
max * arctan(k*x) * 2 / Pi

How to find the average of a set of bearings [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do you calculate the average of a set of angles?
If I have a set of bearings, ranging from 1-360, how can I find the average? Usually to find the average one would add them all up and divide by the number of items. The problem here is that doing that in the case of [1, 359], 2 bearings, would result in in 180, which in fact should be 360. Any ideas?
Represent the angles as vectors with Norm=1 and average the sum.
x1 = {cos(a),sin(a)}
x2 = {cos(b),sin(b)}
(x1+x2)/2 = {(cos(a)+cos(b))/2,(sin(a)+sin(b))/2}
which means the angle for the mean is
atan2((sin(a)+sin(b)) /(cos(a)+cos(b)))
Just beware of controlling the possible overflow when the denominator is close to zero.
It isn't clear from your question what you're trying to define the "average" to be... for directions on a circle there is no clear-cut obvious notion of average.
One interpretation is the value x that is the closest fit to the set of provided values, in the least-squares sense, where the distance between two bearings is defined as the smallest angle between them. Here is code to compute this average:
In[2]:= CircDist[a_, b_] := 180 - Mod[180 + a - b, 360]
In[6]:= Average[bearings_] :=
x /. NMinimize[
Sum[CircDist[x, bearings[[i]]]^2, {i, 1, Length[bearings]}],
x][[2]]
In[10]:= Average[{1, 359}]
Out[10]= -3.61294*10^-15
So what you want is the middle of two bearings - what happens if you have {90, 270}? Is the desired answer 0 or 180? This is something to consider.. also what's the middle of three bearings?
One thing you could do is:
Take the first two bearings in your set
Work out the difference between the two in either direction (i.e. [1, 359] would give 2 degrees in one direction, and 358 in the other)
If you want the desired angle to be the middle of the acutest of the two, take that as your difference and add to the anti-clockwise most of the pair (i.e. 359)
Use this as the new bearing and the next (i.e. 3rd in set) as the other bearing, and repeat, until all are 'middled'.
Off the top of my head, I don't think this is going to be fair, it'll probably bias it in one direction though (i.e. maybe in preference of the later values in your set).

Resources