Incrementing across a changeable distance 0-1 keeping time equal [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm unable to think through this one I think it's one of those moments where the answer is really simple but I'm too close to the problem to see the solution.
I have a distance that's changeable and an object that has to traverse this distance in the same time regardless of length.
The start of the distance is valued as 0 and the end of the distance is valued as 1.
Obviously the incrementation will be smaller the larger the length to keep the times equal.
What formula could I use to calculate the 0-1 incrementation but keep the time taken equal.
I know it seems overly complicated way to increment but it's part of the third party plugin I've been given.
I'm coding in C#.
Thanks.
[EDIT]
Sorry I wasn't very clear.
For incrementation the start point is always 0 and the end point is always 1.
So the object can move += 0.5 for example.
so when the length increase from say 30 to 65 it should take longer to increment from 0 to 1.

So you are looking for a way to have a number x in the range [0,1] that maps to some y in some arbitrary range [min,max], and are looking for the increment value a such that if x -> y then x + a -> y + b for some constant b? If I have understood your question correctly, then your a value should be:
a = b / (max - min) note: make sure to format this correctly for C#, especially be sure to cast and that sort of thing.
This is basically saying that a should be the fraction of the range that a spans, that if b is half the range from min to max, then a should be 0.5, and if b spans one fifth the range, a should be 0.2.

Related

How to find even distributed values between lower and upper boundary in C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
Let's say I have two values (lower boundary = 0.5, upper boundary = 100.0). Then I want to split this range 40 times so that the first is 0.5 and the 40th is 100.0. How can I find the 38 values between 0.5 and 100.0 so that the distance between the values is always the same?
This sounds like a homework? You should be really able to solve this simple problem.
Always split your task into steps. Before you think of any code, you must understand the problem. Then find a solution. You can phrase the solution in words and pseudo code. Finally, you can try to implement this solution:
First you must find the appropriate algorithm:
Identify the input and the output (result):
a) calculate the range of your lower_bound=0.5 (input) and upper_bound=100 (input): range=upper_bound-lower_bound
b) Then divide range by steps=40 (input) to get the interval.
c) Finally, walk from lower_bound to upper_bound by incrementing each step by interval to get each value that belongs to the range.
Implement the algorithm
void Main()
{
double steps = 40;
double lowerBound = 0.5;
double upperBound = 100;
double range = Math.Abs(upperBound - lowerBound);
double interval = range / steps;
var rangeValues = new List<double>();
for (double step = lowerBound; step <= upperBound; step += interval)
{
rangeValues.Add(step);
}
}
Test and improve the algorithm to improve performance and readability. For example rename variables and move code into methods. by moving code into methods that accept the input as parameters, you can convert the hard-coded algorithm into a dynamic one.
Make the algorithm robust (important).
For example, you must ensure that the variable steps is never zero to avoid an DivideByZeroException exception. For this reason you must validate the input before you can run the algorithm.

How to calculate a logical curve along a set of values? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am trying to take a float value, with an arbitrary minimum and maximum possible value, and convert it to a linear scale, for representation on a bar-shaped indicator. The problem is, I can't just lerp it between the minimum and maximum, because the maximum value will always be dramatically higher than the minimum value. I have an array of arbitrary values that I want to act as intermediate points between the minimum and maximum. Now I just need to calculate a logical best-fit curve through the points. Each value is always larger than the last, and the rate of increase in value accelerates the further up you go, but there is no simple formula for calculating this rate of acceleration.
Here's an example of the values that may be used:
6.0, 13.5, 30.0, 75.0, 375.0
where 6 is the minimum, and 375 is the maximum.
If x is exactly one of these values, I would want a simple value depending on how many total values there are, I.E 0, 0.25, 0.5, 0.75, 1. The issue is calculating the in-between values.
How would I go about achieving this? I apologize if a question like this has already been asked, as it feels like a common problem, although I didn't know what to search for. If this has already been answered before, please just point me in the right direction.
Reposting my comment as an answer, as requested.
If a curve might be y(x) = k^(ax+b), take logs of both sides and you have a linear relation. As pointed out tho, this is maths not programming.
I’d pick k = 2, e or 10 for easier implementation; a & b you work out from data.

Explain this c# code please [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Can you please explain what's going on in this code (how is it multiplying by 4 as said in comments in code?):
public static int GetNextSize(int i)
{
//multiply it by four and make sure it is positive
return i > 0 ? i << 2 : ~(i << 2) + 1;
}
Is there any better or cleaner way to do this? or is this one the optimum one?
Also, any practical situations where this (or this type of) code will be helpful?
Thanks.
The ? is the ternary operator, effectively a returnable if/else statement
if (i>0)
return i multiplied by four (bitshift to the left two)
else
return negative i multiplied by four
The ~x+1 means two's compliment and add one, effectively making it a negative number. The x here happens to be i<<2
It looks like some optimized C-like code to me.
For #2, are you referring to the logical OR operator?
a || b=c
Since a is evaluated first, the total expression will be true if a is true, so b=c is only evaluated is a is false. This effectively means if not a: b=c
if i is positive:
it will shift the bits by two to the left, which is effectively the same as multiplying by 4.
if i is not positive (is negative or zero), it will again multiply by 4, then negate all the bits (that's what ~ does) and add 1 (due to 2s complement -- it's necessary for positive numbers).

Regarding logical equation on my C programming [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a situation on my C programming here and just wondering whether my solution is the correct way:
I have a LED display with particle count sensor and will show 6 digit of seven segment numbers as the count value. The sensor will give voltage input value. The input is from 0V to 10V. So the range of 0V-10V need to be shown in the display as 000000 to 999999 count.
My solution is:
Display number = Input voltage * 99999.9
For example:
Display number = 10.000*99999.9=999999
Display number = 5.500*99999.9=549999
Display number = 2.300*99999.9=229999
Is this the correct solution? I notice that I will get a lot of 9 on the display value.
The most usable and user friendly solution is to ignore the fact that your most significant digit is capable of displaying up to 9 and simply multiply by 10000 unless you desperately need the maxim resolution in which case simply use a scale factor of 100000 and document that your range is 0-9.99999.
My reasoning is that it is better to either loose one digit in the accuracy across the whole range or clip just the maximum value than to have an error across the entire range.

How to Program 'Chance'? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
i have an idea of something but i don't have a clue how to do it :D
basically just like in the games for example, every 6 hours you can open a chest where you can grab some item for free. Now i want to have a chest that the user can open every 6 hours, i have the items written in mysql server where every item gives experience points to the user. but how to do it?
lets say the database have 15 items, now every item have its chance value for example
nothing = 75% of getting it
sword_1 = 15% of getting it
sword_2 = 30% of getting it
any idea how to do it?
Define a range of % to get an item
Example - sword_1 = 0-15%, sword_2 = 15-45%
Note that the numbers above don't add up to 100%, so unless you really want return more than one time, sometimes.. the above ranges takes care of getting nothing.
If you want multiple rewards on a roll, simply adjust the ranges accordingly.
Generate a random number between 0 and 100. For the items where generated number falls within the range, those are your rewards won for that round.
You could assign a numeric value to each of your items in the table, the percentage of getting it would be the difference between it and its next lowest number.
Nothing, 60 (60% chance)
sword_1, 70 (10% chance)
sword_2, 100 (30% chance)
Make sure that your max item is 100.
You can then do a select from that table with a random number:
SELECT * FROM ChestItems WHERE ChanceValue < FLOOR(RAND() * 100) ORDER BY ChanceValue DESC LIMIT 1;
There are several (and much better) ways you can do this. This is just a quick example.

Categories