Car chase trajectory and time
$begingroup$
I Cannot figure out this problem.(it is in 2d)
There are 2 cars. 1st car is chasing second car. Car1 has starting
position (2,4) and speed of 5(coordinates per second). Car2 has
position of (16,20) moving in at speed of 2(coordinates per second) at
an angle of 60(degrees)(angle is relative to x axis).
What angle does car1 need to choose and how long is it will take to
chase car2?
Should I use vectors? Because for me it looks like impossible to solve it
I know hot to solve this question for 1 dimension but I cannot figure it for 2 dimensions.
Thank you
linear-algebra algebra-precalculus geometry
$endgroup$
|
show 1 more comment
$begingroup$
I Cannot figure out this problem.(it is in 2d)
There are 2 cars. 1st car is chasing second car. Car1 has starting
position (2,4) and speed of 5(coordinates per second). Car2 has
position of (16,20) moving in at speed of 2(coordinates per second) at
an angle of 60(degrees)(angle is relative to x axis).
What angle does car1 need to choose and how long is it will take to
chase car2?
Should I use vectors? Because for me it looks like impossible to solve it
I know hot to solve this question for 1 dimension but I cannot figure it for 2 dimensions.
Thank you
linear-algebra algebra-precalculus geometry
$endgroup$
$begingroup$
Did you try to use vectors? Also, "angle of 60" relative to what? The line connecting Car1 and Car2? The x-axis?
$endgroup$
– arne.b
Feb 18 '14 at 14:40
$begingroup$
Ignore my comment. I can't read.
$endgroup$
– user121049
Feb 18 '14 at 14:40
$begingroup$
the angle is relative to x axis
$endgroup$
– Andrius
Feb 18 '14 at 14:46
$begingroup$
The 2D case works like 2 1D cases, except that you do not know Car1's speed in x-direction or Car1's speed in y-direction, only a combination of both, which gives you a system of 2 equations to transform and insert into a third one, ultimately solving a quadratic equation, no matter whether you derive them from a vector-based approach, coordinates, or else. Again, what did you try?
$endgroup$
– arne.b
Feb 18 '14 at 16:40
$begingroup$
I have not tried a lot. I actually had no idea how to do it. But the simultaneous equation idea sounds good it might just work, I will try it.
$endgroup$
– Andrius
Feb 18 '14 at 19:02
|
show 1 more comment
$begingroup$
I Cannot figure out this problem.(it is in 2d)
There are 2 cars. 1st car is chasing second car. Car1 has starting
position (2,4) and speed of 5(coordinates per second). Car2 has
position of (16,20) moving in at speed of 2(coordinates per second) at
an angle of 60(degrees)(angle is relative to x axis).
What angle does car1 need to choose and how long is it will take to
chase car2?
Should I use vectors? Because for me it looks like impossible to solve it
I know hot to solve this question for 1 dimension but I cannot figure it for 2 dimensions.
Thank you
linear-algebra algebra-precalculus geometry
$endgroup$
I Cannot figure out this problem.(it is in 2d)
There are 2 cars. 1st car is chasing second car. Car1 has starting
position (2,4) and speed of 5(coordinates per second). Car2 has
position of (16,20) moving in at speed of 2(coordinates per second) at
an angle of 60(degrees)(angle is relative to x axis).
What angle does car1 need to choose and how long is it will take to
chase car2?
Should I use vectors? Because for me it looks like impossible to solve it
I know hot to solve this question for 1 dimension but I cannot figure it for 2 dimensions.
Thank you
linear-algebra algebra-precalculus geometry
linear-algebra algebra-precalculus geometry
edited Feb 18 '14 at 16:43
arne.b
345210
345210
asked Feb 18 '14 at 14:07
AndriusAndrius
1032
1032
$begingroup$
Did you try to use vectors? Also, "angle of 60" relative to what? The line connecting Car1 and Car2? The x-axis?
$endgroup$
– arne.b
Feb 18 '14 at 14:40
$begingroup$
Ignore my comment. I can't read.
$endgroup$
– user121049
Feb 18 '14 at 14:40
$begingroup$
the angle is relative to x axis
$endgroup$
– Andrius
Feb 18 '14 at 14:46
$begingroup$
The 2D case works like 2 1D cases, except that you do not know Car1's speed in x-direction or Car1's speed in y-direction, only a combination of both, which gives you a system of 2 equations to transform and insert into a third one, ultimately solving a quadratic equation, no matter whether you derive them from a vector-based approach, coordinates, or else. Again, what did you try?
$endgroup$
– arne.b
Feb 18 '14 at 16:40
$begingroup$
I have not tried a lot. I actually had no idea how to do it. But the simultaneous equation idea sounds good it might just work, I will try it.
$endgroup$
– Andrius
Feb 18 '14 at 19:02
|
show 1 more comment
$begingroup$
Did you try to use vectors? Also, "angle of 60" relative to what? The line connecting Car1 and Car2? The x-axis?
$endgroup$
– arne.b
Feb 18 '14 at 14:40
$begingroup$
Ignore my comment. I can't read.
$endgroup$
– user121049
Feb 18 '14 at 14:40
$begingroup$
the angle is relative to x axis
$endgroup$
– Andrius
Feb 18 '14 at 14:46
$begingroup$
The 2D case works like 2 1D cases, except that you do not know Car1's speed in x-direction or Car1's speed in y-direction, only a combination of both, which gives you a system of 2 equations to transform and insert into a third one, ultimately solving a quadratic equation, no matter whether you derive them from a vector-based approach, coordinates, or else. Again, what did you try?
$endgroup$
– arne.b
Feb 18 '14 at 16:40
$begingroup$
I have not tried a lot. I actually had no idea how to do it. But the simultaneous equation idea sounds good it might just work, I will try it.
$endgroup$
– Andrius
Feb 18 '14 at 19:02
$begingroup$
Did you try to use vectors? Also, "angle of 60" relative to what? The line connecting Car1 and Car2? The x-axis?
$endgroup$
– arne.b
Feb 18 '14 at 14:40
$begingroup$
Did you try to use vectors? Also, "angle of 60" relative to what? The line connecting Car1 and Car2? The x-axis?
$endgroup$
– arne.b
Feb 18 '14 at 14:40
$begingroup$
Ignore my comment. I can't read.
$endgroup$
– user121049
Feb 18 '14 at 14:40
$begingroup$
Ignore my comment. I can't read.
$endgroup$
– user121049
Feb 18 '14 at 14:40
$begingroup$
the angle is relative to x axis
$endgroup$
– Andrius
Feb 18 '14 at 14:46
$begingroup$
the angle is relative to x axis
$endgroup$
– Andrius
Feb 18 '14 at 14:46
$begingroup$
The 2D case works like 2 1D cases, except that you do not know Car1's speed in x-direction or Car1's speed in y-direction, only a combination of both, which gives you a system of 2 equations to transform and insert into a third one, ultimately solving a quadratic equation, no matter whether you derive them from a vector-based approach, coordinates, or else. Again, what did you try?
$endgroup$
– arne.b
Feb 18 '14 at 16:40
$begingroup$
The 2D case works like 2 1D cases, except that you do not know Car1's speed in x-direction or Car1's speed in y-direction, only a combination of both, which gives you a system of 2 equations to transform and insert into a third one, ultimately solving a quadratic equation, no matter whether you derive them from a vector-based approach, coordinates, or else. Again, what did you try?
$endgroup$
– arne.b
Feb 18 '14 at 16:40
$begingroup$
I have not tried a lot. I actually had no idea how to do it. But the simultaneous equation idea sounds good it might just work, I will try it.
$endgroup$
– Andrius
Feb 18 '14 at 19:02
$begingroup$
I have not tried a lot. I actually had no idea how to do it. But the simultaneous equation idea sounds good it might just work, I will try it.
$endgroup$
– Andrius
Feb 18 '14 at 19:02
|
show 1 more comment
2 Answers
2
active
oldest
votes
$begingroup$
Hint: Write the position of car 2 as a function of $t$. You can use trig to get the velocity in each $x$ and $y$ directions. Now assume car 1 travels at angle $theta$. You can use trig to get its $x$ and $y$ velocity, then get its position as a function of $t$. These two positions must be equal. You get one equation from each coordinate, and have two unknowns: $t, theta$, so can solve for both.
$endgroup$
$begingroup$
i tried this but the end equation ends impossible to solve. with sin and cos
$endgroup$
– Andrius
Feb 18 '14 at 19:42
$begingroup$
You have $14+tsqrt 3=5tcos theta, 16+t=5tsin theta$ If you square and add, the $theta$'s disappear and you have a quadratic in $t$
$endgroup$
– Ross Millikan
Feb 18 '14 at 20:17
$begingroup$
Did not think of that. Thanks a bunch ! :)
$endgroup$
– Andrius
Feb 18 '14 at 20:32
add a comment |
$begingroup$
Please, do not consider this an answer to the mathematical question, but an extended comment for programmers on how to implement the solution as a computer program.
You can consider this is a shameless rip-off from the answer by Ross Millikan, rewritten for programmers implementing solutions to this type of problem, with a few practical details and suggestions sprinkled in.
In a computer program, to represent a 2D point moving at fixed speed in a fixed direction, it is best to use
$$begin{cases}
x(t) = x_0 + t x_v \
y(t) = y_0 + t y_v \
end{cases} tag{1}label{NA1}$$
because the vector algebra solution without trigonometric functions is easier to implement, and on typical current hardware, faster as well (because trigonometric functions are "slow" compared to addition, multiplication, and division, on most hardware architectures).
If the point is at $(x_{t_0}, y_{t_0})$ at time $t = t_0$, with speed $v$ and direction $varphi$ (from origin, $varphi = 0text{°} = 360text{°}$ is towards $+x$, $varphi = 90text{°} = -270text{°}$ is towards $+y$, $varphi = 180text{°} = -180text{°}$ is towards $-x$, and $varphi = 270text{°} = -90text{°}$ is towards $-y$), then
$$begin{cases}
x_0 = x_{t_0} - t_0 v cosvarphi \
y_0 = y_{t_0} - t_0 v sinvarphi \
x_v = v cosvarphi \
y_v = v sinvarphi \
end{cases} tag{2}label{NA2}$$
In other words, using $eqref{NA1}$ does not mean we cannot define the point trajectories using (angular) direction and speed; it is just that internally, you'll want to use the vector form.
Let's say that we know $eqref{NA1}$ for some target, and wish to intercept it with a point that starts at $(X_T , Y_0T$ at time $t = T$, with constant speed $V$. Mathematically, for our projectile, we have
$$begin{cases}
X(t) = X_T + (t - T) X_v \
Y(t) = Y_T + (t - T) Y_v \
X_v^2 + Y_v^2 = V^2
end{cases}$$
and we need to solve for $t$, $X_v$, and $Y_v$ from
$$bbox{begin{cases}
X(t) = x(t) \
Y(t) = y(t) \
X_v^2 + Y_v^2 = v^2
end{cases}} quad iff quad bbox{begin{cases}
X_0 - x_0 + t (X_v - x_v) - t_0 X_v = 0 \
Y_0 - y_0 + t (Y_v - y_v) - t_0 Y_v = 0 \
X_v^2 + Y_v^2 = v^2
end{cases}}$$
In practice, it is best to solve for $t$ first. For the interception to be possible, we obviously only care about solutions where $t ge T$, since we cannot fire backwards in time; and a solution $t = T$ means the target intercepts the projectile starting point $(X_T , Y_T)$ at time $t = T$.
There are two cases we need to handle separately:
Target and projectile speeds are equal, $x_v^2 + y_v^2 = V^2$.
First, calculate
$$bbox{d = T (x_v^2 + y_v^2) - x_v (X_T - x_0) - y_v (Y_T - y_0) }$$
If $d = 0$, interception is not possible.
Otherwise, calculate
$$bbox{t = frac{T^2 (x_v^2 + y_v^2) - (X_T - x_0)^2 - (Y_T - y_0)^2}{2 d}}$$
If $t le T$, interception is not possible.
Target and projectile velocities differ, $x_v^2 + y_v^2 ne V^2$.
First, calculate
$$bbox{ r = V^2 bigr( (T x_v + x_0 - X_T)^2 + (T y_v + y_0 - Y_T)^2 bigr) - bigr( x_v (y_0 - Y_T) - y_v (x_0 - X_T) bigr)^2 }$$
If $r le 0$, interception is not possible.
Then, calculate
$$bbox{ begin{aligned}
s &= T V^2 + x_v ( x_0 - X_T ) + y_v ( y_0 - Y_T) \
d &= V^2 - x_v^2 - y_v^2 \
end{aligned} }$$
and finally
$$bbox{ begin{aligned}
t_1 &= frac{s + sqrt{r}}{d} \
t_2 &= frac{s - sqrt{r}}{d} \
end{aligned} }$$
If both $t_1 le T$ and $t_2 le T$, intersection is not possible.
If both $t_1 gt T$ and $t_2 gt T$, set $t = min( t_1 , t_2 )$.
Otherwise, set $t = t_1$ if $t_1 gt T$; or $t = t_2$ if $T_2 gt T$.
At this point, we have the time of the interception $t gt T$ if interception is indeed possible. Then, the velocity vector $(X_V , Y_V)$ for that interception is
$$bbox{ begin{cases}
displaystyle X_V = frac{x_0 - X_T + t x_v}{t - T} \
displaystyle Y_V = frac{y_0 - Y_T + t y_v}{t - T} \
end{cases} }$$
If the interception time $t$ was computed as above, you'll find that $X_V^2 + Y_V^2 = V^2$, to within rounding error.
Here is an example implementation in Python 3:
#!/usr/bin/python3
#
# SPDX-License-Identifier: CC0-1.0
#
# Public domain. Written by Nominal Animal; no warranties, no guarantees.
#
import math
import sys
class MovingPoint(tuple):
"""2D point moving in a constant direction at a constant velocity"""
def __new__(cls, xt, yt, t=0, xv=None, yv=None, speed=None, direction=None):
if (speed is not None) and (direction is not None):
angle = float(direction) * 3.14159265358979323846 / 180.0
xv = float(speed) * math.cos(angle)
yv = float(speed) * math.sin(angle)
else:
if xv is None:
xv = 0.0
if yv is None:
yv = 0.0
x0 = float(xt) - float(t)*xv
y0 = float(yt) - float(t)*yv
return super().__new__(cls, (x0, y0, xv, yv))
@property
def speed(self):
"""Speed"""
return math.sqrt(self[2]*self[2] + self[3]*self[3])
@property
def direction(self):
"""Direction in degrees"""
return math.atan2(self[3], self[2]) * 180.0 / 3.14159265358979323846
@property
def x0(self):
"""X coordinate at time t=0"""
return self[0]
@property
def y0(self):
"""Y coordinate at time t=0"""
return self[1]
@property
def xv(self):
"""Velocity vector x component"""
return self[2]
@property
def yv(self):
"""Velocity vector y component"""
return self[3]
@property
def p0(self):
"""Location at time t=0"""
return (self[0], self[1])
@property
def v(self):
"""Velocity vector"""
return (self[2], self[3])
def x(self, t):
"""X coordinate as a function of time"""
return self[0] + float(t) * self[2]
def y(self, t):
"""Y coordinate as a function of time"""
return self[1] + float(t) * self[3]
def p(self, t):
"""Location as a function of time"""
return (self[0] + float(t) * self[2], self[1] + float(t) * self[3])
def intercept(mpoint, xt, yt, t, v):
"""Calculate interception to MovingPoint mpoint.
Returns None if no intersection, tuple (t, xv, yv) otherwise."""
x0, y0, xv, yv = mpoint.x0, mpoint.y0, mpoint.xv, mpoint.yv
xt, yt, t, v = float(xt), float(yt), float(t), float(v)
mvv = xv*xv + yv*yv
vv = v*v
if mvv == vv:
d = t*mvv - xv*(xt - x0) - yv*(yt - y0)
if d == 0.0:
return None
it = (t*t * mvv - (xt - x0)**2 - (yt - y0)**2) / (2*d)
if it <= t:
return None
else:
r = vv*( (t*xv + x0 - xt)**2 + (t*yv + y0 - yt)**2 ) - ( xv*(y0 - yt) - yv*(x0 - xt) )**2
if r <= 0:
return None
r = math.sqrt(r)
s = t*vv + xv*(x0 - xt) + yv*(y0 - yt)
d = vv - mvv
t1 = (s + r) / d
t2 = (s - r) / d
if t1 > t and t2 > t:
it = min(t1, t2)
elif t1 > t:
it = t1
elif t2 > t:
it = t2
else:
return None
vx = (x0 - xt + it*xv) / (it - t)
vy = (y0 - yt + it*yv) / (it - t)
return (it, vx, vy)
if __name__ == '__main__':
if len(sys.argv) != 9:
sys.stderr.write("n")
sys.stderr.write("Usage: %s [ -h | --help | help ]n" % sys.argv[0])
sys.stderr.write(" %s x0 y0 xv yv XT YT T Vn" % sys.argv[0])
sys.stderr.write("n")
sys.exit(1)
x0 = float(sys.argv[1])
y0 = float(sys.argv[2])
xv = float(sys.argv[3])
yv = float(sys.argv[4])
sys.stderr.write("Target: x(0)=%.6f, y(0)=%.6f, xv=%.6f, yv=%.6fn" % (x0, y0, xv, yv))
xt = float(sys.argv[5])
yt = float(sys.argv[6])
t = float(sys.argv[7])
v = float(sys.argv[8])
sys.stderr.write("Interceptor: x(%.6f)=%.6f, y(%.6f)=%.6f, speed=%.6fn" % (t, xt, t, yt, v))
target = MovingPoint(x0, y0, xv=xv, yv=yv)
icept = intercept(target, xt, yt, t, v)
if icept is None:
sys.stderr.write("Interception is not possible.n")
else:
sys.stderr.write("Interception occurs at time %.6f, using xv=%.6f, yv=%.6fn" % icept)
bullet = MovingPoint(xt, yt, t=t, xv=icept[1], yv=icept[2])
sys.stdout.write("#time target_x target_y interceptor_x interceptor_yn")
N = 20
t0 = min(t, 0.0)
prevtau = t0 - 1
for i in range(0, N+1):
tau = ((N-i)*t0 + i*icept[0]) / N
if prevtau < t and tau > t:
sys.stdout.write("%.6f %.6f %.6f %.6f %.6fn" % (t, target.x(t), target.y(t), bullet.x(t), bullet.y(t)))
elif prevtau < 0 and tau > 0:
sys.stdout.write("%.6f %.6f %.6f %.6f %.6fn" % (0, target.x(0), target.y(0), bullet.x(0), bullet.y(0)))
prevtau = tau
if tau < t:
sys.stdout.write("%.6f %.6f %.6fn" % (tau, target.x(tau), target.y(tau)))
else:
sys.stdout.write("%.6f %.6f %.6f %.6f %.6fn" % (tau, target.x(tau), target.y(tau), bullet.x(tau), bullet.y(tau)))
The MovingPoint class implements a 2D point that moves in a constant direction at constant speed. The intercept() function computes an interception trajectory.
If you run the above program using e.g. python3 intercept.py 1.00 3.25 0.5 0.3 8.00 -4.00 5.5 0.85, we test if we can hit a target $x(t) = 1.00 + 0.5 t$, $y(t) = 3.25 + 0.3 t$, with a projectile that starts at $x(5.5) = 8.00$, $y(5.5) = -4.00$, with velocity $0.85$. The output is
Target: x(0)=1.000000, y(0)=3.250000, xv=0.500000, yv=0.300000
Interceptor: x(5.500000)=8.000000, y(5.500000)=-4.000000, speed=0.850000
Interception occurs at time 22.935375, using xv=0.256243, yv=0.810456
#time target_x target_y interceptor_x interceptor_y
0.000000 1.000000 3.250000
1.146769 1.573384 3.594031
2.293538 2.146769 3.938061
3.440306 2.720153 4.282092
4.587075 3.293538 4.626123
5.500000 3.750000 4.900000 8.000000 -4.000000
5.733844 3.866922 4.970153 8.059921 -3.810480
6.880613 4.440306 5.314184 8.353772 -2.881074
8.027381 5.013691 5.658214 8.647623 -1.951667
9.174150 5.587075 6.002245 8.941474 -1.022261
10.320919 6.160459 6.346276 9.235325 -0.092855
11.467688 6.733844 6.690306 9.529176 0.836551
12.614456 7.307228 7.034337 9.823028 1.765957
13.761225 7.880613 7.378368 10.116879 2.695363
14.907994 8.453997 7.722398 10.410730 3.624769
16.054763 9.027381 8.066429 10.704581 4.554176
17.201531 9.600766 8.410459 10.998432 5.483582
18.348300 10.174150 8.754490 11.292283 6.412988
19.495069 10.747534 9.098521 11.586134 7.342394
20.641838 11.320919 9.442551 11.879985 8.271800
21.788607 11.894303 9.786582 12.173837 9.201206
22.935375 12.467688 10.130613 12.467688 10.130613
which tells us that yes, it is possible, using $x_v = 0.256243$ and $y_v = 0.810456$ ($sqrt{x_v^2 + y_v^2} = 0.85$ as specified), in which case the intercept occurs at $(12.467688, 10.130613)$ at time $t = 22.935375$.
If you redirect the output to a file, you can plot the trajectories using plot "file" u 2:3 t "target" w linespoints, "file" u 4:5 t "interceptor" w linespoints.
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "69"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
noCode: true, onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f680863%2fcar-chase-trajectory-and-time%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Hint: Write the position of car 2 as a function of $t$. You can use trig to get the velocity in each $x$ and $y$ directions. Now assume car 1 travels at angle $theta$. You can use trig to get its $x$ and $y$ velocity, then get its position as a function of $t$. These two positions must be equal. You get one equation from each coordinate, and have two unknowns: $t, theta$, so can solve for both.
$endgroup$
$begingroup$
i tried this but the end equation ends impossible to solve. with sin and cos
$endgroup$
– Andrius
Feb 18 '14 at 19:42
$begingroup$
You have $14+tsqrt 3=5tcos theta, 16+t=5tsin theta$ If you square and add, the $theta$'s disappear and you have a quadratic in $t$
$endgroup$
– Ross Millikan
Feb 18 '14 at 20:17
$begingroup$
Did not think of that. Thanks a bunch ! :)
$endgroup$
– Andrius
Feb 18 '14 at 20:32
add a comment |
$begingroup$
Hint: Write the position of car 2 as a function of $t$. You can use trig to get the velocity in each $x$ and $y$ directions. Now assume car 1 travels at angle $theta$. You can use trig to get its $x$ and $y$ velocity, then get its position as a function of $t$. These two positions must be equal. You get one equation from each coordinate, and have two unknowns: $t, theta$, so can solve for both.
$endgroup$
$begingroup$
i tried this but the end equation ends impossible to solve. with sin and cos
$endgroup$
– Andrius
Feb 18 '14 at 19:42
$begingroup$
You have $14+tsqrt 3=5tcos theta, 16+t=5tsin theta$ If you square and add, the $theta$'s disappear and you have a quadratic in $t$
$endgroup$
– Ross Millikan
Feb 18 '14 at 20:17
$begingroup$
Did not think of that. Thanks a bunch ! :)
$endgroup$
– Andrius
Feb 18 '14 at 20:32
add a comment |
$begingroup$
Hint: Write the position of car 2 as a function of $t$. You can use trig to get the velocity in each $x$ and $y$ directions. Now assume car 1 travels at angle $theta$. You can use trig to get its $x$ and $y$ velocity, then get its position as a function of $t$. These two positions must be equal. You get one equation from each coordinate, and have two unknowns: $t, theta$, so can solve for both.
$endgroup$
Hint: Write the position of car 2 as a function of $t$. You can use trig to get the velocity in each $x$ and $y$ directions. Now assume car 1 travels at angle $theta$. You can use trig to get its $x$ and $y$ velocity, then get its position as a function of $t$. These two positions must be equal. You get one equation from each coordinate, and have two unknowns: $t, theta$, so can solve for both.
answered Feb 18 '14 at 17:01
Ross MillikanRoss Millikan
299k24200374
299k24200374
$begingroup$
i tried this but the end equation ends impossible to solve. with sin and cos
$endgroup$
– Andrius
Feb 18 '14 at 19:42
$begingroup$
You have $14+tsqrt 3=5tcos theta, 16+t=5tsin theta$ If you square and add, the $theta$'s disappear and you have a quadratic in $t$
$endgroup$
– Ross Millikan
Feb 18 '14 at 20:17
$begingroup$
Did not think of that. Thanks a bunch ! :)
$endgroup$
– Andrius
Feb 18 '14 at 20:32
add a comment |
$begingroup$
i tried this but the end equation ends impossible to solve. with sin and cos
$endgroup$
– Andrius
Feb 18 '14 at 19:42
$begingroup$
You have $14+tsqrt 3=5tcos theta, 16+t=5tsin theta$ If you square and add, the $theta$'s disappear and you have a quadratic in $t$
$endgroup$
– Ross Millikan
Feb 18 '14 at 20:17
$begingroup$
Did not think of that. Thanks a bunch ! :)
$endgroup$
– Andrius
Feb 18 '14 at 20:32
$begingroup$
i tried this but the end equation ends impossible to solve. with sin and cos
$endgroup$
– Andrius
Feb 18 '14 at 19:42
$begingroup$
i tried this but the end equation ends impossible to solve. with sin and cos
$endgroup$
– Andrius
Feb 18 '14 at 19:42
$begingroup$
You have $14+tsqrt 3=5tcos theta, 16+t=5tsin theta$ If you square and add, the $theta$'s disappear and you have a quadratic in $t$
$endgroup$
– Ross Millikan
Feb 18 '14 at 20:17
$begingroup$
You have $14+tsqrt 3=5tcos theta, 16+t=5tsin theta$ If you square and add, the $theta$'s disappear and you have a quadratic in $t$
$endgroup$
– Ross Millikan
Feb 18 '14 at 20:17
$begingroup$
Did not think of that. Thanks a bunch ! :)
$endgroup$
– Andrius
Feb 18 '14 at 20:32
$begingroup$
Did not think of that. Thanks a bunch ! :)
$endgroup$
– Andrius
Feb 18 '14 at 20:32
add a comment |
$begingroup$
Please, do not consider this an answer to the mathematical question, but an extended comment for programmers on how to implement the solution as a computer program.
You can consider this is a shameless rip-off from the answer by Ross Millikan, rewritten for programmers implementing solutions to this type of problem, with a few practical details and suggestions sprinkled in.
In a computer program, to represent a 2D point moving at fixed speed in a fixed direction, it is best to use
$$begin{cases}
x(t) = x_0 + t x_v \
y(t) = y_0 + t y_v \
end{cases} tag{1}label{NA1}$$
because the vector algebra solution without trigonometric functions is easier to implement, and on typical current hardware, faster as well (because trigonometric functions are "slow" compared to addition, multiplication, and division, on most hardware architectures).
If the point is at $(x_{t_0}, y_{t_0})$ at time $t = t_0$, with speed $v$ and direction $varphi$ (from origin, $varphi = 0text{°} = 360text{°}$ is towards $+x$, $varphi = 90text{°} = -270text{°}$ is towards $+y$, $varphi = 180text{°} = -180text{°}$ is towards $-x$, and $varphi = 270text{°} = -90text{°}$ is towards $-y$), then
$$begin{cases}
x_0 = x_{t_0} - t_0 v cosvarphi \
y_0 = y_{t_0} - t_0 v sinvarphi \
x_v = v cosvarphi \
y_v = v sinvarphi \
end{cases} tag{2}label{NA2}$$
In other words, using $eqref{NA1}$ does not mean we cannot define the point trajectories using (angular) direction and speed; it is just that internally, you'll want to use the vector form.
Let's say that we know $eqref{NA1}$ for some target, and wish to intercept it with a point that starts at $(X_T , Y_0T$ at time $t = T$, with constant speed $V$. Mathematically, for our projectile, we have
$$begin{cases}
X(t) = X_T + (t - T) X_v \
Y(t) = Y_T + (t - T) Y_v \
X_v^2 + Y_v^2 = V^2
end{cases}$$
and we need to solve for $t$, $X_v$, and $Y_v$ from
$$bbox{begin{cases}
X(t) = x(t) \
Y(t) = y(t) \
X_v^2 + Y_v^2 = v^2
end{cases}} quad iff quad bbox{begin{cases}
X_0 - x_0 + t (X_v - x_v) - t_0 X_v = 0 \
Y_0 - y_0 + t (Y_v - y_v) - t_0 Y_v = 0 \
X_v^2 + Y_v^2 = v^2
end{cases}}$$
In practice, it is best to solve for $t$ first. For the interception to be possible, we obviously only care about solutions where $t ge T$, since we cannot fire backwards in time; and a solution $t = T$ means the target intercepts the projectile starting point $(X_T , Y_T)$ at time $t = T$.
There are two cases we need to handle separately:
Target and projectile speeds are equal, $x_v^2 + y_v^2 = V^2$.
First, calculate
$$bbox{d = T (x_v^2 + y_v^2) - x_v (X_T - x_0) - y_v (Y_T - y_0) }$$
If $d = 0$, interception is not possible.
Otherwise, calculate
$$bbox{t = frac{T^2 (x_v^2 + y_v^2) - (X_T - x_0)^2 - (Y_T - y_0)^2}{2 d}}$$
If $t le T$, interception is not possible.
Target and projectile velocities differ, $x_v^2 + y_v^2 ne V^2$.
First, calculate
$$bbox{ r = V^2 bigr( (T x_v + x_0 - X_T)^2 + (T y_v + y_0 - Y_T)^2 bigr) - bigr( x_v (y_0 - Y_T) - y_v (x_0 - X_T) bigr)^2 }$$
If $r le 0$, interception is not possible.
Then, calculate
$$bbox{ begin{aligned}
s &= T V^2 + x_v ( x_0 - X_T ) + y_v ( y_0 - Y_T) \
d &= V^2 - x_v^2 - y_v^2 \
end{aligned} }$$
and finally
$$bbox{ begin{aligned}
t_1 &= frac{s + sqrt{r}}{d} \
t_2 &= frac{s - sqrt{r}}{d} \
end{aligned} }$$
If both $t_1 le T$ and $t_2 le T$, intersection is not possible.
If both $t_1 gt T$ and $t_2 gt T$, set $t = min( t_1 , t_2 )$.
Otherwise, set $t = t_1$ if $t_1 gt T$; or $t = t_2$ if $T_2 gt T$.
At this point, we have the time of the interception $t gt T$ if interception is indeed possible. Then, the velocity vector $(X_V , Y_V)$ for that interception is
$$bbox{ begin{cases}
displaystyle X_V = frac{x_0 - X_T + t x_v}{t - T} \
displaystyle Y_V = frac{y_0 - Y_T + t y_v}{t - T} \
end{cases} }$$
If the interception time $t$ was computed as above, you'll find that $X_V^2 + Y_V^2 = V^2$, to within rounding error.
Here is an example implementation in Python 3:
#!/usr/bin/python3
#
# SPDX-License-Identifier: CC0-1.0
#
# Public domain. Written by Nominal Animal; no warranties, no guarantees.
#
import math
import sys
class MovingPoint(tuple):
"""2D point moving in a constant direction at a constant velocity"""
def __new__(cls, xt, yt, t=0, xv=None, yv=None, speed=None, direction=None):
if (speed is not None) and (direction is not None):
angle = float(direction) * 3.14159265358979323846 / 180.0
xv = float(speed) * math.cos(angle)
yv = float(speed) * math.sin(angle)
else:
if xv is None:
xv = 0.0
if yv is None:
yv = 0.0
x0 = float(xt) - float(t)*xv
y0 = float(yt) - float(t)*yv
return super().__new__(cls, (x0, y0, xv, yv))
@property
def speed(self):
"""Speed"""
return math.sqrt(self[2]*self[2] + self[3]*self[3])
@property
def direction(self):
"""Direction in degrees"""
return math.atan2(self[3], self[2]) * 180.0 / 3.14159265358979323846
@property
def x0(self):
"""X coordinate at time t=0"""
return self[0]
@property
def y0(self):
"""Y coordinate at time t=0"""
return self[1]
@property
def xv(self):
"""Velocity vector x component"""
return self[2]
@property
def yv(self):
"""Velocity vector y component"""
return self[3]
@property
def p0(self):
"""Location at time t=0"""
return (self[0], self[1])
@property
def v(self):
"""Velocity vector"""
return (self[2], self[3])
def x(self, t):
"""X coordinate as a function of time"""
return self[0] + float(t) * self[2]
def y(self, t):
"""Y coordinate as a function of time"""
return self[1] + float(t) * self[3]
def p(self, t):
"""Location as a function of time"""
return (self[0] + float(t) * self[2], self[1] + float(t) * self[3])
def intercept(mpoint, xt, yt, t, v):
"""Calculate interception to MovingPoint mpoint.
Returns None if no intersection, tuple (t, xv, yv) otherwise."""
x0, y0, xv, yv = mpoint.x0, mpoint.y0, mpoint.xv, mpoint.yv
xt, yt, t, v = float(xt), float(yt), float(t), float(v)
mvv = xv*xv + yv*yv
vv = v*v
if mvv == vv:
d = t*mvv - xv*(xt - x0) - yv*(yt - y0)
if d == 0.0:
return None
it = (t*t * mvv - (xt - x0)**2 - (yt - y0)**2) / (2*d)
if it <= t:
return None
else:
r = vv*( (t*xv + x0 - xt)**2 + (t*yv + y0 - yt)**2 ) - ( xv*(y0 - yt) - yv*(x0 - xt) )**2
if r <= 0:
return None
r = math.sqrt(r)
s = t*vv + xv*(x0 - xt) + yv*(y0 - yt)
d = vv - mvv
t1 = (s + r) / d
t2 = (s - r) / d
if t1 > t and t2 > t:
it = min(t1, t2)
elif t1 > t:
it = t1
elif t2 > t:
it = t2
else:
return None
vx = (x0 - xt + it*xv) / (it - t)
vy = (y0 - yt + it*yv) / (it - t)
return (it, vx, vy)
if __name__ == '__main__':
if len(sys.argv) != 9:
sys.stderr.write("n")
sys.stderr.write("Usage: %s [ -h | --help | help ]n" % sys.argv[0])
sys.stderr.write(" %s x0 y0 xv yv XT YT T Vn" % sys.argv[0])
sys.stderr.write("n")
sys.exit(1)
x0 = float(sys.argv[1])
y0 = float(sys.argv[2])
xv = float(sys.argv[3])
yv = float(sys.argv[4])
sys.stderr.write("Target: x(0)=%.6f, y(0)=%.6f, xv=%.6f, yv=%.6fn" % (x0, y0, xv, yv))
xt = float(sys.argv[5])
yt = float(sys.argv[6])
t = float(sys.argv[7])
v = float(sys.argv[8])
sys.stderr.write("Interceptor: x(%.6f)=%.6f, y(%.6f)=%.6f, speed=%.6fn" % (t, xt, t, yt, v))
target = MovingPoint(x0, y0, xv=xv, yv=yv)
icept = intercept(target, xt, yt, t, v)
if icept is None:
sys.stderr.write("Interception is not possible.n")
else:
sys.stderr.write("Interception occurs at time %.6f, using xv=%.6f, yv=%.6fn" % icept)
bullet = MovingPoint(xt, yt, t=t, xv=icept[1], yv=icept[2])
sys.stdout.write("#time target_x target_y interceptor_x interceptor_yn")
N = 20
t0 = min(t, 0.0)
prevtau = t0 - 1
for i in range(0, N+1):
tau = ((N-i)*t0 + i*icept[0]) / N
if prevtau < t and tau > t:
sys.stdout.write("%.6f %.6f %.6f %.6f %.6fn" % (t, target.x(t), target.y(t), bullet.x(t), bullet.y(t)))
elif prevtau < 0 and tau > 0:
sys.stdout.write("%.6f %.6f %.6f %.6f %.6fn" % (0, target.x(0), target.y(0), bullet.x(0), bullet.y(0)))
prevtau = tau
if tau < t:
sys.stdout.write("%.6f %.6f %.6fn" % (tau, target.x(tau), target.y(tau)))
else:
sys.stdout.write("%.6f %.6f %.6f %.6f %.6fn" % (tau, target.x(tau), target.y(tau), bullet.x(tau), bullet.y(tau)))
The MovingPoint class implements a 2D point that moves in a constant direction at constant speed. The intercept() function computes an interception trajectory.
If you run the above program using e.g. python3 intercept.py 1.00 3.25 0.5 0.3 8.00 -4.00 5.5 0.85, we test if we can hit a target $x(t) = 1.00 + 0.5 t$, $y(t) = 3.25 + 0.3 t$, with a projectile that starts at $x(5.5) = 8.00$, $y(5.5) = -4.00$, with velocity $0.85$. The output is
Target: x(0)=1.000000, y(0)=3.250000, xv=0.500000, yv=0.300000
Interceptor: x(5.500000)=8.000000, y(5.500000)=-4.000000, speed=0.850000
Interception occurs at time 22.935375, using xv=0.256243, yv=0.810456
#time target_x target_y interceptor_x interceptor_y
0.000000 1.000000 3.250000
1.146769 1.573384 3.594031
2.293538 2.146769 3.938061
3.440306 2.720153 4.282092
4.587075 3.293538 4.626123
5.500000 3.750000 4.900000 8.000000 -4.000000
5.733844 3.866922 4.970153 8.059921 -3.810480
6.880613 4.440306 5.314184 8.353772 -2.881074
8.027381 5.013691 5.658214 8.647623 -1.951667
9.174150 5.587075 6.002245 8.941474 -1.022261
10.320919 6.160459 6.346276 9.235325 -0.092855
11.467688 6.733844 6.690306 9.529176 0.836551
12.614456 7.307228 7.034337 9.823028 1.765957
13.761225 7.880613 7.378368 10.116879 2.695363
14.907994 8.453997 7.722398 10.410730 3.624769
16.054763 9.027381 8.066429 10.704581 4.554176
17.201531 9.600766 8.410459 10.998432 5.483582
18.348300 10.174150 8.754490 11.292283 6.412988
19.495069 10.747534 9.098521 11.586134 7.342394
20.641838 11.320919 9.442551 11.879985 8.271800
21.788607 11.894303 9.786582 12.173837 9.201206
22.935375 12.467688 10.130613 12.467688 10.130613
which tells us that yes, it is possible, using $x_v = 0.256243$ and $y_v = 0.810456$ ($sqrt{x_v^2 + y_v^2} = 0.85$ as specified), in which case the intercept occurs at $(12.467688, 10.130613)$ at time $t = 22.935375$.
If you redirect the output to a file, you can plot the trajectories using plot "file" u 2:3 t "target" w linespoints, "file" u 4:5 t "interceptor" w linespoints.
$endgroup$
add a comment |
$begingroup$
Please, do not consider this an answer to the mathematical question, but an extended comment for programmers on how to implement the solution as a computer program.
You can consider this is a shameless rip-off from the answer by Ross Millikan, rewritten for programmers implementing solutions to this type of problem, with a few practical details and suggestions sprinkled in.
In a computer program, to represent a 2D point moving at fixed speed in a fixed direction, it is best to use
$$begin{cases}
x(t) = x_0 + t x_v \
y(t) = y_0 + t y_v \
end{cases} tag{1}label{NA1}$$
because the vector algebra solution without trigonometric functions is easier to implement, and on typical current hardware, faster as well (because trigonometric functions are "slow" compared to addition, multiplication, and division, on most hardware architectures).
If the point is at $(x_{t_0}, y_{t_0})$ at time $t = t_0$, with speed $v$ and direction $varphi$ (from origin, $varphi = 0text{°} = 360text{°}$ is towards $+x$, $varphi = 90text{°} = -270text{°}$ is towards $+y$, $varphi = 180text{°} = -180text{°}$ is towards $-x$, and $varphi = 270text{°} = -90text{°}$ is towards $-y$), then
$$begin{cases}
x_0 = x_{t_0} - t_0 v cosvarphi \
y_0 = y_{t_0} - t_0 v sinvarphi \
x_v = v cosvarphi \
y_v = v sinvarphi \
end{cases} tag{2}label{NA2}$$
In other words, using $eqref{NA1}$ does not mean we cannot define the point trajectories using (angular) direction and speed; it is just that internally, you'll want to use the vector form.
Let's say that we know $eqref{NA1}$ for some target, and wish to intercept it with a point that starts at $(X_T , Y_0T$ at time $t = T$, with constant speed $V$. Mathematically, for our projectile, we have
$$begin{cases}
X(t) = X_T + (t - T) X_v \
Y(t) = Y_T + (t - T) Y_v \
X_v^2 + Y_v^2 = V^2
end{cases}$$
and we need to solve for $t$, $X_v$, and $Y_v$ from
$$bbox{begin{cases}
X(t) = x(t) \
Y(t) = y(t) \
X_v^2 + Y_v^2 = v^2
end{cases}} quad iff quad bbox{begin{cases}
X_0 - x_0 + t (X_v - x_v) - t_0 X_v = 0 \
Y_0 - y_0 + t (Y_v - y_v) - t_0 Y_v = 0 \
X_v^2 + Y_v^2 = v^2
end{cases}}$$
In practice, it is best to solve for $t$ first. For the interception to be possible, we obviously only care about solutions where $t ge T$, since we cannot fire backwards in time; and a solution $t = T$ means the target intercepts the projectile starting point $(X_T , Y_T)$ at time $t = T$.
There are two cases we need to handle separately:
Target and projectile speeds are equal, $x_v^2 + y_v^2 = V^2$.
First, calculate
$$bbox{d = T (x_v^2 + y_v^2) - x_v (X_T - x_0) - y_v (Y_T - y_0) }$$
If $d = 0$, interception is not possible.
Otherwise, calculate
$$bbox{t = frac{T^2 (x_v^2 + y_v^2) - (X_T - x_0)^2 - (Y_T - y_0)^2}{2 d}}$$
If $t le T$, interception is not possible.
Target and projectile velocities differ, $x_v^2 + y_v^2 ne V^2$.
First, calculate
$$bbox{ r = V^2 bigr( (T x_v + x_0 - X_T)^2 + (T y_v + y_0 - Y_T)^2 bigr) - bigr( x_v (y_0 - Y_T) - y_v (x_0 - X_T) bigr)^2 }$$
If $r le 0$, interception is not possible.
Then, calculate
$$bbox{ begin{aligned}
s &= T V^2 + x_v ( x_0 - X_T ) + y_v ( y_0 - Y_T) \
d &= V^2 - x_v^2 - y_v^2 \
end{aligned} }$$
and finally
$$bbox{ begin{aligned}
t_1 &= frac{s + sqrt{r}}{d} \
t_2 &= frac{s - sqrt{r}}{d} \
end{aligned} }$$
If both $t_1 le T$ and $t_2 le T$, intersection is not possible.
If both $t_1 gt T$ and $t_2 gt T$, set $t = min( t_1 , t_2 )$.
Otherwise, set $t = t_1$ if $t_1 gt T$; or $t = t_2$ if $T_2 gt T$.
At this point, we have the time of the interception $t gt T$ if interception is indeed possible. Then, the velocity vector $(X_V , Y_V)$ for that interception is
$$bbox{ begin{cases}
displaystyle X_V = frac{x_0 - X_T + t x_v}{t - T} \
displaystyle Y_V = frac{y_0 - Y_T + t y_v}{t - T} \
end{cases} }$$
If the interception time $t$ was computed as above, you'll find that $X_V^2 + Y_V^2 = V^2$, to within rounding error.
Here is an example implementation in Python 3:
#!/usr/bin/python3
#
# SPDX-License-Identifier: CC0-1.0
#
# Public domain. Written by Nominal Animal; no warranties, no guarantees.
#
import math
import sys
class MovingPoint(tuple):
"""2D point moving in a constant direction at a constant velocity"""
def __new__(cls, xt, yt, t=0, xv=None, yv=None, speed=None, direction=None):
if (speed is not None) and (direction is not None):
angle = float(direction) * 3.14159265358979323846 / 180.0
xv = float(speed) * math.cos(angle)
yv = float(speed) * math.sin(angle)
else:
if xv is None:
xv = 0.0
if yv is None:
yv = 0.0
x0 = float(xt) - float(t)*xv
y0 = float(yt) - float(t)*yv
return super().__new__(cls, (x0, y0, xv, yv))
@property
def speed(self):
"""Speed"""
return math.sqrt(self[2]*self[2] + self[3]*self[3])
@property
def direction(self):
"""Direction in degrees"""
return math.atan2(self[3], self[2]) * 180.0 / 3.14159265358979323846
@property
def x0(self):
"""X coordinate at time t=0"""
return self[0]
@property
def y0(self):
"""Y coordinate at time t=0"""
return self[1]
@property
def xv(self):
"""Velocity vector x component"""
return self[2]
@property
def yv(self):
"""Velocity vector y component"""
return self[3]
@property
def p0(self):
"""Location at time t=0"""
return (self[0], self[1])
@property
def v(self):
"""Velocity vector"""
return (self[2], self[3])
def x(self, t):
"""X coordinate as a function of time"""
return self[0] + float(t) * self[2]
def y(self, t):
"""Y coordinate as a function of time"""
return self[1] + float(t) * self[3]
def p(self, t):
"""Location as a function of time"""
return (self[0] + float(t) * self[2], self[1] + float(t) * self[3])
def intercept(mpoint, xt, yt, t, v):
"""Calculate interception to MovingPoint mpoint.
Returns None if no intersection, tuple (t, xv, yv) otherwise."""
x0, y0, xv, yv = mpoint.x0, mpoint.y0, mpoint.xv, mpoint.yv
xt, yt, t, v = float(xt), float(yt), float(t), float(v)
mvv = xv*xv + yv*yv
vv = v*v
if mvv == vv:
d = t*mvv - xv*(xt - x0) - yv*(yt - y0)
if d == 0.0:
return None
it = (t*t * mvv - (xt - x0)**2 - (yt - y0)**2) / (2*d)
if it <= t:
return None
else:
r = vv*( (t*xv + x0 - xt)**2 + (t*yv + y0 - yt)**2 ) - ( xv*(y0 - yt) - yv*(x0 - xt) )**2
if r <= 0:
return None
r = math.sqrt(r)
s = t*vv + xv*(x0 - xt) + yv*(y0 - yt)
d = vv - mvv
t1 = (s + r) / d
t2 = (s - r) / d
if t1 > t and t2 > t:
it = min(t1, t2)
elif t1 > t:
it = t1
elif t2 > t:
it = t2
else:
return None
vx = (x0 - xt + it*xv) / (it - t)
vy = (y0 - yt + it*yv) / (it - t)
return (it, vx, vy)
if __name__ == '__main__':
if len(sys.argv) != 9:
sys.stderr.write("n")
sys.stderr.write("Usage: %s [ -h | --help | help ]n" % sys.argv[0])
sys.stderr.write(" %s x0 y0 xv yv XT YT T Vn" % sys.argv[0])
sys.stderr.write("n")
sys.exit(1)
x0 = float(sys.argv[1])
y0 = float(sys.argv[2])
xv = float(sys.argv[3])
yv = float(sys.argv[4])
sys.stderr.write("Target: x(0)=%.6f, y(0)=%.6f, xv=%.6f, yv=%.6fn" % (x0, y0, xv, yv))
xt = float(sys.argv[5])
yt = float(sys.argv[6])
t = float(sys.argv[7])
v = float(sys.argv[8])
sys.stderr.write("Interceptor: x(%.6f)=%.6f, y(%.6f)=%.6f, speed=%.6fn" % (t, xt, t, yt, v))
target = MovingPoint(x0, y0, xv=xv, yv=yv)
icept = intercept(target, xt, yt, t, v)
if icept is None:
sys.stderr.write("Interception is not possible.n")
else:
sys.stderr.write("Interception occurs at time %.6f, using xv=%.6f, yv=%.6fn" % icept)
bullet = MovingPoint(xt, yt, t=t, xv=icept[1], yv=icept[2])
sys.stdout.write("#time target_x target_y interceptor_x interceptor_yn")
N = 20
t0 = min(t, 0.0)
prevtau = t0 - 1
for i in range(0, N+1):
tau = ((N-i)*t0 + i*icept[0]) / N
if prevtau < t and tau > t:
sys.stdout.write("%.6f %.6f %.6f %.6f %.6fn" % (t, target.x(t), target.y(t), bullet.x(t), bullet.y(t)))
elif prevtau < 0 and tau > 0:
sys.stdout.write("%.6f %.6f %.6f %.6f %.6fn" % (0, target.x(0), target.y(0), bullet.x(0), bullet.y(0)))
prevtau = tau
if tau < t:
sys.stdout.write("%.6f %.6f %.6fn" % (tau, target.x(tau), target.y(tau)))
else:
sys.stdout.write("%.6f %.6f %.6f %.6f %.6fn" % (tau, target.x(tau), target.y(tau), bullet.x(tau), bullet.y(tau)))
The MovingPoint class implements a 2D point that moves in a constant direction at constant speed. The intercept() function computes an interception trajectory.
If you run the above program using e.g. python3 intercept.py 1.00 3.25 0.5 0.3 8.00 -4.00 5.5 0.85, we test if we can hit a target $x(t) = 1.00 + 0.5 t$, $y(t) = 3.25 + 0.3 t$, with a projectile that starts at $x(5.5) = 8.00$, $y(5.5) = -4.00$, with velocity $0.85$. The output is
Target: x(0)=1.000000, y(0)=3.250000, xv=0.500000, yv=0.300000
Interceptor: x(5.500000)=8.000000, y(5.500000)=-4.000000, speed=0.850000
Interception occurs at time 22.935375, using xv=0.256243, yv=0.810456
#time target_x target_y interceptor_x interceptor_y
0.000000 1.000000 3.250000
1.146769 1.573384 3.594031
2.293538 2.146769 3.938061
3.440306 2.720153 4.282092
4.587075 3.293538 4.626123
5.500000 3.750000 4.900000 8.000000 -4.000000
5.733844 3.866922 4.970153 8.059921 -3.810480
6.880613 4.440306 5.314184 8.353772 -2.881074
8.027381 5.013691 5.658214 8.647623 -1.951667
9.174150 5.587075 6.002245 8.941474 -1.022261
10.320919 6.160459 6.346276 9.235325 -0.092855
11.467688 6.733844 6.690306 9.529176 0.836551
12.614456 7.307228 7.034337 9.823028 1.765957
13.761225 7.880613 7.378368 10.116879 2.695363
14.907994 8.453997 7.722398 10.410730 3.624769
16.054763 9.027381 8.066429 10.704581 4.554176
17.201531 9.600766 8.410459 10.998432 5.483582
18.348300 10.174150 8.754490 11.292283 6.412988
19.495069 10.747534 9.098521 11.586134 7.342394
20.641838 11.320919 9.442551 11.879985 8.271800
21.788607 11.894303 9.786582 12.173837 9.201206
22.935375 12.467688 10.130613 12.467688 10.130613
which tells us that yes, it is possible, using $x_v = 0.256243$ and $y_v = 0.810456$ ($sqrt{x_v^2 + y_v^2} = 0.85$ as specified), in which case the intercept occurs at $(12.467688, 10.130613)$ at time $t = 22.935375$.
If you redirect the output to a file, you can plot the trajectories using plot "file" u 2:3 t "target" w linespoints, "file" u 4:5 t "interceptor" w linespoints.
$endgroup$
add a comment |
$begingroup$
Please, do not consider this an answer to the mathematical question, but an extended comment for programmers on how to implement the solution as a computer program.
You can consider this is a shameless rip-off from the answer by Ross Millikan, rewritten for programmers implementing solutions to this type of problem, with a few practical details and suggestions sprinkled in.
In a computer program, to represent a 2D point moving at fixed speed in a fixed direction, it is best to use
$$begin{cases}
x(t) = x_0 + t x_v \
y(t) = y_0 + t y_v \
end{cases} tag{1}label{NA1}$$
because the vector algebra solution without trigonometric functions is easier to implement, and on typical current hardware, faster as well (because trigonometric functions are "slow" compared to addition, multiplication, and division, on most hardware architectures).
If the point is at $(x_{t_0}, y_{t_0})$ at time $t = t_0$, with speed $v$ and direction $varphi$ (from origin, $varphi = 0text{°} = 360text{°}$ is towards $+x$, $varphi = 90text{°} = -270text{°}$ is towards $+y$, $varphi = 180text{°} = -180text{°}$ is towards $-x$, and $varphi = 270text{°} = -90text{°}$ is towards $-y$), then
$$begin{cases}
x_0 = x_{t_0} - t_0 v cosvarphi \
y_0 = y_{t_0} - t_0 v sinvarphi \
x_v = v cosvarphi \
y_v = v sinvarphi \
end{cases} tag{2}label{NA2}$$
In other words, using $eqref{NA1}$ does not mean we cannot define the point trajectories using (angular) direction and speed; it is just that internally, you'll want to use the vector form.
Let's say that we know $eqref{NA1}$ for some target, and wish to intercept it with a point that starts at $(X_T , Y_0T$ at time $t = T$, with constant speed $V$. Mathematically, for our projectile, we have
$$begin{cases}
X(t) = X_T + (t - T) X_v \
Y(t) = Y_T + (t - T) Y_v \
X_v^2 + Y_v^2 = V^2
end{cases}$$
and we need to solve for $t$, $X_v$, and $Y_v$ from
$$bbox{begin{cases}
X(t) = x(t) \
Y(t) = y(t) \
X_v^2 + Y_v^2 = v^2
end{cases}} quad iff quad bbox{begin{cases}
X_0 - x_0 + t (X_v - x_v) - t_0 X_v = 0 \
Y_0 - y_0 + t (Y_v - y_v) - t_0 Y_v = 0 \
X_v^2 + Y_v^2 = v^2
end{cases}}$$
In practice, it is best to solve for $t$ first. For the interception to be possible, we obviously only care about solutions where $t ge T$, since we cannot fire backwards in time; and a solution $t = T$ means the target intercepts the projectile starting point $(X_T , Y_T)$ at time $t = T$.
There are two cases we need to handle separately:
Target and projectile speeds are equal, $x_v^2 + y_v^2 = V^2$.
First, calculate
$$bbox{d = T (x_v^2 + y_v^2) - x_v (X_T - x_0) - y_v (Y_T - y_0) }$$
If $d = 0$, interception is not possible.
Otherwise, calculate
$$bbox{t = frac{T^2 (x_v^2 + y_v^2) - (X_T - x_0)^2 - (Y_T - y_0)^2}{2 d}}$$
If $t le T$, interception is not possible.
Target and projectile velocities differ, $x_v^2 + y_v^2 ne V^2$.
First, calculate
$$bbox{ r = V^2 bigr( (T x_v + x_0 - X_T)^2 + (T y_v + y_0 - Y_T)^2 bigr) - bigr( x_v (y_0 - Y_T) - y_v (x_0 - X_T) bigr)^2 }$$
If $r le 0$, interception is not possible.
Then, calculate
$$bbox{ begin{aligned}
s &= T V^2 + x_v ( x_0 - X_T ) + y_v ( y_0 - Y_T) \
d &= V^2 - x_v^2 - y_v^2 \
end{aligned} }$$
and finally
$$bbox{ begin{aligned}
t_1 &= frac{s + sqrt{r}}{d} \
t_2 &= frac{s - sqrt{r}}{d} \
end{aligned} }$$
If both $t_1 le T$ and $t_2 le T$, intersection is not possible.
If both $t_1 gt T$ and $t_2 gt T$, set $t = min( t_1 , t_2 )$.
Otherwise, set $t = t_1$ if $t_1 gt T$; or $t = t_2$ if $T_2 gt T$.
At this point, we have the time of the interception $t gt T$ if interception is indeed possible. Then, the velocity vector $(X_V , Y_V)$ for that interception is
$$bbox{ begin{cases}
displaystyle X_V = frac{x_0 - X_T + t x_v}{t - T} \
displaystyle Y_V = frac{y_0 - Y_T + t y_v}{t - T} \
end{cases} }$$
If the interception time $t$ was computed as above, you'll find that $X_V^2 + Y_V^2 = V^2$, to within rounding error.
Here is an example implementation in Python 3:
#!/usr/bin/python3
#
# SPDX-License-Identifier: CC0-1.0
#
# Public domain. Written by Nominal Animal; no warranties, no guarantees.
#
import math
import sys
class MovingPoint(tuple):
"""2D point moving in a constant direction at a constant velocity"""
def __new__(cls, xt, yt, t=0, xv=None, yv=None, speed=None, direction=None):
if (speed is not None) and (direction is not None):
angle = float(direction) * 3.14159265358979323846 / 180.0
xv = float(speed) * math.cos(angle)
yv = float(speed) * math.sin(angle)
else:
if xv is None:
xv = 0.0
if yv is None:
yv = 0.0
x0 = float(xt) - float(t)*xv
y0 = float(yt) - float(t)*yv
return super().__new__(cls, (x0, y0, xv, yv))
@property
def speed(self):
"""Speed"""
return math.sqrt(self[2]*self[2] + self[3]*self[3])
@property
def direction(self):
"""Direction in degrees"""
return math.atan2(self[3], self[2]) * 180.0 / 3.14159265358979323846
@property
def x0(self):
"""X coordinate at time t=0"""
return self[0]
@property
def y0(self):
"""Y coordinate at time t=0"""
return self[1]
@property
def xv(self):
"""Velocity vector x component"""
return self[2]
@property
def yv(self):
"""Velocity vector y component"""
return self[3]
@property
def p0(self):
"""Location at time t=0"""
return (self[0], self[1])
@property
def v(self):
"""Velocity vector"""
return (self[2], self[3])
def x(self, t):
"""X coordinate as a function of time"""
return self[0] + float(t) * self[2]
def y(self, t):
"""Y coordinate as a function of time"""
return self[1] + float(t) * self[3]
def p(self, t):
"""Location as a function of time"""
return (self[0] + float(t) * self[2], self[1] + float(t) * self[3])
def intercept(mpoint, xt, yt, t, v):
"""Calculate interception to MovingPoint mpoint.
Returns None if no intersection, tuple (t, xv, yv) otherwise."""
x0, y0, xv, yv = mpoint.x0, mpoint.y0, mpoint.xv, mpoint.yv
xt, yt, t, v = float(xt), float(yt), float(t), float(v)
mvv = xv*xv + yv*yv
vv = v*v
if mvv == vv:
d = t*mvv - xv*(xt - x0) - yv*(yt - y0)
if d == 0.0:
return None
it = (t*t * mvv - (xt - x0)**2 - (yt - y0)**2) / (2*d)
if it <= t:
return None
else:
r = vv*( (t*xv + x0 - xt)**2 + (t*yv + y0 - yt)**2 ) - ( xv*(y0 - yt) - yv*(x0 - xt) )**2
if r <= 0:
return None
r = math.sqrt(r)
s = t*vv + xv*(x0 - xt) + yv*(y0 - yt)
d = vv - mvv
t1 = (s + r) / d
t2 = (s - r) / d
if t1 > t and t2 > t:
it = min(t1, t2)
elif t1 > t:
it = t1
elif t2 > t:
it = t2
else:
return None
vx = (x0 - xt + it*xv) / (it - t)
vy = (y0 - yt + it*yv) / (it - t)
return (it, vx, vy)
if __name__ == '__main__':
if len(sys.argv) != 9:
sys.stderr.write("n")
sys.stderr.write("Usage: %s [ -h | --help | help ]n" % sys.argv[0])
sys.stderr.write(" %s x0 y0 xv yv XT YT T Vn" % sys.argv[0])
sys.stderr.write("n")
sys.exit(1)
x0 = float(sys.argv[1])
y0 = float(sys.argv[2])
xv = float(sys.argv[3])
yv = float(sys.argv[4])
sys.stderr.write("Target: x(0)=%.6f, y(0)=%.6f, xv=%.6f, yv=%.6fn" % (x0, y0, xv, yv))
xt = float(sys.argv[5])
yt = float(sys.argv[6])
t = float(sys.argv[7])
v = float(sys.argv[8])
sys.stderr.write("Interceptor: x(%.6f)=%.6f, y(%.6f)=%.6f, speed=%.6fn" % (t, xt, t, yt, v))
target = MovingPoint(x0, y0, xv=xv, yv=yv)
icept = intercept(target, xt, yt, t, v)
if icept is None:
sys.stderr.write("Interception is not possible.n")
else:
sys.stderr.write("Interception occurs at time %.6f, using xv=%.6f, yv=%.6fn" % icept)
bullet = MovingPoint(xt, yt, t=t, xv=icept[1], yv=icept[2])
sys.stdout.write("#time target_x target_y interceptor_x interceptor_yn")
N = 20
t0 = min(t, 0.0)
prevtau = t0 - 1
for i in range(0, N+1):
tau = ((N-i)*t0 + i*icept[0]) / N
if prevtau < t and tau > t:
sys.stdout.write("%.6f %.6f %.6f %.6f %.6fn" % (t, target.x(t), target.y(t), bullet.x(t), bullet.y(t)))
elif prevtau < 0 and tau > 0:
sys.stdout.write("%.6f %.6f %.6f %.6f %.6fn" % (0, target.x(0), target.y(0), bullet.x(0), bullet.y(0)))
prevtau = tau
if tau < t:
sys.stdout.write("%.6f %.6f %.6fn" % (tau, target.x(tau), target.y(tau)))
else:
sys.stdout.write("%.6f %.6f %.6f %.6f %.6fn" % (tau, target.x(tau), target.y(tau), bullet.x(tau), bullet.y(tau)))
The MovingPoint class implements a 2D point that moves in a constant direction at constant speed. The intercept() function computes an interception trajectory.
If you run the above program using e.g. python3 intercept.py 1.00 3.25 0.5 0.3 8.00 -4.00 5.5 0.85, we test if we can hit a target $x(t) = 1.00 + 0.5 t$, $y(t) = 3.25 + 0.3 t$, with a projectile that starts at $x(5.5) = 8.00$, $y(5.5) = -4.00$, with velocity $0.85$. The output is
Target: x(0)=1.000000, y(0)=3.250000, xv=0.500000, yv=0.300000
Interceptor: x(5.500000)=8.000000, y(5.500000)=-4.000000, speed=0.850000
Interception occurs at time 22.935375, using xv=0.256243, yv=0.810456
#time target_x target_y interceptor_x interceptor_y
0.000000 1.000000 3.250000
1.146769 1.573384 3.594031
2.293538 2.146769 3.938061
3.440306 2.720153 4.282092
4.587075 3.293538 4.626123
5.500000 3.750000 4.900000 8.000000 -4.000000
5.733844 3.866922 4.970153 8.059921 -3.810480
6.880613 4.440306 5.314184 8.353772 -2.881074
8.027381 5.013691 5.658214 8.647623 -1.951667
9.174150 5.587075 6.002245 8.941474 -1.022261
10.320919 6.160459 6.346276 9.235325 -0.092855
11.467688 6.733844 6.690306 9.529176 0.836551
12.614456 7.307228 7.034337 9.823028 1.765957
13.761225 7.880613 7.378368 10.116879 2.695363
14.907994 8.453997 7.722398 10.410730 3.624769
16.054763 9.027381 8.066429 10.704581 4.554176
17.201531 9.600766 8.410459 10.998432 5.483582
18.348300 10.174150 8.754490 11.292283 6.412988
19.495069 10.747534 9.098521 11.586134 7.342394
20.641838 11.320919 9.442551 11.879985 8.271800
21.788607 11.894303 9.786582 12.173837 9.201206
22.935375 12.467688 10.130613 12.467688 10.130613
which tells us that yes, it is possible, using $x_v = 0.256243$ and $y_v = 0.810456$ ($sqrt{x_v^2 + y_v^2} = 0.85$ as specified), in which case the intercept occurs at $(12.467688, 10.130613)$ at time $t = 22.935375$.
If you redirect the output to a file, you can plot the trajectories using plot "file" u 2:3 t "target" w linespoints, "file" u 4:5 t "interceptor" w linespoints.
$endgroup$
Please, do not consider this an answer to the mathematical question, but an extended comment for programmers on how to implement the solution as a computer program.
You can consider this is a shameless rip-off from the answer by Ross Millikan, rewritten for programmers implementing solutions to this type of problem, with a few practical details and suggestions sprinkled in.
In a computer program, to represent a 2D point moving at fixed speed in a fixed direction, it is best to use
$$begin{cases}
x(t) = x_0 + t x_v \
y(t) = y_0 + t y_v \
end{cases} tag{1}label{NA1}$$
because the vector algebra solution without trigonometric functions is easier to implement, and on typical current hardware, faster as well (because trigonometric functions are "slow" compared to addition, multiplication, and division, on most hardware architectures).
If the point is at $(x_{t_0}, y_{t_0})$ at time $t = t_0$, with speed $v$ and direction $varphi$ (from origin, $varphi = 0text{°} = 360text{°}$ is towards $+x$, $varphi = 90text{°} = -270text{°}$ is towards $+y$, $varphi = 180text{°} = -180text{°}$ is towards $-x$, and $varphi = 270text{°} = -90text{°}$ is towards $-y$), then
$$begin{cases}
x_0 = x_{t_0} - t_0 v cosvarphi \
y_0 = y_{t_0} - t_0 v sinvarphi \
x_v = v cosvarphi \
y_v = v sinvarphi \
end{cases} tag{2}label{NA2}$$
In other words, using $eqref{NA1}$ does not mean we cannot define the point trajectories using (angular) direction and speed; it is just that internally, you'll want to use the vector form.
Let's say that we know $eqref{NA1}$ for some target, and wish to intercept it with a point that starts at $(X_T , Y_0T$ at time $t = T$, with constant speed $V$. Mathematically, for our projectile, we have
$$begin{cases}
X(t) = X_T + (t - T) X_v \
Y(t) = Y_T + (t - T) Y_v \
X_v^2 + Y_v^2 = V^2
end{cases}$$
and we need to solve for $t$, $X_v$, and $Y_v$ from
$$bbox{begin{cases}
X(t) = x(t) \
Y(t) = y(t) \
X_v^2 + Y_v^2 = v^2
end{cases}} quad iff quad bbox{begin{cases}
X_0 - x_0 + t (X_v - x_v) - t_0 X_v = 0 \
Y_0 - y_0 + t (Y_v - y_v) - t_0 Y_v = 0 \
X_v^2 + Y_v^2 = v^2
end{cases}}$$
In practice, it is best to solve for $t$ first. For the interception to be possible, we obviously only care about solutions where $t ge T$, since we cannot fire backwards in time; and a solution $t = T$ means the target intercepts the projectile starting point $(X_T , Y_T)$ at time $t = T$.
There are two cases we need to handle separately:
Target and projectile speeds are equal, $x_v^2 + y_v^2 = V^2$.
First, calculate
$$bbox{d = T (x_v^2 + y_v^2) - x_v (X_T - x_0) - y_v (Y_T - y_0) }$$
If $d = 0$, interception is not possible.
Otherwise, calculate
$$bbox{t = frac{T^2 (x_v^2 + y_v^2) - (X_T - x_0)^2 - (Y_T - y_0)^2}{2 d}}$$
If $t le T$, interception is not possible.
Target and projectile velocities differ, $x_v^2 + y_v^2 ne V^2$.
First, calculate
$$bbox{ r = V^2 bigr( (T x_v + x_0 - X_T)^2 + (T y_v + y_0 - Y_T)^2 bigr) - bigr( x_v (y_0 - Y_T) - y_v (x_0 - X_T) bigr)^2 }$$
If $r le 0$, interception is not possible.
Then, calculate
$$bbox{ begin{aligned}
s &= T V^2 + x_v ( x_0 - X_T ) + y_v ( y_0 - Y_T) \
d &= V^2 - x_v^2 - y_v^2 \
end{aligned} }$$
and finally
$$bbox{ begin{aligned}
t_1 &= frac{s + sqrt{r}}{d} \
t_2 &= frac{s - sqrt{r}}{d} \
end{aligned} }$$
If both $t_1 le T$ and $t_2 le T$, intersection is not possible.
If both $t_1 gt T$ and $t_2 gt T$, set $t = min( t_1 , t_2 )$.
Otherwise, set $t = t_1$ if $t_1 gt T$; or $t = t_2$ if $T_2 gt T$.
At this point, we have the time of the interception $t gt T$ if interception is indeed possible. Then, the velocity vector $(X_V , Y_V)$ for that interception is
$$bbox{ begin{cases}
displaystyle X_V = frac{x_0 - X_T + t x_v}{t - T} \
displaystyle Y_V = frac{y_0 - Y_T + t y_v}{t - T} \
end{cases} }$$
If the interception time $t$ was computed as above, you'll find that $X_V^2 + Y_V^2 = V^2$, to within rounding error.
Here is an example implementation in Python 3:
#!/usr/bin/python3
#
# SPDX-License-Identifier: CC0-1.0
#
# Public domain. Written by Nominal Animal; no warranties, no guarantees.
#
import math
import sys
class MovingPoint(tuple):
"""2D point moving in a constant direction at a constant velocity"""
def __new__(cls, xt, yt, t=0, xv=None, yv=None, speed=None, direction=None):
if (speed is not None) and (direction is not None):
angle = float(direction) * 3.14159265358979323846 / 180.0
xv = float(speed) * math.cos(angle)
yv = float(speed) * math.sin(angle)
else:
if xv is None:
xv = 0.0
if yv is None:
yv = 0.0
x0 = float(xt) - float(t)*xv
y0 = float(yt) - float(t)*yv
return super().__new__(cls, (x0, y0, xv, yv))
@property
def speed(self):
"""Speed"""
return math.sqrt(self[2]*self[2] + self[3]*self[3])
@property
def direction(self):
"""Direction in degrees"""
return math.atan2(self[3], self[2]) * 180.0 / 3.14159265358979323846
@property
def x0(self):
"""X coordinate at time t=0"""
return self[0]
@property
def y0(self):
"""Y coordinate at time t=0"""
return self[1]
@property
def xv(self):
"""Velocity vector x component"""
return self[2]
@property
def yv(self):
"""Velocity vector y component"""
return self[3]
@property
def p0(self):
"""Location at time t=0"""
return (self[0], self[1])
@property
def v(self):
"""Velocity vector"""
return (self[2], self[3])
def x(self, t):
"""X coordinate as a function of time"""
return self[0] + float(t) * self[2]
def y(self, t):
"""Y coordinate as a function of time"""
return self[1] + float(t) * self[3]
def p(self, t):
"""Location as a function of time"""
return (self[0] + float(t) * self[2], self[1] + float(t) * self[3])
def intercept(mpoint, xt, yt, t, v):
"""Calculate interception to MovingPoint mpoint.
Returns None if no intersection, tuple (t, xv, yv) otherwise."""
x0, y0, xv, yv = mpoint.x0, mpoint.y0, mpoint.xv, mpoint.yv
xt, yt, t, v = float(xt), float(yt), float(t), float(v)
mvv = xv*xv + yv*yv
vv = v*v
if mvv == vv:
d = t*mvv - xv*(xt - x0) - yv*(yt - y0)
if d == 0.0:
return None
it = (t*t * mvv - (xt - x0)**2 - (yt - y0)**2) / (2*d)
if it <= t:
return None
else:
r = vv*( (t*xv + x0 - xt)**2 + (t*yv + y0 - yt)**2 ) - ( xv*(y0 - yt) - yv*(x0 - xt) )**2
if r <= 0:
return None
r = math.sqrt(r)
s = t*vv + xv*(x0 - xt) + yv*(y0 - yt)
d = vv - mvv
t1 = (s + r) / d
t2 = (s - r) / d
if t1 > t and t2 > t:
it = min(t1, t2)
elif t1 > t:
it = t1
elif t2 > t:
it = t2
else:
return None
vx = (x0 - xt + it*xv) / (it - t)
vy = (y0 - yt + it*yv) / (it - t)
return (it, vx, vy)
if __name__ == '__main__':
if len(sys.argv) != 9:
sys.stderr.write("n")
sys.stderr.write("Usage: %s [ -h | --help | help ]n" % sys.argv[0])
sys.stderr.write(" %s x0 y0 xv yv XT YT T Vn" % sys.argv[0])
sys.stderr.write("n")
sys.exit(1)
x0 = float(sys.argv[1])
y0 = float(sys.argv[2])
xv = float(sys.argv[3])
yv = float(sys.argv[4])
sys.stderr.write("Target: x(0)=%.6f, y(0)=%.6f, xv=%.6f, yv=%.6fn" % (x0, y0, xv, yv))
xt = float(sys.argv[5])
yt = float(sys.argv[6])
t = float(sys.argv[7])
v = float(sys.argv[8])
sys.stderr.write("Interceptor: x(%.6f)=%.6f, y(%.6f)=%.6f, speed=%.6fn" % (t, xt, t, yt, v))
target = MovingPoint(x0, y0, xv=xv, yv=yv)
icept = intercept(target, xt, yt, t, v)
if icept is None:
sys.stderr.write("Interception is not possible.n")
else:
sys.stderr.write("Interception occurs at time %.6f, using xv=%.6f, yv=%.6fn" % icept)
bullet = MovingPoint(xt, yt, t=t, xv=icept[1], yv=icept[2])
sys.stdout.write("#time target_x target_y interceptor_x interceptor_yn")
N = 20
t0 = min(t, 0.0)
prevtau = t0 - 1
for i in range(0, N+1):
tau = ((N-i)*t0 + i*icept[0]) / N
if prevtau < t and tau > t:
sys.stdout.write("%.6f %.6f %.6f %.6f %.6fn" % (t, target.x(t), target.y(t), bullet.x(t), bullet.y(t)))
elif prevtau < 0 and tau > 0:
sys.stdout.write("%.6f %.6f %.6f %.6f %.6fn" % (0, target.x(0), target.y(0), bullet.x(0), bullet.y(0)))
prevtau = tau
if tau < t:
sys.stdout.write("%.6f %.6f %.6fn" % (tau, target.x(tau), target.y(tau)))
else:
sys.stdout.write("%.6f %.6f %.6f %.6f %.6fn" % (tau, target.x(tau), target.y(tau), bullet.x(tau), bullet.y(tau)))
The MovingPoint class implements a 2D point that moves in a constant direction at constant speed. The intercept() function computes an interception trajectory.
If you run the above program using e.g. python3 intercept.py 1.00 3.25 0.5 0.3 8.00 -4.00 5.5 0.85, we test if we can hit a target $x(t) = 1.00 + 0.5 t$, $y(t) = 3.25 + 0.3 t$, with a projectile that starts at $x(5.5) = 8.00$, $y(5.5) = -4.00$, with velocity $0.85$. The output is
Target: x(0)=1.000000, y(0)=3.250000, xv=0.500000, yv=0.300000
Interceptor: x(5.500000)=8.000000, y(5.500000)=-4.000000, speed=0.850000
Interception occurs at time 22.935375, using xv=0.256243, yv=0.810456
#time target_x target_y interceptor_x interceptor_y
0.000000 1.000000 3.250000
1.146769 1.573384 3.594031
2.293538 2.146769 3.938061
3.440306 2.720153 4.282092
4.587075 3.293538 4.626123
5.500000 3.750000 4.900000 8.000000 -4.000000
5.733844 3.866922 4.970153 8.059921 -3.810480
6.880613 4.440306 5.314184 8.353772 -2.881074
8.027381 5.013691 5.658214 8.647623 -1.951667
9.174150 5.587075 6.002245 8.941474 -1.022261
10.320919 6.160459 6.346276 9.235325 -0.092855
11.467688 6.733844 6.690306 9.529176 0.836551
12.614456 7.307228 7.034337 9.823028 1.765957
13.761225 7.880613 7.378368 10.116879 2.695363
14.907994 8.453997 7.722398 10.410730 3.624769
16.054763 9.027381 8.066429 10.704581 4.554176
17.201531 9.600766 8.410459 10.998432 5.483582
18.348300 10.174150 8.754490 11.292283 6.412988
19.495069 10.747534 9.098521 11.586134 7.342394
20.641838 11.320919 9.442551 11.879985 8.271800
21.788607 11.894303 9.786582 12.173837 9.201206
22.935375 12.467688 10.130613 12.467688 10.130613
which tells us that yes, it is possible, using $x_v = 0.256243$ and $y_v = 0.810456$ ($sqrt{x_v^2 + y_v^2} = 0.85$ as specified), in which case the intercept occurs at $(12.467688, 10.130613)$ at time $t = 22.935375$.
If you redirect the output to a file, you can plot the trajectories using plot "file" u 2:3 t "target" w linespoints, "file" u 4:5 t "interceptor" w linespoints.
answered Dec 28 '18 at 11:34
Nominal AnimalNominal Animal
7,1132617
7,1132617
add a comment |
add a comment |
Thanks for contributing an answer to Mathematics Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f680863%2fcar-chase-trajectory-and-time%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
Did you try to use vectors? Also, "angle of 60" relative to what? The line connecting Car1 and Car2? The x-axis?
$endgroup$
– arne.b
Feb 18 '14 at 14:40
$begingroup$
Ignore my comment. I can't read.
$endgroup$
– user121049
Feb 18 '14 at 14:40
$begingroup$
the angle is relative to x axis
$endgroup$
– Andrius
Feb 18 '14 at 14:46
$begingroup$
The 2D case works like 2 1D cases, except that you do not know Car1's speed in x-direction or Car1's speed in y-direction, only a combination of both, which gives you a system of 2 equations to transform and insert into a third one, ultimately solving a quadratic equation, no matter whether you derive them from a vector-based approach, coordinates, or else. Again, what did you try?
$endgroup$
– arne.b
Feb 18 '14 at 16:40
$begingroup$
I have not tried a lot. I actually had no idea how to do it. But the simultaneous equation idea sounds good it might just work, I will try it.
$endgroup$
– Andrius
Feb 18 '14 at 19:02