gpt4 book ai didi

c# - 帮助将 Python 应用程序转换为 C#

转载 作者:太空宇宙 更新时间:2023-11-03 14:34:35 25 4
gpt4 key购买 nike

各位

这是一个小型 Python 应用程序的链接:

http://en.wikipedia.org/wiki/File:Beta-skeleton.svg

我想我已经正确地转换了它。 (来源在帖子底部)

但是,Math.Acos 总是返回 NaN。 python版的acos和Math.Acos有区别吗?

    private Random rnd = new Random();
private double scale = 5;
private double radius = 10;
private double beta1 = 1.1;
private double beta2 = 0.9;
private double theta1;
private double theta2;

private Point[] points = new Point[10];

public MainWindow()
{
InitializeComponent();
for (int i = 0; i < 100; i++ )
{
points[i] = new Point((rnd.NextDouble() * scale),
(rnd.NextDouble() * scale));
}

theta1 = Math.Asin(1/beta1);
theta2 = Math.PI - Math.Asin(beta2);
}

private double Dot(Point p, Point q, Point r)
{
var pr = new Point();
var qr = new Point();

//(p[0]-r[0])
pr.X = p.X-r.X;

//(p[1]-r[1])
pr.Y = p.Y-r.Y;

//(q[0]-r[0])
qr.X = q.X-r.X;

//(q[1]-r[1])
qr.Y = q.Y-r.Y;

return (pr.X*qr.X) + (pr.Y*qr.Y);
}

private double Sharp(Point p,Point q)
{
double theta = 0;

foreach(var pnt in points)
{
if(pnt!=p && pnt!=q)
{
var dotpq = Dot(p, q, pnt);
double t = Math.Acos(dotpq);
double u = Math.Pow((dotpq * dotpq), 0.5);

var tempVal = t/u;

theta = Math.Max(theta, tempVal);
}
}
return theta;

}

private void DrawPoint(Point p)
{
var e = new Ellipse
{
Width = radius/2,
Height = radius/2,
Stroke = Brushes.Red,
Visibility = Visibility.Visible
};

Canvas.SetTop(e, p.Y + radius);
Canvas.SetLeft(e, p.X + radius);

MyCanvas.Children.Add(e);
}

private void DrawEdge1(Point p,Point q)
{
var l = new Line
{
X1 = p.X,
Y1 = p.Y,
X2 = q.X,
Y2 = q.Y,
Stroke = Brushes.Black,
Width = 1,
Visibility = Visibility.Visible
};

MyCanvas.Children.Add(l);
}

private void DrawEdge2(Point p,Point q)
{
var l = new Line
{
X1 = p.X,
Y1 = p.Y,
X2 = q.X,
Y2 = q.Y,
Stroke = Brushes.Blue,
Width = 1,
Visibility = Visibility.Visible
};

MyCanvas.Children.Add(l);
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
foreach (var p in points)
{
foreach (var q in points)
{
var theta = Sharp(p, q);

if(theta < theta1) DrawEdge1(p, q);
else if(theta < theta2) DrawEdge2(p, q);

}
}
}

最佳答案

要从点积中获取角度,您需要做的是在计算之前去掉长度。

python有什么:

prq = acos(dot(p,q,r) / (dot(p,p,r)*dot(q,q,r))**0.5)

您所做的不是在 Acos 中划分,而是在之后划分。

所以:

int r = pnt;
int ppr = Dot(p,p,r);
int qqr = Dot(q,q,r);
int pqr = Dot(p,q,r);

double u = Math.Acos(pqr / Math.Sqrt(ppr * qqr));

当然要更改变量,我只是想让它与 python 保持相似以帮助您理解:)

关于c# - 帮助将 Python 应用程序转换为 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3753925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com