Correlate two floats using c# [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I know im an idiot and I am sure this is simple math. But I cannot seem to wrap my head around it here is my situation
When X = 3, I need Y = 0, and when X = 0 I need y = 1;.
I am trying to fill a progress bar based upon how low X is.
The value to fill the progress bar (Y) must be between 0 and 1.
Math?

// "Single" is just like "float"
Single y = (3.0f - x) / 3.0f;
So that
x=3 -> y=0.00
x=2 -> y=0.33
x=1 -> y=0.66
x=0 -> y=1.00
Alternatively:
// different points of view are better
Single y = -(x - 3.0f) / 3.0f;

As I know best from my high school:
y = a*x + b
You must solve equations:
0 = a*3 + b and
1 = a*0 + b
a = -b/3; b =1
So your equation is: y=-1/3*x+1
private float GetProgressValue(float x)
{
return x/-3f + 1f;
}

Related

The result of calculation deviates after some digits [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
Result of calculation shown below should be equal to 0,007306897 (I referred from 2 books) . But when i check result from Watch 1, i see that result is equal to 0,007306882. I split the process into some parts. And the problem is occurring when c is calculating.
///Declarations
double sigma = 1.00000000;
double a,b,e,c;
a = (1 / Math.Sqrt(2 * Math.PI)); //calculated properly
c = -(i * i + j * j) / 2.00000000 * (sigma * sigma); //i and j are equal to -2
e = Math.E; //calculated properly
b = Math.Pow(e, c);
result=a * b;
Unfortunately the double type is not highly accurate. The link below shows that some numbers like 1.05 can not be stored accurately by the double type.
http://www.binaryconvert.com/convert_double.html
For normal usage it is usually accurate enough, if you need accuracy to the level you describe you probably need to use something like binary coded decimal. That will mean the normal math library won't work. You could try asking on some physics sites to see what they use when modelling complex systems to maintain accuracy.

Combinations in n-dimensions - How determine if Points see each other (If they are on same axis) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
In n-dimensional grid (Max. 10^7 dimensions) are two points. They have imaginary sensors on every axis.
I need algorithm what will calculate all possible options when these two points can spot themselves.
Formal written from my task document (translated to english):
Let A with the coordinates (a1, a2, ..., an) and B with the coordinates (b1, b2, ..., bn)
are two points in n-dimensional space and exists i ∈ 1, 2, ..., n such that ai = bi then A and B sees each other
Example:
In 1-dimensional space with length 10 is total 45 combinations how to put 2 points when they see each other (They see each other every time).
It is easy combination 10C2 (10 of 2) = 45
How to calculate it in 2,3,4,...,10^7 dimensions by program (I prefer C#)?
Proper test data what i have:
Input:
1
10
Output: 45
Input:
2
5 8
Output: 220
Input:
3
8 12 11
Output: 14784
More explanation:
Output is number of combinations when two points in space see each other (are on same axis). In 1 dimensional space is only one axis so they see each other always. In 2 dimensional space are 2 axis, so they can see each other only in some case
This image example explaining more than text i think
I'm sure it's correct.
C(x,y) is combination x of y.
Lets say we have one dimention, lets call it X of lenght 8. There are C(8,2) = 8*7/2 = 28 possibilities to see eachother.
When we add second dimention, named Y, of lenght 12 now we have 12 lines parallel to X. So we have 12*28=336 possibilities to being found on all the lines parallel to X. Now, on Y dimention we have C(12,2) = 66 possibilities. And there are 8 lines like that so 66*8=528.
In total: 336 +528= 846 possibilities
Now lets add another dimention, labeled as Z with lenght of 11. There are C(1,2) = 11*10/2 = 55 in one line and (atention) we have 8*12 lines like that. So is it's 55*8*12 = 5280 possibilities!
Now in total we have:
Paralel to X axis: C(8,2)*11*12 = 3696
Parallel to Y axis C(12,2)*8*11 = 5808
Parallel to Z axis C(11,2)*8*12 = 5280
TOTAL = 14784
In general the formula for n dimentions with n1,n2... nk lenghts is:
Sum of C(ni,2) * (n1*n2...*nk)/ni
Or shorter:
sum of (n1*n2*n3...nk)/2 * (ni-1)
example:
dimentions with 3,8,9,11:
(3*8*9*11)/2*(3-1) = 2376
(3*8*9*11)/2*(8-1) = 8316
(3*8*9*11)/2*(9-1) = 9504
(3*8*9*11)/2*(11-1) = 11880
Total = 32076
The easiest equasion:
(n1*n2*n3...ni)(n1+n2+...ni - k)/2, where ni are lenghs, and k is number of dimentions
I have create sample code so you can try it. i have check it's working fine.
Equation is : C(n,r)=n!/(r!(n−r)!)
Example:
1. 10C2=45
2. 10C3=120
public void Calc()
{
int result= nCr(10, 3);
}
public int nCr(int n,int r )
{
int nValue=1;
int rValue = 1;
for (int i = n-r+1; i <= n; i++)
{
nValue = nValue*i;
}
for (int i = 1; i <= r; i++)
{
rValue = rValue*i;
}
return nValue/rValue;
}

Solar System program [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I want to make a program with solar system planets.I have the Sun and planets (Mercury, Venus, Earth .. ) and i want to make the planets to move around the sun.
Here is the math part
Where
X=R1 * cos α Y=R2 * sin α α = 1
I try to implement it like this
double x;
double y;
x = (earth.Location.X - sun.Location.X) * Math.Cos(1);
y = (earth.Location.Y - sun.Location.Y+30) * Math.Sin(1);
earth.Location = new Point(Convert.ToInt32(x), Convert.ToInt32(y));
but is not really working,it's dissapearing from form, can someone help me ?
P.S: i use a timer for that code
Don't try to calculate the new position based on the old position. Instead, use the sun's position (fixed, I assume), the distance of earth from the sun, and the angle. Change the angle when you want to move the earth.
You need some setup like this:
// angle
double alpha = 0; // radians, so from zero to 2*PI is a circle.
// distance from the sun
double r1 = 200; // or whatever you decide
double r2 = 100;
How to calculate Earth's position given the value of alpha:
x = sun.Location.X + (r1 * Math.Cos(alpha));
y = sun.Location.Y + (r2 * Math.Sin(alpha));
Next, you probably want to look at making alpha change value every few milliseconds so that Earth will move.

Need help to convert formula into 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 years ago.
Improve this question
I got this formula,
R/Rs = (5800/9500)2(2.5123.37)1/2 = 1.76
How do I turn that into C# so that the value is 1.76. Don't understand what you do with the 2 and 1/2?
Formula is from http://skyserver.sdss.org/dr5/en/proj/advanced/hr/radius1.asp
You are looking for Math.Pow
Math.Pow(5800d/9500d, 2)*Math.Pow(Math.Pow(2.512, 3.37),0.5);
And using 5800d/9500d is important here (forcing double, one of the d's should do), as it would otherwise do integer division, leaving you with 0^2 and overall a big 0...
If you put this into a method taking the necessary double values that should be irrelevant.
You can do :
double res = Math.Pow(5800 / 9500d, 2) * Math.Sqrt(Math.Pow(2.512, 3.37));
Console.WriteLine(res.ToString("0.00"));
output :
1.76
Working demo
A power of 0.5 is a square root.
Its
double i = 5800.0 / 9500;
i = Math.Pow(i, 2);
double x = Math.Pow(2.512, 3.37);
x = Math.Sqrt(x);
x = x * i;
x = Math.Round(x, 2);
OR
Math.Round(Math.Pow(5800.0 / 9500, 2) * Math.Sqrt(Math.Pow(2.512, 3.37)), 2)
The trick is here is in the first line itself. If you will divide 5800 by 9500, it will return zero as division will happen in integers. So to do an actual division resulting in fractions one the the values have to be converted into decimal which i did by converting 5800 to 5800.0

unrecognized or unsupported array type exception in C# [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Image conversion in OpenCV C#
WeightedImg.Bitmap.SetPixel(x, y,Color.FromArgb((int)Math.Ceiling(color * R),
(int)Math.Ceiling(color * G),(int)Math.Ceiling(color * B)));
this line of code produces the exception above .. any one knows the solution ?
and what does the exception mean?
thanks a lot
You need to set the alphachannel, as in *A*rgb
The alphachannel set's the transparency of the color
you really need to give us more to work on. how are those variables defined? what's the exception and stack trace print? this equivalent of what you're posting, for example, works as a charm on my machine
Bitmap b = new Bitmap("somefile.bmp");
double color = 1, R = 2, G = 3, B = 4;
int x = 1, y = 1;
b.SetPixel(x, y, Color.FromArgb((int)Math.Ceiling(color * R), (int)Math.Ceiling(color * G), (int)Math.Ceiling(color * B)));

Categories