I have a list of points. I take first one and very next point (second point) and draw a line between first and second point.
I want to add another vertex in between existing points.
This is my method.
I'm calculating all points of lines. by using y = mx + c.
My problem is I'm able to get all vertical points x = a, some diagonal points and Horizontal points but not all. I need to know what is the cause and how can I fix it ?
I'm creating a bitmap overlay provided by ICImaging video grabber library.So I'm limited to pass only integer points to draw on screen.
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);
}
}
}
}
Edit
I have changed my method.
Code is in the answer.