# How to get tangent based on direction of intersection?

Basically, Im extending the Rollercoaster example so the track is procedurally generated. I have most things working but I have hit a small hiccup:

I have two lines A-B and C-D.
A-B is calculated by taking the direction of A to Mid and extending by half the distance of C-D.

It is important that X is the opposite side of the C-D to A.

I want to get the tangent of C-D where X is on the opposite of the C-D line to A. I can get X for a given setup. However, I need a solution that works regardless of A being left or right of the C-D line. I came to this question several times, and I made my best to understand it, but I failed. I cannot imagine visually your geometrical construction, as your description appear to me as self-contradictory on several levels. Maybe my understanding of geometry relations is too limited for your case. I’m sorry. I hope that someone else would be of greater help.

1 Like

Thanks for looking Ill try to simplify the issue… If A is on the left of C-D, I need to find X on the right.
If A is on the right of C-D, I need to find X on the left.

X will always originate from Mid, and the distance from Mid to X is always half the length of C-D

In my previous image I was using A-B to help me track the position of A.

1 Like

In case all points lie on a plane, say XY, the notion of left or right would be connected to counter clock-wise or clock-wise rotation.
If you look at the sign of z component of cross product of vectors CD and CA, it will let you tell if A is to the left or to the right of CD.

Just to see whether I have understood your question → here is a video of a program that:

• sets A, C and D randomly in 3D space
• makes point X so that the distance to the CD’s middle Mid is the same as the distance from Mid to C and to D
• point X lies on the ‘opposite’ side of line CD in respect to A

Does the video show correctly what you want?

Yes, thats perfect

Good. Here are the calculations (all variables are `THREE.Vector3` objects):

``````// random points A, C and D
A.set( random(-30,30), random(-30,30), random(-30,30) );
C.set( random(-20,20), random(-20,20), random(-20,20) );
D.set( random(-20,20), random(-20,20), random(-20,20) );

// calculate Mid and X
Mid.lerpVectors( C, D, 0.5 );
CD.subVectors( C, D );
X.subVectors( C, A ).cross( CD ).cross( CD ).setLength(-CD.length()/2).add( Mid );
``````
2 Likes

Thank you so much. In the end I managed to get something working with some very dirty code and objects. This is a million times cleaner 1 Like