# Calculate the probability of seeing a landmark when starting at a given point and walking straight in a random direction

Input 5 integers: Landmark X & Y, Starting Point X & Y, and View Distance Radius D

Output 1 double: % chance of coming within D of Landmark (X,Y) ("seeing") when walking straight in a random direction from Starting Point (X,Y).

Assume that "random direction" shall be a direction uniform on [0,2π], the land is flat, and of course the 'hiker' never turns after he begins moving.

Example: If landmark is at (10,10), starting point is (5,5), and view distance is 20 then the output would be 100% as no matter which direction you walk the landmark will be immediately visible.

• This kinda lost its appeal since you already linked to the solution. – Martin Ender Jul 24 '14 at 18:23
• @MartinBüttner I hadn't considered that since it is hardly implemented as code... but I suppose I can remove it if people also want to solve the math. – MetaGuru Jul 24 '14 at 19:08
• @ioSamurai just wanted to let you know that people can still see the math if they choose to look at the revisions - although I don't see any problem with leaving the math there.. people would be able to copy the math from the first answer, and would be challenged with optimizing the space anyways... – user2813274 Aug 6 '14 at 15:18
• @user2813274 Yeah I didn't think showing the math was an issue anyways because it was really about the coding. – MetaGuru Aug 6 '14 at 15:24
• If you want to make this challenging/interesting, i suggest you ban trigonometric functions. Accuracy of probability should also be specified (I suggest to the nearest 1%, given that probability can be from 0 to 100%.) – Level River St Aug 6 '14 at 15:39

## CJam -41-40-39-38/3531/26

This seems to work. It is my first attempt at CJam and/or codegolf. Run the code at http://cjam.aditsu.net/. In the section called input just place the variables as integers delimited with spaces in the input block in this order: distance D, landmark x, starting x, landmark y, starting y (for example 20 10 5 10 5). I had posted a previous one based on a misunderstanding of the equation that has been resolved. I also had been returning answers as probability ratios rather than percentages. Note: the second code has only 31 characters but combines alot from another user's CJam code.

ri{riri- 2#}:U;UU+.5#/mSP/:A.5<A1?e2

r{~riri-_*}_~+mq/mSP/:A.5<A1?e2


Accuratish one without arcsine being directly called (44 characters):

ririri- 2#riri- 2#+ .5#/:A1<AA3#6/+P/1?100*


Even more accurate (52 characters):

ririri- 2#riri- 2#+ .5#/:A1<AA3#6/+A5#40/3*+P/1?100


Update:

The absolute best I've written is 26 characters. I've still learned alot by watching professorfish's attempts but the crux is mine. I assumes (potentially incorrectly) that if you can see the landmark if it is closer (not closer than or equal to) your sight radius.

1r{~riri-_*}_~+mq/mSP/e<e2


import Data.Complex
(s,l)%d=100*min(asin(d/magnitude(s-l))/pi)1


Use like (xstart:+ystart, xlandmark:+ylandmark) % distance. Gives result in percent. Why is it so expensive to load modules in Haskell?!?

Note that there is no if/then/else, pattern matching, etc. in this code, min does the magic.

• I didn't expect Haskell to do so well here, nor the Spanish Inquisition. :D – cjfaure Aug 7 '14 at 20:12
• what is the undocumented feature, if i may ask? – proud haskeller Aug 7 '14 at 20:56
• is it the fixity of min? – proud haskeller Aug 7 '14 at 21:04
• @proud haskeller: the way min deals with NaN. Since NaN compares to a non-NaN to False, the exact implementation of min is relevant (whether or not it uses the inverse of a result of a comparison). As far as I can tell, this behaviour is not specified anywhere, but I'd be glad to be proven wrong. – TheSpanishInquisition Aug 7 '14 at 21:05
• when you write NaN, you mean undefined? or is there some other NaN I'm not aware about? – proud haskeller Aug 7 '14 at 21:13

# CJam, 44 40 38 37

First CJam script! Uses the method on the Math.SE answer here.

Supports non-integer inputs as well, at no extra character cost.

rd{rdrd-_*}:U~U+mq/_1<{mSP/}{;1}?100*


## Order of inputs

The inputs are given in this order, on STDIN, separated by spaces:

2. Landmark X
3. Starting Point X
4. Landmark Y
5. Starting Point Y
• Why not use P instead of 3.14? P starts off defined as pi This will give you the same length as mine. – kaine Aug 6 '14 at 19:24
• @kaine Fortunately I've thought of another saving, I'm now beating you by one character – user16402 Aug 6 '14 at 19:26
• How is that fortunate? – kaine Aug 6 '14 at 19:26
• There is, what I believe, to be a mistake in this. If you start exactly as far as you can see away, you get 50% on yours 100% on mine. Easy change. An it is debatable which is correct. – kaine Aug 6 '14 at 19:33
• ri{riri-_}:U;UU+mq/_1>{;1}{mSP/}?100 you are welcome dammit – kaine Aug 6 '14 at 20:05

# Haskell — 72 70 69 68

w s y l z d|r<d=100|0<1=asin(d/r)/pi*100where r=sqrt$(l-s)^2+(z-y)^2  • you can replace True with 0<1, and use$ in the definition of r. – proud haskeller Aug 6 '14 at 19:53
• Thanks, I can't believe I missed that \$ opportunity. – DrJPepper Aug 6 '14 at 20:17
• You could also squish where up against 100. – comperendinous Aug 6 '14 at 20:48
• It breaks vim's syntax highlighting (why I didn't delete the space in the first place), but it does indeed compile. – DrJPepper Aug 6 '14 at 20:54
• Also, asinh should really be asin. The character reduction is a bonus! – comperendinous Aug 6 '14 at 21:33

# Python - 73

from math import*
f=lambda l,s,d:(abs(l-s)<=d or asin(d/abs(l-s))/pi)*100


l is a complex number (e.g. 5 + 5j) describing the landmark position, s describes the start position and d is the view distance, for the example from the question call f as follows: f(5 + 5j, 10 + 10j, 20)

• I guess you are missing a bracket (. – Falko Aug 9 '14 at 13:49
• Now you are missing two brackets (...). ;) Otherwise you might return True rather than 100 in case the landmark is closer than the distance d. – Falko Aug 9 '14 at 20:29
• Oh, that's what you mean... – hlt Aug 9 '14 at 21:43