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.