Calculate Distance Between Two Moving Elements

how to calculate shortest distance between two moving objects

Assuming you are asking for 2-d space, at t=0, let the starting points be (d1,0) and (0,d2) on the coordinate axes. We can assume this because one object is always moving horizontally (E-W direction, along X-axis) and other vertically (S-N direction, along Y-axis). Now, after some time t, their positions will be,(d1-t*v1) and (0,d2-t*v2). (Speed-Distance-Time relation).



Now, distance between them at this time t will be,

D = d^2 = (d1-t*v1)^2 + (d2-t*v2)^2

So, differentiating both sides wrt t,

dD/dt = 2(-v1)(d1-t*v1) + 2(-v2)(d2-t*v2)   ....(1)

For D to be minimum, dD/dt = 0 and second differential must be positive. Now, second differential :

d2D/dt2 = 2*v1^2 + 2*v2^2 which is positive for all real v1/v2. So, if `dD/dt = 0`, distance will be minimum.



So, equating (1) = 0, we get

t = (d1v1 + d2v2)/(v1^2 + v2^2)

So, get sqrt(D) at t = --above value-- and that shall be your answer.

PS: ask these type of questions on mathematics stackexchange.

calculate distance between two moving elements

The result is only set once. You'll need to update it as objects move.

One solution is to update the value on a timer, using JavaScript's setInterval().

In the example below, I'm updating the result every 100 milliseconds.

var $x=$('#x');var $y=$('#y');var $result=$('#result');
function updateDistance() {
var lFirst = $x.offset().left; var lSecond = $y.offset().left; var ldist = parseInt(lFirst - lSecond);
var tFirst = $x.offset().top; var tSecond = $y.offset().top; var tdist = parseInt(tFirst - tSecond);
$result.text(parseInt(tdist + ldist));
}
setInterval(updateDistance, 100);
html,body {  margin: 0;  padding: 0;}
* { transition: all 1s;}
#x,#y { width: 50px; height: 50px; margin-left: 0; margin-top: 0; background: black;}
#container { height: 100vh; width: 100vw; background: lightgrey;}
#container:hover #x { margin-left: 50vw;}
#container:hover #y { margin-top: 50vh;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><div id="container">  <div id="result"></div>  <div id="x"></div>  <div id="y"></div></div>

Minimum distance between two elements in a circular array

Edit: I think I answered the question in the title, but what you want is a direction not a distance. Then:

function getDirection(from, to, array) {
if (from === to) {
return 0;
}
var internal = (Math.max(from, to) - Math.min(from, to) < array.length/2) ? true : false;
if (internal && from < to
||
!internal && from > to
) {
return 1;
} else {
return -1;
}
}

How to find the distance between two elements in a 2D array

You don't need to make it too complicate by using Scipy. This problem can easily done by help of mathematics.

Equation of coordinate inside circle is x^2 + y^2 <= Radius^2, so just check coordinate that inside the circle.

list = [[-,O,-,-,O,-,],
[O,-,-,-,-,O],
[O,O,-,-,X,-],
[-,-,O,-,-,-]]
X_coor = #Coordinate of X, in this case y = 2, x = 4
d = #Maximum distance from X in this case d = 3
total = 0
O_coor = [] #Store coordinate of all O near X
for y in range(max(0, X_coor.y - d), min(list.length - 1, X_coor.y + d)):
for x in range(max(0, X_coor.x - sqrt(d**2 - (y - X_coor.y)**2)), min(list.length - 1, X_coor.x + sqrt(d**2 - (y - X_coor.y)**2))):
if list[y][x] == "O":
total++
O_coor.append([x, y])
print(total)
print(O_coor)

It a long code, but you can ask me parts that you don't understand.

Note: This solution check only coordinate in circle area not entire list, so even if you have large list this still very fast.

Distance between two elements in an two dimensional array

The distance from [a1][b1] to [a2][b2] is abs(a1-a2)+abs(b1-b2).

How do I calculate the width between two elements?

If you mean the horizontal distance between two elements, you need the difference between the top right coord of the left element and the top left coord of the right element. The top right coord of an element is just the top left coord plus its width, as given in Pekka's answer.

To get the top left position an element, you can use the javascript method offsetLeft(). This returns the offset in the x dimension between an element and its parent. You iterate up the DOM tree adding successive offsetLeft's until you get to the document root. The resulting sum is your x position. The excellent Quirksmode shows you how to do this.

Edit: for completeness, I include example javascript to find an element's position:

function getNodePosition(node) {     
var top = left = 0;
while (node) {
if (node.tagName) {
top = top + node.offsetTop;
left = left + node.offsetLeft;
node = node.offsetParent;
} else {
node = node.parentNode;
}
}
return [top, left];
}

Checking Distance Between Two Circles

Hints:

There are two subproblems: finding the collision point and reflecting the trajectories.

To find the collision point, you can reason as follows:

  • inflate one ball by the radius of the other and let the other reduce to a point;

  • consider the relative speeds of the ball and the point (i.e. vector difference), and move the ball to the origin.

Now you have a fixed circle and a point moving along a straight line. You can find the first intersection point between the circle and the line by solving a quadratic equation. The most convenient is to use the parametric equation of the point motion vs. the implicit equation of the circle. For the same price, you get the meeting time.

To reflect the trajectories, consider that the normal to the circle at the intersection point is the bisector of the incident and reflected directions. In case of a perfect elastic shock, the speed after reflection is the same as before.

Now by undoing the initial transformations, you can find the meeting point and the new directions of the two balls.

Just a bit of vector calculus :-)

Trying to calculate the distance between the position of two objects

Completely wrong.

You need to return a value, not a Soldier.

This is more general:

public static double distanceBetween(double x1, double y1, double x2, double y2) {
double dx = Math.abs(x2-x1);
double dy = Math.abs(y2-y1);
if (dx > dy) {
double r = dy/dx;
return dx*Math.sqrt(1.0 + r*r);
} else {
double r = dx/dy;
return dy*Math.sqrt(1.0 + r*r);
}
}

The ^ operator is not exponentiation; it's XOR.

You can override this method this way:

public static double distanceBetween(Soldier s1, Solder s2) {
return distanceBetween(s1.xPos, s1.yPos, s2.xPos, s2.yPos);
}

Since you are having trouble, I'll spell it out for you:

/**
* Distance calc
* User: mduffy
* Date: 11/1/2015
* Time: 9:58 AM
* @link http://stackoverflow.com/questions/33462961/trying-to-calculate-the-distance-between-the-position-of-two-objects/33463222?noredirect=1#comment54712511_33463222
*/
public class Soldier {

public final double xPos;
public final double yPos;

public static void main(String[] args) {
Soldier s = new Soldier(0, 0);
Cavalier c = new Cavalier(3, 4);
System.out.println(String.format("distance: %f10.3", s.distanceBetween(c)));
}

public Soldier(double xPos, double yPos) {
this.xPos = xPos;
this.yPos = yPos;
}

public double distanceBetween(Soldier s) {
return distanceBetween(this.xPos, this.yPos, s.xPos, s.yPos);
}

public static double distanceBetween(double x1, double y1, double x2, double y2) {
double dx = Math.abs(x2-x1);
double dy = Math.abs(y2-y1);
if (dx > dy) {
double r = dy/dx;
return dx*Math.sqrt(1.0 + r*r);
} else {
double r = dx/dy;
return dy*Math.sqrt(1.0 + r*r);
}
}

public static double distanceBetween(Soldier s1, Soldier s2) {
return distanceBetween(s1.xPos, s1.yPos, s2.xPos, s2.yPos);
}
}

class Cavalier extends Soldier {
public Cavalier(double x, double y) {
super(x, y);
}
}

If you want different distance calculation methods (e.g. Euclidian, Manhattan, Pearson, spherical, etc.) you can have an interface that lets you change it by adding a new implementation:

public interface DistanceCalculator {
double distance(double x1, double y1, double x2, double y2);
}

Now you can easily switch by adding new code rather than modifying existing code.



Related Topics



Leave a reply



Submit