You are tasked with planning a flying route for a local airplane delivery company. You need to route an airplane from point A to point B. You just can't start at A, point the airplane at B, and go, however, as the prevailing winds will blow you off course. Instead you need to figure out which direction you should point the airplane so that it will fly directly to B, taking the wind into account.


7 floating-point numbers, encoding A_x, A_y, B_x, B_y, S, W_x, W_y. These are the coordinates of your start and destination, the airspeed of your airplane, and the strength of the wind along the x and y axes (the direction the wind blows to, not from).


You should print the angle in degrees (rotating counterclockwise from the positive x axis) that the plane should point to reach B in a straight line. Print GROUNDED if the wind is so strong as to make the trip impossible.

You may round to the nearest degree, and do so with any method you would like (up/down/nearest/...).



0 0 10 0 100 0 -50
0 0 10 0 50 -55 0
3.3 9.1 -2.7 1.1 95.0 8.8 1.7



Shortest code wins.

  • 1
    \$\begingroup\$ flat earth? or small fuel tank :-) \$\endgroup\$
    – pmg
    May 4, 2011 at 20:32
  • 5
    \$\begingroup\$ Wait... the earth isn't flat? \$\endgroup\$ May 4, 2011 at 20:53
  • 1
    \$\begingroup\$ (Xb-Xa)(V·sinα+Wy)=(Yb-Ya)(V·cosα+Wx) ... Great. \$\endgroup\$ May 4, 2011 at 20:58
  • \$\begingroup\$ @BlaXpirit: What if the two vectors are in opposite directions? \$\endgroup\$
    – Lowjacker
    May 4, 2011 at 22:53
  • 1
    \$\begingroup\$ Actually the earth IS flat. Learn your geography. \$\endgroup\$ Sep 17, 2011 at 19:42

3 Answers 3


Perl - 222 chars

use Math::Trig;($A,$a,$B,$b,$s,$W,$w)=split' ',<>;$c=atan2($b-$a,$B-$A);$A=atan2($w,$W);$S=sqrt($W*$W+$w*$w);$X=$S*sin($A-$c);$T=$S*cos($A-$c);$C=asin($X/$s);print((-$T>$s*cos($C))?"GROUNDED":(360+rad2deg($c-$C))%360,"\n")

Straighforward algorithm, and really only golfed by squeezing whitespace and variable name length, but I thought we needed a first answer here. I've been learning some J for golfing; I suspect simply translating to J (or Ruby) will readily beat this. Off to try those.

$X = crosswind component, $T = tailwind component. We're grounded if the tailwind is actually a headwind (i.e., negative) and stronger than our airspeed. Otherwise, $C is the wind correction angle that we subtract from our course $c to get our heading. We need to turn far enough to balance the crosswind with the cross-track component of our velocity.


J - 155 chars

h=:3 :0
'c w s'=.(([:j./[:-~/2 2$4{.]),([:j./5 6{]),4{])0".y
'T X'=.+.w*+c%|c
C=.-_1 o.X%s
>((s>|w)*.(-T)<s*2 o.C){'GROUNDED';360|<.360+(C+{:*.c)*180%o.1

For example:

   h '0 0 10 0 100 0 -50'
   h '0 0 10 0 50 -55 0'
   h '3.3 9.1 -2.7 1.1 95.0 8.8 1.7'

Remove the 0". ahead of y if you don't mind J numeric syntax (_ for unary negation):

   h 0 0 10 0 100 0 _50

As I mentioned in my Perl answer, I'm only learning J, but liking its power.


Perl: 193

Admittedly this is (mostly) DCharness's Perl code: but who doesn't like self-rewriting source?

use Math::Trig;$/=' ';@i=<>;$_='1=atan2(3-1,2-0);0=atan2(6,5);2=sqrt(6*6+5*5);5=2*sin(0-1);3=2*cos(0-1);6=asin(5/4);print-3>4*cos 6?GROUNDED:int rad2deg(1-6),"\n"';s/((?<!\w)\d)/\$i[$1]/g;eval

Also it will output degrees out of spec (i.e. <0 or >360), but did I mention self-rewriting source?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.