从图表中找到与用户输入的Y坐标相对应的X坐标 [英] Finding X coordinate corresponding to the user input Y coordinate from chart
问题描述
我有一个使用某些给定值创建的图表.我想要的是找到与Y坐标相对应的X坐标值(在用于绘制图表的值中不存在).
简而言之,假设我已经绘制了Y坐标1,2,3 ...和X坐标9,10,11....现在,如果我想找到与Y坐标值1.5相对应的X值,应该是什么代码? ?
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?
推荐答案
假设(1,9之间有一条线)和(2,10)
数学的:
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#代码:
C# Code:
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:
Point[] points = new Point[3] { new Point(1, 9), new Point(2, 10), new Point(3, 11) };
Console.WriteLine(ComputeY(points, 1.5));
但是:如果您没有输入正确的数组,那将不是一个很好的结果.例如(5,9)(1,10)(1,3)(3,3)
我以为那是上升的.我刚刚回答了您的问题,但是您可以自己批准解决方案以获得完美的软件.
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.
这篇关于从图表中找到与用户输入的Y坐标相对应的X坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!