10
\$\begingroup\$

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.

input

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).

output

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/...).

examples

inputs

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

outputs

30
GROUNDED
229

Shortest code wins.

\$\endgroup\$
  • 1
    \$\begingroup\$ flat earth? or small fuel tank :-) \$\endgroup\$ – pmg May 4 '11 at 20:32
  • 5
    \$\begingroup\$ Wait... the earth isn't flat? \$\endgroup\$ – Keith Randall May 4 '11 at 20:53
  • 1
    \$\begingroup\$ (Xb-Xa)(V·sinα+Wy)=(Yb-Ya)(V·cosα+Wx) ... Great. \$\endgroup\$ – Oleh Prypin May 4 '11 at 20:58
  • \$\begingroup\$ @BlaXpirit: What if the two vectors are in opposite directions? \$\endgroup\$ – Lowjacker May 4 '11 at 22:53
  • \$\begingroup\$ @BlaXpirit: Search for a sin x + b cos x = c on Google for some methods of solving your equation. Direct solving may not be the best way to go here, though... \$\endgroup\$ – Keith Randall May 6 '11 at 21:17
1
\$\begingroup\$

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'
30
   h '0 0 10 0 50 -55 0'
GROUNDED
   h '3.3 9.1 -2.7 1.1 95.0 8.8 1.7'
229

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
30

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

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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