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

6

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.

MetaGuru

Posted 2014-07-24T16:35:51.457

Reputation: 71

1This kinda lost its appeal since you already linked to the solution. – Martin Ender – 2014-07-24T18:23:02.573

@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 – 2014-07-24T19:08:03.173

@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 – 2014-08-06T15:18:13.467

@user2813274 Yeah I didn't think showing the math was an issue anyways because it was really about the coding. – MetaGuru – 2014-08-06T15:24:23.243

2If 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 – 2014-08-06T15:39:14.450

Answers

3

CJam -41 -40 -39 -38/35 31/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

kaine

Posted 2014-07-24T16:35:51.457

Reputation: 536

6

Haskell, 65 63

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.

TheSpanishInquisition

Posted 2014-07-24T16:35:51.457

Reputation: 421

2I didn't expect Haskell to do so well here, nor the Spanish Inquisition. :D – cjfaure – 2014-08-07T20:12:21.787

what is the undocumented feature, if i may ask? – proud haskeller – 2014-08-07T20:56:00.367

is it the fixity of \min``? – proud haskeller – 2014-08-07T21:04:56.857

@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 – 2014-08-07T21:05:32.500

when you write NaN, you mean undefined? or is there some other NaN I'm not aware about? – proud haskeller – 2014-08-07T21:13:42.750

oh you're talking about 0/0? i see. – proud haskeller – 2014-08-07T21:17:51.467

We commented simultaneously, so yes, it's the fixity, and it's in the hs98 report, but you have to think about it when you read it. By NaN I mean the result of a computation that doesn't have a result representable in the relevant data type, such as asin x with abs x > 1 in this case. undefined is something very different. – TheSpanishInquisition – 2014-08-07T21:19:05.043

actually, i'd say that is an undocumented feature in the instance of Compare (Complex a), but yes. – proud haskeller – 2014-08-07T21:21:41.357

i used to think NaN is a value that is represented by the data type, but has no meaning, something like 3%0 in Data.Ratio . but i guess it could be useful returning NaN instead of undefined so that something valuable could be done with the result even when an error occurred. – proud haskeller – 2014-08-07T21:24:29.223

what happens if you input 0,0(start) 0,5(landmark) 5(viewing distance)? I think this code is effectively identical to my 26 character one. – kaine – 2014-08-07T23:00:51.497

@kaine: 50, just like it should. – TheSpanishInquisition – 2014-08-08T05:21:27.847

Im not sure whether it should or shouldn't. Can you see it if $ r <d $ or $ r <=d $? I prefer it this , however, as that is the shorter of my answers. – kaine – 2014-08-08T11:28:09.070

+1, importing is really expensive when golfing in Haskell. To use sort (compare 'on' key), one need to do import Data.List; import Data.Function;... – Ray – 2014-08-12T12:34:40.453

2

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*

Interpreter at cjam.aditsu.net.

Order of inputs

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

  1. View Distance Radius
  2. Landmark X
  3. Starting Point X
  4. Landmark Y
  5. Starting Point Y

user16402

Posted 2014-07-24T16:35:51.457

Reputation:

Why not use P instead of 3.14? P starts off defined as pi This will give you the same length as mine. – kaine – 2014-08-06T19:24:34.823

1@kaine Fortunately I've thought of another saving, I'm now beating you by one character – None – 2014-08-06T19:26:14.790

1How is that fortunate? – kaine – 2014-08-06T19:26:56.283

1There 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 – 2014-08-06T19:33:28.040

ri{riri-_}:U;UU+mq/_1>{;1}{mSP/}?100 you are welcome dammit – kaine – 2014-08-06T20:05:12.783

I'm going to stop becaue the more I try to trim both yours and mine, the closer to each other they get! – kaine – 2014-08-06T20:23:08.217

@kaine fixed the bug, same char count – None – 2014-08-07T07:47:58.447

1

Haskell — 72 70 69 68

I think this satisfies the question, but if not please leave a comment and I'll do my best to fix it:

w s y l z d|r<d=100|0<1=asin(d/r)/pi*100where r=sqrt$(l-s)^2+(z-y)^2

DrJPepper

Posted 2014-07-24T16:35:51.457

Reputation: 499

you can replace True with 0<1, and use $ in the definition of r. – proud haskeller – 2014-08-06T19:53:53.900

Thanks, I can't believe I missed that $ opportunity. – DrJPepper – 2014-08-06T20:17:50.100

You could also squish where up against 100. – comperendinous – 2014-08-06T20:48:32.847

It breaks vim's syntax highlighting (why I didn't delete the space in the first place), but it does indeed compile. – DrJPepper – 2014-08-06T20:54:31.477

1Also, asinh should really be asin. The character reduction is a bonus! – comperendinous – 2014-08-06T21:33:06.880

asin kept returning NaN – DrJPepper – 2014-08-07T03:27:08.520

They're not the same. asin(1)=PI/2=1.57 asinh(1)=0.88. If asin is giving NaN it's because you're giving it an argument outside the range -1 to 1, which doesn't make sense according to the definition of sin. – Level River St – 2014-08-08T10:13:56.210

@steveverrill Sorry about that, I tried again and I think when I was getting NaN I was using invalid arguments. – DrJPepper – 2014-08-08T18:45:42.933

1

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)

hlt

Posted 2014-07-24T16:35:51.457

Reputation: 181

I guess you are missing a bracket (. – Falko – 2014-08-09T13:49:47.633

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 – 2014-08-09T20:29:30.553

Oh, that's what you mean... – hlt – 2014-08-09T21:43:03.110