# Find a two points in differents segments which their distances is K

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

## Recommended Posts

There are two segments A and B, and I would like to find two points Ap and Bp which their distance is K

for exmaple :

A (0,0) (9,9)

B (0,0) (9,18)

and K is 2

so I will solve that by find AX+b line , and I will solve it by basic algebra, but how should the computer do that ?

##### Share on other sites

Ap and Bp are points in A and B respectively, right?

You want that the distance between Ap and Bp be k.

So, if Ap is (Apx, Apy) and Bp is (Bpx, Bpy), length(Apx - Bpx,Apy - Bpy) = k, then (Apx-Bpx)2 + (Apy-Bpy)2 = k2

That's the only restriction aside from Ap and Bp being inside A and B? I guess you can't find one solution for this, you need something more.

Imagine this case, A (0,0) (0,1), B (0,0) (1,0) and k = 1. One possible solution is Ap = (0,0) and Bp = (1,0), but it could be Ap = (0,1) and Bp = (0,0), or Ap = (0. sqrt(2)) and Bp = (sqrt(2),0), etc, etc

Anyway, if you can do it with algebra, write a general solution (you should be able to write Ap = f1(A,B) and Bp = f2(A,B)) and just implement that solution in code. You would have two functions that takes 2 vectors as input and return a vector as result, and those functions will resolve whatever you found that work for f1 and f2 on paper.

Edited by DiegoSLTS

##### Share on other sites

Ap and Bp are points in A and B respectively, right?

You want that the distance between Ap and Bp be k.

So, if Ap is (Apx, Apy) and Bp is (Bpx, Bpy), length(Apx - Bpx,Apy - Bpy) = k, then (Apx-Bpx)2 + (Apy-Bpy)2 = k2

That's the only restriction aside from Ap and Bp being inside A and B? I guess you can't find one solution for this, you need something more.

Imagine this case, A (0,0) (0,1), B (0,0) (1,0) and k = 1. One possible solution is Ap = (0,0) and Bp = (1,0), but it could be Ap = (0,1) and Bp = (0,0), or Ap = (0. sqrt(2)) and Bp = (sqrt(2),0), etc, etc

Anyway, if you can do it with algebra, write a general solution (you should be able to write Ap = f1(A,B) and Bp = f2(A,B)) and just implement that solution in code. You would have two functions that takes 2 vectors as input and return a vector as result, and those functions will resolve whatever you found that work for f1 and f2 on paper.

I dont know what is respectively :

here an exmaple so you can understand me better :

the segments are

(0,0) (9,9)

(0,0) (9,18)

and k is 2

Edited by MaorNr

##### Share on other sites
The two points you have written are not the unique points satisfying your condition. Your two segments are defined by the equations

P(t) = (1 - t)A_0 + tA_1, Q(u) = (1 - u)B_0 + uB_1 (0 <= t, u <= 1).

You want to find all the values of t and u such that the equation (P(t) - Q(u))^2 = k^2. This is a quadratic equation in two variables and the solution is thus not necessarily unique. Is there any other restriction you have in mind? Do you want the two points to have the same x-coordinate?

##### Share on other sites

Are you only looking for the vertical distance, or any distance? For example, your point E can also be moved from (2,2) to (4,4) and still be k=2 units from D. There are infinitely many more solutions as well in that case.

For the vertical distance only, make two equations on the form y(x)=A*x+b as you suggested earlier then subtract them, then find where your new function attains the value of k.

1. y1(x) = A1*x+b1
2. y2(x) = A2*x+b2
3. y(x) = y2(x)-y1(x)
4. solve for x in y(x)=k

It can be solved analytically all the way to step 4, assuming the lines are not parallel in which case some extra care has to be taken.

##### Share on other sites

Are you only looking for the vertical distance, or any distance? For example, your point E can also be moved from (2,2) to (4,4) and still be k=2 units from D. There are infinitely many more solutions as well in that case.

For the vertical distance only, make two equations on the form y(x)=A*x+b as you suggested earlier then subtract them, then find where your new function attains the value of k.

1. y1(x) = A1*x+b1
2. y2(x) = A2*x+b2
3. y(x) = y2(x)-y1(x)
4. solve for x in y(x)=k

It can be solved analytically all the way to step 4, assuming the lines are not parallel in which case some extra care has to be taken.

The two points you have written are not the unique points satisfying your condition. Your two segments are defined by the equations

P(t) = (1 - t)A_0 + tA_1, Q(u) = (1 - u)B_0 + uB_1 (0 <= t, u <= 1).

You want to find all the values of t and u such that the equation (P(t) - Q(u))^2 = k^2. This is a quadratic equation in two variables and the solution is thus not necessarily unique. Is there any other restriction you have in mind? Do you want the two points to have the same x-coordinate?

I'll tell you guys my problem and why I need that .

there is problem when an object move too fast, the collision system can't deal with it .

so I made an idea on paper which take the old and the new position of two movements of circles , and I need to find the Point in/on this segments (from the old and new position) which the distance

is equal to the radiuses of the these two balls , and the next conditions and calculations depends on time , but this is the basic .

Edited by MaorNr

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 13
• 75
• 11
• 10
• 14
• ### Forum Statistics

• Total Topics
632967
• Total Posts
3009580
• ### Who's Online (See full list)

There are no registered users currently online

×