13
\$\begingroup\$

An angry bird is shot at an angle \$β\$ to the horizontal at a speed \$u\$. The ground is steep, inclined at an angle \$α\$. Find the horizontal distance \$q\$ that the bird traveled before it hit the ground.

diagram showing an inclined plane at angle α and an angry bird being fired at angle β at speed u

Make a function \$f(α, β, u)\$ that returns the length \$q\$: the horizontal distance that the bird traveled before it hit the ground.

Constraints and notes:

  • \$-90° < α < 90°\$.
  • \$0° < β < 180°\$.
  • \$α < β\$.
  • \$0 \le u < 10^9\$.
  • Assume acceleration due to gravity \$g = 10\$.
  • You may use radians instead of degrees for \$α\$, \$β\$.
  • Dimensions of \$u\$ are irrelevant as long as they are consistent with \$g\$ and \$q\$.
  • No air resistance or anything too fancy.

Shortest code wins.

See the Wikipedia article on projectile motion for some equations.

Samples:

f(0, 45, 10) = 10
f(0, 90, 100) = 0
f(26.565, 45, 10) = 5
f(26.565, 135, 10) = 15
\$\endgroup\$
1
  • \$\begingroup\$ As I saw some confusion about the formula, here it is for others to use it: q = ABS[1/5 u^2 Cos[β] Sec[α] Sin[β - α]] \$\endgroup\$ Commented Feb 14, 2011 at 12:09

3 Answers 3

3
\$\begingroup\$

Java

Works for radians only

double q(double a, double b, double u){
          return (Math.abs(((-Math.tan(a)+(Math.tan(b)))*(u*u)*(0.2*(Math.cos(b)*Math.cos(b))))));
      }

Golfed Version (Thanks to Peter)

double z=u*Math.cos(b);return(Math.tan(b)-Math.tan(a))*z*z/5;

Maths Used:

$$ q = ut\cos \beta \\ q\tan \alpha = ut\sin \beta - 0.5 \times 10 t^2 \\ - \tan \alpha + \tan \beta = 5\frac q {u^2} \sec^2 \beta \\ q = \frac {(\tan \beta - \tan \alpha)u^2} {5\sec^2 \beta } $$

\$\endgroup\$
12
  • \$\begingroup\$ There is something wrong with this... I just cant figure out correctly, can some1 help? \$\endgroup\$ Commented Feb 12, 2011 at 23:44
  • \$\begingroup\$ This formula is not correct. Please see comment at gnibbler's post \$\endgroup\$
    – Eelvex
    Commented Feb 13, 2011 at 21:37
  • \$\begingroup\$ So yet, we dont have any perfect solution :) \$\endgroup\$ Commented Feb 13, 2011 at 23:06
  • 1
    \$\begingroup\$ updated the formula... fire some testcases now please \$\endgroup\$ Commented Feb 13, 2011 at 23:42
  • \$\begingroup\$ You can save a few chars - Math.abs is unnecessary, -x+y is shorter as y-x, *0.2 is shorter as /5, and you have unnecessary brackets. OTOH you're missing the return type of the method. \$\endgroup\$ Commented Feb 14, 2011 at 0:06
2
\$\begingroup\$

Haskell (37 35)

Based on Aman's solution:

q a b u=(tan a+tan b)*u*u*cos b^2/5

I think, this problem isn't real code-golf, as it is more implementing a formula than really doing some algorithm.

\$\endgroup\$
3
  • \$\begingroup\$ Maybe you are right, since the formula is already too short. \$\endgroup\$
    – Eelvex
    Commented Feb 12, 2011 at 23:45
  • 1
    \$\begingroup\$ Would something like /5 or /5. work? \$\endgroup\$
    – Nabb
    Commented Feb 13, 2011 at 15:51
  • 1
    \$\begingroup\$ This formula is not correct. Please see comment at gnibbler's post. \$\endgroup\$
    – Eelvex
    Commented Feb 13, 2011 at 21:37
2
\$\begingroup\$

Python3 - 65 chars

from math import*
f=lambda α,β,u:(tan(α)+tan(β))*u*u*.2*cos(β)**2
\$\endgroup\$
2
  • \$\begingroup\$ That's not quite correct. 1) f should always be positive and 2) for α > 0 it returns a larger value than for a=0, which is not possible. \$\endgroup\$
    – Eelvex
    Commented Feb 13, 2011 at 21:36
  • \$\begingroup\$ Ah well, I copied FUZxxl's formula :/ \$\endgroup\$
    – gnibbler
    Commented Feb 14, 2011 at 1:04

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.