【发布时间】:2018-11-23 23:21:39
【问题描述】:
我有一个点列表。我取第一个点和下一个点(第二点)并在第一点和第二点之间画一条线。
我想在现有点之间添加另一个顶点。
这是我的方法。
我正在计算线的所有点。通过使用 y = mx + c。
我的问题是我能够得到所有垂直点 x = a、一些对角点和水平点,但不是全部。我需要知道是什么原因以及如何解决?
我正在创建由 ICImaging 视频采集器库提供的位图叠加层。所以我只能通过整数点在屏幕上绘制。
private void btnAddPoint_Click(object sender, EventArgs e)
{
//IsLineSelected = true;
Debug.Print("Current Points in List ");
foreach (System.Drawing.Point P in PathPoints)
{
Debug.Print(P.ToString());
}
for (int IndexI = 0; IndexI != PathPoints.Count - 1; IndexI++)
{
if (IndexI == PathPoints.Count-1)
{
break;
}
else
{
Debug.Print("P1:" + PathPoints[IndexI].ToString() + "P2:"+ PathPoints[IndexI+1].ToString());
ShapeDirectory.AllLinePoints(PathPoints[IndexI], PathPoints[IndexI + 1]);
}
}
foreach (System.Drawing.Point P in ShapeDirectory.LinePoints)
{
PathPoints3.Add(P);
}
drawLinePoints = true;
}
public static void AllLinePoints(Point p1, Point p2)
{
double YDiff = p2.Y - p1.Y;
double XDiff = p2.X - p1.X;
double SlopM = Math.Round(Math.Abs(YDiff / XDiff));
double YinterceptB = Math.Round(Math.Abs(p1.Y - (SlopM * p1.X)));
Debug.Print("Slop: " + SlopM.ToString() + "Y Intercept: " + YinterceptB.ToString());
if (SlopM == 0)
{
}
if (Double.IsNegativeInfinity(SlopM) || Double.IsPositiveInfinity(SlopM))
{
int LowerBoundX = 0;
int LowerBoundY = 0;
int upperBoundX;
int upperBoundY;
double distanceBetwwenP1andp2 = GetDistanceBetween2points(p1, p2);
if (p1.X == p2.X )
{
LowerBoundX = p1.X;
upperBoundX = p2.X;
}
if (p1.Y <= p2.Y)
{
LowerBoundY = p1.Y;
upperBoundY = p2.Y;
}
else
{
LowerBoundY = p2.Y;
upperBoundY = p1.Y;
}
//Vertical
for (int YIndex = LowerBoundY; YIndex <= upperBoundY; YIndex++)
{
TempLinePoint.X = LowerBoundX;
TempLinePoint.Y = YIndex;
//Debug.Print("Current Line Points X: " + XIndex.ToString() + "Y: " + YIndex.ToString());
LinePoints.Add(TempLinePoint);
}
}
else
{
int LowerBoundX;
int LowerBoundY;
int upperBoundX;
int upperBoundY;
double distanceBetwwenP1andp2 = GetDistanceBetween2points(p1, p2);
if (p1.X <= p2.X && p1.Y <= p2.Y)
{
LowerBoundX = p1.X;
upperBoundX = p2.X;
LowerBoundY = p1.Y;
upperBoundY = p2.Y;
}
else
{
LowerBoundX = p2.X;
upperBoundX = p1.X;
LowerBoundY = p2.Y;
upperBoundY = p1.Y;
}
//if Vertical
for (int YIndex = LowerBoundY; YIndex <= upperBoundY; YIndex++)
{
for (int XIndex = LowerBoundX; XIndex <= upperBoundX; XIndex++)
{
if (YIndex == (SlopM * XIndex) + YinterceptB)
{
TempLinePoint.X = XIndex;
TempLinePoint.Y = YIndex;
//Debug.Print("Current Line Points X: " + XIndex.ToString() + "Y: " + YIndex.ToString());
LinePoints.Add(TempLinePoint);
}
}
}
}
编辑
我改变了我的方法。
代码在答案中。
【问题讨论】:
标签: c# line polygon point point-in-polygon