c#得到零星的结果

时间:2012-01-30 20:02:43

标签: c# graphing

所以我真的被困在这几个小时,这非常令人沮丧。我得到了零星的结果。该程序有时会工作,但在其他时候,它找不到我正在寻找的“切线”之一,即使数学上肯定是一个。

我是c#的新手所以,如果有人有时间帮助一个菜鸟,我们将非常感激。

private List<PointF> SplitAndSolve(Graphics g, List<PointF> pointList)
    {
        if (pointList.Count < 4)
            return pointList;
        List<List<PointF>> leftAndRight = SplitByX(pointList);
        List<PointF> left = leftAndRight[0];
        List<PointF> right = leftAndRight[1];
        drawPolygon(g, left);// just a way to visually assess the correctness
        drawPolygon(g, right);// same
        left = SplitAndSolve(g, left);
        right = SplitAndSolve(g, right);
        Combine(g, left, right);
        return pointList;
    }
    private List<PointF> Combine(Graphics g, List<PointF> left, List<PointF> right)
    {
        //find tangents
        List<PointF> topTangents = GetTangents(g, left, right, TOP);
        drawPoint(g, topTangents[0]);//visual debug
        drawPoint(g, topTangents[1]);//visual debug
        List<PointF> botTangents = GetTangents(g, left, right, BOT);
        drawPoint(g, botTangents[0]);//visual debug
        drawPoint(g, botTangents[1]);//""
        // get a new polygon

        return left;// just a place holder so I don't get errors for the time being
    }
    private List<PointF> GetTangents(Graphics g, List<PointF> left, List<PointF> right, float topOrBot)
    {
        List<PointF> tangents = new List<PointF>();
        foreach (PointF leftAnchor in left)
        {
            foreach (PointF rightAnchor in right)
            {
                double lax = leftAnchor.X;
                double lay = leftAnchor.Y;
                double rax = rightAnchor.X;
                double ray = rightAnchor.Y;
                double m = (lay - ray) / (lax - rax);
                double b = (-1 * m * lax) + lay;
                bool isTangent = true;
                foreach (PointF lpoi in left)
                {
                    if ((topOrBot == TOP) && (Test(m, b, lpoi) > 0))
                    {
                        isTangent = false;

                    }
                    if ((topOrBot == BOT) && (Test(m, b, lpoi) < 0))
                    {
                        isTangent = false;

                    }
                }

                foreach (PointF rpoi in right)
                {
                    if ((topOrBot == TOP) && (Test(m, b, rpoi) > 0))
                    {
                        isTangent = false;

                    }
                    if ((topOrBot == BOT) && (Test(m, b, rpoi) < 0))
                    {
                        isTangent = false;

                    }
                }
                if (isTangent)
                {
                    tangents.Add(leftAnchor);
                    tangents.Add(rightAnchor);
                    return tangents;
                }

            }

        }
        return null;
    }

    /*  Test, test to see the location of a point in relation to a line
     *  @float m slope of the line
     *  @float b the constast of the y intercept form
     *  @Pointf r is the point to be tested against the line
     * 
     *  returns some k > 0 if point is below the line
     *  returns some k < 0 if point is above the line
     *  returns 0 if point is found along the line
     */
    private double Test(double m, double b, PointF r)
    {
        return (m*(double)r.X) + b - (double)r.Y;
    }

所以我有点确信这是一个编程错误,因为我已经完成了它,虽然我可能弄错了。

我很抱歉,如果这是一个不合适的帖子,我真的只是卡住了,我不是故意对任何人都很麻烦,如果我滥用论坛请告诉我。

我发现穿过两个顶点的线,每个子组一个,所有其他线都在下面(顶部切线)或上面(底部切线)。错误是使用算法,它应该总是迭代循环永远不会达到返回null。但偶尔会这样。我猜这是一个精确错误。

1 个答案:

答案 0 :(得分:3)

这可能是双精度问题吗?也许你应该让你的测试功能“模糊”。

像这样:

  double result = (m*(double)r.X) + b - (double)r.Y;

  if ((result > -0.0001) && (result < 0.0001))
     return 0.0;
  else
     return result;