# certain distance along a line

This topic is 5231 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Currently, I use this to move along lines:
private Vector3 PointOnLine(Vector3 startPT, Vector3 directionPT, float parNum)
{
Vector3 newVector = new Vector3();
newVector.X = (startPT.X + (parNum * directionPT.X));
newVector.Y = (startPT.Y + (parNum * directionPT.Y));
newVector.Z = (startPT.Z + (parNum * directionPT.Z));
return newVector;
}

how do i do the same thing using "parNum" to obtain a "newVector" that is a certain distance from "startPT" along the line created by "startPT" and "directionPT" effectively, "parNum" should be the distance; i need to modify this function so that it returns a Vector3 that is "parNum" units away from "startPT" along the line created by "startPT" and "directionPT"

##### Share on other sites

v = directionPT - startPT; // Get vector between points
v.Normalize(); // normalize to unit vector
v *= parNum; // multiply unit vector by number of units along line
v += startPT; // make it start from teh start

##### Share on other sites
There's some complications when "startPT" isn't (0,0,0)...

##### Share on other sites
private Vector3 PointOnLine(Vector3 startPT, Vector3 directionPT, float parNum){	Vector3 newVector = new Vector3();	float Length = directionPT.Length();	parNum /= Length;	newVector.X = (startPT.X + (parNum * directionPT.X));	newVector.Y = (startPT.Y + (parNum * directionPT.Y));	newVector.Z = (startPT.Z + (parNum * directionPT.Z));	return newVector;}

with that function, if you pass in a distance for parNum, it will do the same as if you pass in 0-1 now. IFooBar misunderstood, he thought directionPT was the end point, which is why what he said doesn't work. It should work if you change it to this:
v = directionPT; // Get vector between points, directionPT is already the vector between the pointsv.Normalize(); // normalize to unit vectorv *= parNum; // multiply unit vector by number of units along linev += startPT; // make it start from teh start

1. 1
2. 2
Rutin
21
3. 3
A4L
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633741
• Total Posts
3013624
×