我有一个由 PointF 数组(通过鼠标单击创建)定义的 GraphicsPath,它由 Graphics.DrawCurve 绘制。然后变换路径以将起点和终点旋转到 0 度。转换和旋转的曲线(和原始曲线)PointF 数组仍然可用。
我需要找到从路径的直线端点到曲线本身的最长垂直距离。
因为我已经旋转了曲线,所以找到最长的距离应该与边界的高度相同......但我还需要知道 0 轴沿(现在是什么)的距离是多少(如果我可以找到曲线)。我进行了广泛搜索-我尝试了自定义样条函数以尝试沿曲线获得更多固定点,但这最终以相同的结果结束-我不知道点之间的曲线是什么,那就是考虑到点之间的插值,90% 可能是这种情况。
现在它已经旋转了,我尝试了一个 for 循环来计算顶部,并在每一列中,尝试查看 IsVisible 下的每个点是否针对转换后的路径,但它总是返回 false。
输入点(参考):{X=499,Y=64} {X=305,Y=117} {X=149,Y=114}
(从左到右,我知道我需要做一些检查以查看它们将来输入的方式,但现在,只是想让它工作)
GraphicsPath gP = new GraphicsPath();
gP.AddCurve(lPoints.ToArray());
Graphics g = pbImage.CreateGraphics();
g.SmoothingMode = SmoothingMode.AntiAlias;
//draws original curve (hiding for now to validate and visualize rotation:
//g.DrawPath(new Pen(Color.Blue, 1.75f), gP);
//g.DrawLine(new Pen(Color.Red, 1.75f), lPoints[0], lPoints[lPoints.Count - 1]);
// get angle in radians
double theta2 = Math.Atan2(lPoints[0].Y - lPoints[lPoints.Count - 1].Y, lPoints[0].X - lPoints[lPoints.Count - 1].X);
// convert radians to degrees
double angle = -1 * (theta2 * (180.0 / Math.PI));
//set a new path to the old one, to keep both sets of data
GraphicsPath newPath = gP;
//create rotational matrix, and transform
Matrix m = new Matrix();
m.RotateAt(Convert.ToSingle(angle), lPoints[0]);
newPath.Transform(m);
//draw transformed path to picture box
g.DrawPath(new Pen(Color.Green, 1f), newPath);
//get new points from transformed curve (interestingly enough, it adds more points than the oringial input)
PointF[] tP = newPath.PathPoints;
//create some temp variables to make the next section easier
PointF pS = tP[0];
PointF pE = tP[tP.Length - 1];
//draw the straight line for visual validation
g.DrawLine(new Pen(Color.Red, 1f), pS, pE);
//get the bounds of the new path
RectangleF bRect = newPath.GetBounds();
a
// loop through x's
for (float i = pE.X; i < pS.X; i = i + 5)
{
float h = pS.Y - bRect.Y;
bool bFound = false;
// loop through y's - do loop until found
do
{
if (newPath.IsVisible(i, h, g))
{
// never found
tbOutPt.Text = tbOutPt.Text + "found!!!!";
bFound = true;
}
h++;
} while (bFound = false && h < bRect.Height);
}
我唯一能想到的另一件事是基于边界创建一个新的位图,重新绘制曲线,然后逐列获取像素颜色,直到它与绘制曲线的颜色相匹配。
希望有人有比这更好的解决方案。