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

问题描述 投票:2回答:1

我输入了这个:

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

怎么解决?

c# math oxyplot
1个回答
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))
© www.soinside.com 2019 - 2024. All rights reserved.