Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a chart created with some given values. What I want is to find the value of X coordinate corresponding to the Y coordinate(which is not present in the values that was used to plot the chart).
Simply, suppose I have plotted with Y coordinates 1,2,3... and X coordinates 9,10,11.... Now what should be the code if I want to find the X value corresponding to Y coordinate value, 1.5?
Is it possible to find the above without using CursorPositionChanged event, i.e by passing Y coordinate through textbox?
Posted
Comments
Dr.Walt Fair, PE 16-Feb-12 13:40pm    
It's not very clear to me what the problem is. Can you just do a linear interpolation between values or is there something else to consider?
Member 7748670 16-Feb-12 14:03pm    
Plotted graph with:
X-values=1,2,3
Y-values=9,10,11
And I want intermediate values, i.e, how do i get X value for 9.5(Y value)..?
thanks....

1 solution

Hi, Suppose that there is a line between (1,9) and (2,10)

Mathematical:
Y - y1 = (X - x1)(y2 - y1)/(x2 - x1)

Y - 9 = (X  - 1)(10 - 9)/(2 - 1)

Y = X + 8



If X = 1.5 Then Y = 1.5 + 8 = 9.5



C# Code:
C#
private double ComputeY(Point[] points, double x)
       {
          // points = (from p in points
          //          orderby p.X ascending
          //          select p).ToArray();

           var myPoints = (from p in points
                           where p.X >= x || p.X <= x
                           select p).Take(2);

           double x1, x2, y1, y2;
           if (myPoints.Count() == 2)
           {
               Point[] twoPoints = myPoints.ToArray();

               x1 = twoPoints[0].X;
               x2 = twoPoints[1].X;
               y1 = twoPoints[0].Y;
               y2 = twoPoints[1].Y;

               if (x2 - x1 == 0)
                   throw new Exception("Division by zero!");

               double z = (y2 - y1) / (x2 - x1);

               return x * z - x1 * z + y1;
           }

           throw new Exception("2 points not found");

           return 0;
       }



Then:

C#
Point[] points = new Point[3] { new Point(1, 9), new Point(2, 10), new Point(3, 11) };


Console.WriteLine(ComputeY(points, 1.5));


But: Not a good result if you don't enter a correct array. For example (5,9) (1,10) (1,3) (3,3)

I supposed that was ascending. I just answered to your question, but you can approve the solution by yourself to get a perfect software.
 
Share this answer
 
v5
Comments
Member 7748670 17-Feb-12 10:12am    
thanks for the answer, but i guess the above formula is applicable only for straight line. Can I apply the same formula for a curve, or is there any other formula?
thanks!
Shahin Khorshidnia 17-Feb-12 14:55pm    
And Also. The solution is not for only a straight line.
In this case you divided a curve or line to 3 segments. It causes 3 various equations.

If you have more points and bring closer together, the acurate of the answer will be more relaible.
Shahin Khorshidnia 17-Feb-12 14:43pm    
You're welcome.

You must find out equation of the curve. It depends on your mathematical known facts.

Find out the equation for example: y = 2* X ^ 2 - 3 * X - 8

then you can solve.

For your question, these points: (1,9) (2,10) (2,11) only mean a straight line and I've solved it for you. You already have your answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900