Click to See Complete Forum and Search --> : Plot Points along a line
rickjnash
January 22nd, 2009, 03:36 AM
Hi
I am trying to plot some points along a line for graphical app I am building in C#.
I have the start Point and end Point, I then draw the line. Then also want put some marks along the line. A function like this would be usefull:
Point[] PointsAlongLine(Point start, Point end, int spacing)
{
?
}
Thanks
dannystommen
January 22nd, 2009, 04:09 AM
I think this is the result
private Point[] PointsAlongLine(Point start, Point end, int spacing) {
int xDifference = end.X - start.X;
int yDifference = end.Y - start.Y;
int absoluteXdifference = Math.Abs(start.X - end.X);
int absoluteYdifference = Math.Abs(start.Y - end.Y);
int lineLength = (int)Math.Sqrt((Math.Pow(absoluteXdifference, 2) + Math.Pow(absoluteYdifference, 2))); //pythagoras
int steps = lineLength / spacing;
int xStep = xDifference / steps;
int yStep = yDifference / steps;
Point[] result = new Point[steps];
for (int i = 0; i < steps; i++) {
int x = start.X + (xStep * i);
int y = start.Y + (yStep * i);
result[i] = new Point(x, y);
}
return result;
}
toraj58
January 22nd, 2009, 03:01 PM
as complementary to danny for drawing the line and marking a lable in the middle of the line (as an example) you can use the function proposed by danny like this:
protected override void OnPaint(PaintEventArgs pe)
{
Graphics gc = pe.Graphics;
Pen RedPen = new Pen(Color.Red, 3);
Point p1 = new Point(100, 100);
Point p2 = new Point(300, 300);
Point[] p = PointsAlongLine(p1, p2, 2); // function proposed by danny
int pXmiddle = (p[0].X + p[p.Length - 1].X) / 2; // X axis of the point in the middle of line
int pYmiddle = (p[0].Y + p[p.Length - 1].Y) / 2; // Y axis of the point in the middle of line
Point middalePoint = new Point(pXmiddle, pYmiddle);
Label lbl1 = new Label();
lbl1.Location = middalePoint;
lbl1.Text = "Middle of the line";
gc.DrawLines(RedPen, p);
this.Controls.Add(lbl1);
}
rickjnash
January 26th, 2009, 10:36 AM
Cheers guys I also managed to do this another way after going to www.mathisfunforum.com!!!
static public void EquationOfALine(Point start, Point end, ref double X, ref double Y)
{
Y = (end.Y - start.Y);
X = (end.X - start.X);
}
static public Point[] PointsAlongALine(Point start, Point end, int numberOfPoints)
{
Point[] points = new Point[numberOfPoints];
//Work out the equaton of the line and the add factor
double X = 0, Y = 0;
EquationOfALine(start, end, ref X, ref Y);
Y = Y / numberOfPoints;
X = X / numberOfPoints;
Point lastPoint = start;
for (int loop = 0; loop < points.Length; loop++)
{
points[loop] = new Point(lastPoint.X + (int)X, lastPoint.Y + (int)Y);
lastPoint = points[loop];
}
return points;
}
I think I can use the above code to help with another problem. I need to find a point X Pixels from the start or end of the line.
toraj58
January 26th, 2009, 11:12 AM
in order to find the middle of the line you can do this:
int pXmiddle = (p[0].X + p[p.Length - 1].X) / 2; // X axis of the point in the middle of line
int pYmiddle = (p[0].Y + p[p.Length - 1].Y) / 2; // Y axis of the point in the middle of line
to find other the logic is similar that (e.g. quarter, one-eighth...)
and base in pixel it as alread in this array Point[] p that you can access each element base on index of the array.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.