每个人,
这是一个指向小型python应用程序的链接:
http://en.wikipedia.org/wiki/File:Beta-skeleton.svg
我想我已经正确转换了它。 (帖子底部的来源)
但是,Math.Acos总是返回NaN。 acos的python版本与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);
}
}
}
答案 0 :(得分:3)
要获得点积的角度,你需要做的就是在你的acos之前取走长度。
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以帮助你理解:)
答案 1 :(得分:2)
我认为这是因为你翻译了Python表达式(dot(p,q,r) / (dot(p,p,r) * dot(q,q,r)) **0.5)
。 Python中的指数具有运算符优先级最低的运算符之一,因此正方形根用于子项dot(p,q,r) / (dot(p,p,r) * dot(q,q,r))
。在你的C#版本中,当计算双'u'的值时,你只取最后两个项的乘积的平方根,即(dotpq * dotpq)
。
答案 2 :(得分:0)
问题的确是调用函数时dotpq
的值是多少。它必须是介于-1和1之间的双精度值,如docs中所述。