- #1
supremeruler
- 10
- 0
I know it's long but I really need help. I'm terrible at math. Anyway, given only a start and end point, I'm trying to draw a line by creating a number of points at regular intervals between those start and end points.
This is what I have so far (it's from C# code so I hope it's not too hard to understand).
1. Find this distance between the start and end points.
2. Calculate the slope of the line using the start and end points.
This is the slope calculation function:
3. Loop through how many points SHOULD be on the line (this is dictated by the user).
4. Within the loop, create a point on the line and give it an x value based on the length of the line so that all points created will be equi-distant.
(j represents the point number, so it will loop from 1 to however many points need to be created.)
5. Calculate the 'b' in 'y = mx + b' so we can figure what the corresponding y co-ordinate of perfectPoint.x.
This is the 'b' calculation function:
6. That's it! However, it always ends up messed up. The line doesn't start at my start point or even end there. It's way off. the slope seems OK but I can't be sure of that.
Thanks!
This is what I have so far (it's from C# code so I hope it's not too hard to understand).
1. Find this distance between the start and end points.
Code:
float touchLength = Vector2.Distance (touchPoints [touch.fingerId] [0], touchPoints [touch.fingerId] [lastIndex]);
2. Calculate the slope of the line using the start and end points.
Code:
float slope = HelperFunctions.CalculateSlope (touchPoints [touch.fingerId] [0].x, touchPoints [touch.fingerId] [lastIndex].x, touchPoints [touch.fingerId] [0].y, touchPoints [touch.fingerId] [lastIndex].y);
This is the slope calculation function:
Code:
public static float CalculateSlope (float y1, float y2, float x1, float x2)
{
float slope = (y2 - y1) / (x2 - x1);
return slope;
}
3. Loop through how many points SHOULD be on the line (this is dictated by the user).
4. Within the loop, create a point on the line and give it an x value based on the length of the line so that all points created will be equi-distant.
Code:
perfectPoint.x = j * (touchLength / touchPoints [touch.fingerId].Count);
5. Calculate the 'b' in 'y = mx + b' so we can figure what the corresponding y co-ordinate of perfectPoint.x.
Code:
float b = HelperFunctions.CalculatePointOnLine (slope, touchPoints [touch.fingerId] [0].x, touchPoints [touch.fingerId] [0].y);
This is the 'b' calculation function:
Code:
public static float CalculatePointOnLine (float slope, float x, float y)
{
float b = y - (slope * x);
return b;
}
6. That's it! However, it always ends up messed up. The line doesn't start at my start point or even end there. It's way off. the slope seems OK but I can't be sure of that.
Thanks!