OxyPlot 画圆不正确(有间隙)
OxyPlot draws circle incorrectly (with gap)
我输入了这个:
myModel.Series.Add(new FunctionSeries((x) => Math.Sqrt(16 - Math.Pow(x, 2)), -4, 4, 0.1, "x^2 + y^2 = 16") { Color = OxyColors.Red });
myModel.Series.Add(new FunctionSeries((x) => - Math.Sqrt(16 - Math.Pow(x, 2)), -4, 4, 0.1) { Color = OxyColors.Red });
OxyPlot 绘制了它:
如何解决?
这是因为
Math.Sqrt(16 - Math.Pow(x, 2))
returns NaN
for x = 4
因为16 - Math.Pow(x, 2)
是双精度计算的。这意味着结果不完全等于 0(在本例中为 -3,5527136788005E-14
)。 Math.Sqrt(-3,5527136788005E-14)
之类的负平方根未定义,如 MSDN.
中所述
你可以通过禁止负数来解决这个问题。取你计算的最大值和 0 like
Math.Sqrt(Math.Max(16 - Math.Pow(x, 2), 0))
我输入了这个:
myModel.Series.Add(new FunctionSeries((x) => Math.Sqrt(16 - Math.Pow(x, 2)), -4, 4, 0.1, "x^2 + y^2 = 16") { Color = OxyColors.Red });
myModel.Series.Add(new FunctionSeries((x) => - Math.Sqrt(16 - Math.Pow(x, 2)), -4, 4, 0.1) { Color = OxyColors.Red });
OxyPlot 绘制了它:
如何解决?
这是因为
Math.Sqrt(16 - Math.Pow(x, 2))
returns NaN
for x = 4
因为16 - Math.Pow(x, 2)
是双精度计算的。这意味着结果不完全等于 0(在本例中为 -3,5527136788005E-14
)。 Math.Sqrt(-3,5527136788005E-14)
之类的负平方根未定义,如 MSDN.
你可以通过禁止负数来解决这个问题。取你计算的最大值和 0 like
Math.Sqrt(Math.Max(16 - Math.Pow(x, 2), 0))