开发者

Help converting Python app to C#

Everyone,

here is a link to a small python app:

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

I think I've correctly converted it. (Source at bottom of post)

But, the Math.Acos always returns NaN. Is there a difference between the python version of acos and Math.Acos?

    private开发者_Go百科 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);

            }                
        }
    }       


What you need to do to get the angle from the dot product is to take away the lengths before you acos.

What python has:

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

What you're doing is not dividing in the Acos, but dividing after.

so:

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));

Of course change the variables, I was just trying to keep it similar to the python to help you understand :)


I think it's due to your translation of the Python expression (dot(p,q,r) / (dot(p,p,r) * dot(q,q,r)) **0.5). Exponentiation in Python has one of the lowest operators precedency-wise, so the square-root is being taken of the subterm dot(p,q,r) / (dot(p,p,r) * dot(q,q,r)). In your C# version, when calculating the value of the double 'u', you're only taking the square-root of the product of the last two terms, i.e. the (dotpq * dotpq).


The question really is what is the value of dotpq when the function gets called. It has to be a double value between -1 and 1 as stated in the docs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜