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:
private double ComputeY(Point[] points, double x)
{
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:
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.