|
-
January 22nd, 2009, 04:36 AM
#1
Plot Points along a line
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
-
January 22nd, 2009, 05:09 AM
#2
Re: Plot Points along a line
I think this is the result
Code:
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;
}
-
January 22nd, 2009, 04:01 PM
#3
Re: Plot Points along a line
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:
Code:
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);
}
Please rate my post if it was helpful for you.  Java, C#, C++, PHP, ASP.NET
SQL Server, MySQL
DirectX
MATH Touraj Ebrahimi
[toraj_e] [at] [yahoo] [dot] [com]
-
January 26th, 2009, 11:36 AM
#4
Re: Plot Points along a line
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.
Last edited by rickjnash; January 26th, 2009 at 11:40 AM.
-
January 26th, 2009, 12:12 PM
#5
Re: Plot Points along a line
in order to find the middle of the line you can do this:
Code:
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.
Please rate my post if it was helpful for you.  Java, C#, C++, PHP, ASP.NET
SQL Server, MySQL
DirectX
MATH Touraj Ebrahimi
[toraj_e] [at] [yahoo] [dot] [com]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|