我正在 C# 中创建一个图表,该图表根据 Y(时间)绘制 X 值(距离)。 该图有两条线:一条要比较的线(我们可以称为 IdealDistance)和一条从外部输入生成的线(RealDistance)。
我想在添加时将 RealDistance X 值与 IdealDistance X 值进行比较,但无法弄清楚如何在已知时间返回 IdealDistance X 值(或 IdealDistance Y 值)。
IdealDistance 线由每秒间隔的多个数据点形成,所需的检查每 100 毫秒完成一次,因此不幸的是不能简单地使用 IdealDistance 数据点集。
- 如何获取给定 Y Axis 值处我的系列之一的 X Axis 值?
最佳答案
如果您采用两个数据点(a 和 b),您可以使用直线方程计算它们之间的任何 X 点的 Y(或任何 Y 点的 X):
public static int GetX(int y, Point a, Point b)
{
var m = CalculateSlope(a, b);
// Horizontal line (x-values are always the same)
if (m == 0.0)
return a.X;
var c = a.Y - a.X * m;
return Convert.ToInt32((y - c) / m);
}
public static int GetY(int x, Point a, Point b)
{
var m = CalculateSlope(a, b);
// Vertical line (y-values are always the same)
if (double.IsPositiveInfinity(m))
return a.Y;
var c = a.Y - a.X * m;
return Convert.ToInt32(m * x + c);
}
public static double CalculateSlope(Point a, Point b)
{
if (b.Y == a.Y)
return double.PositiveInfinity;
if (b.X == a.X)
return 0.0;
return (Convert.ToDouble(b.Y) - Convert.ToDouble(a.Y)) / (Convert.ToDouble(b.X) - Convert.ToDouble(a.X));
}
关于c# - 如何从 C# 中折线图的已知 Y 值获取 X 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28998605/