C# 获取零星结果
所以我实际上已经在这个问题上坚持了几个小时了,这非常令人沮丧。我得到零星的结果。该程序有时可以工作,但有时它找不到我正在寻找的“切线”之一,即使从数学上来说肯定存在一个。
我对 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。但有时确实如此。我猜这是一个精度错误。
So I've literally been stuck on this for hours and it's quite frustrating. I am getting sporadic results. the program is working sometime, but at other times, it can't find one of the "tangents" I'm looking for, even though mathematically there is guaranteed to be one.
I'm really new to c# so, if anyone had some time to help a noob, it would be much appreciated.
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;
}
so I'm somewhat convinced it's a programming error, because I've been over it and over it, though I may be mistaken.
I'm sorry if this is an inappropriate post, I really am just stuck, I don't mean to be bothersome to anyone, if I am misusing the forum please let me know.
I am finding the line that crosses two vertices, one from each subgroup, that all other lines are either below(top tangent) or above(bottom tangent). The error is that with the algorithm, it should always iterative loops should never reach the return null. Yet occasionally it does. I guessing it is a precision error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能只是 double 的精度问题吗?也许你应该让你的测试函数“模糊”。
像这样:
Could this just be precision issues with double? Maybe you should make your Test function "fuzzy".
Like this: