OxyPlot错误地绘制圆圈(有间隙)

时间:2017-12-17 15:31:33

标签: c# math oxyplot

我输入了这个:

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吸引了它:

enter image description here

如何解决?

1 个答案:

答案 0 :(得分:2)

这是因为

Math.Sqrt(16 - Math.Pow(x, 2))

NaN返回x = 4,因为16 - Math.Pow(x, 2)是通过双精度计算的。这意味着结果不等于0(在这种情况下为-3,5527136788005E-14)。像Math.Sqrt(-3,5527136788005E-14)这样的负平方根未定义,如MSDN所述。

你可以通过禁止负数来修复它。只需要计算最大值,0就像

Math.Sqrt(Math.Max(16 - Math.Pow(x, 2), 0))