# How to maintain the alignment

Hello All,

I am facing a problem. My problem is that i am trying to add a point in an line that start from point A to B.

But due to raycastin the new point shift the line alignment. How i can prevent that

Hi!
Looks like you need to find the closest point on the line from the point of intersection

Not sure what you mean, but that is not a goal.

Goal is → i have a like consist of point A and B and now i am adding point C in it so the alignment is getting off.

See first line. Its completely sort of straight. But after adding point it become curvy sort off.

So i want to preserve the aligment here

Means, that the added point is off of the line. Find the closest point on the line from the added point.

Have a look at this post. By varying “m” between [0.0 … 1.0] you get all points on the line between A and B.

Some part is clear.

But here this part is not clear

Since you know the target z-coordinate of the point and also the z-components or origin and direction (3rd equation), that gives you the value of “m”.

Also here origin and direction denotes to what

origin of ray and direction of ray.

To clear you my raycaster is only helping me to get the point of intersection

Also i want to know the method to pick a line with raycasting

let minDistance = Infinity
raycaster.params.Line.threshold = 0.1
raycaster.params.Points.threshold = 0.1
let objects = raycaster.intersectObjects(polygon.getObjectByName(‘line-helpers’).children.concat(flatSite.children),true)
for(let i =0; i< objects.length; i++){
if(objects[i].object.name === ‘polygon-line’){
if(minDistance > pointPosition.distanceTo(objects[i].point)){
minDistance = pointPosition.distanceTo(objects[i].point)
nearestLine = objects[i].object
}
}
}

This is my code to pick the nearest line but this is not the exact approach.

Please correct me on the above request

Thanks

@vielzutun.ch
As I can see, `m` is unknown in the current case. Looks like `Raycaster` works with some tolerance on lines and returns the point of intersection with some shift from the line. But I may be wrong

@Alex Could you provide a codepen, that demonstrates this behaviour?

I have the feeling, that the whole Raycaster bit is muddying the water here. @Alex Alex has a point “A” and a point “B”. And it’s trivial to compute the direction vector from A to B via B - A. It’s true that we currently don’t know the value of “m”.

• If you take m = 0.0, you’ll end up at point A.
• If you take m = 1.0, you’ll end up at point B.
• if you take m = 0.5, you’ll end up exactly half-way on the straight connecting line between A and B.
and so on.

If @Alex is looking for a very specific point on the straight line connection between A and B, then an additional boundary condition is needed, which we don’t have this far.

@prisoner849 , you understand it correctly.

Looks like `Raycaster` works with some tolerance on lines and returns the point of intersection with some shift from the line. But I may be wrong

Yep! `Raycaster.params.Line.threshold` might be what you’re looking for.

So here what i need is
I know for an expert its a very simple math

Here you can see i am clicking near to the line that is my clicked point so that is little away from line and changing the line alignment.

So i need a way to get the perpendicular point that i highlighted

I’ve tried to replicate the behaviour you reported. See this JSFiddle which is more or less a stripped down version of the Raycasting to a Line example. My version has one static line and a static camera only. The red sphere marks the point of intersection, which always “snaps” exactly to the line as soon as the mouse pointer gets within `raycaster.params.Line.threshold` world units of the line.
If you still see the point of intersection, as reported by `intersects[ 0 ].point` to be off-line, please provide a JSFiddle which demonstrates this issue.