27
2
Code a program or function to construct an interactive canvas on the screen of at least 400 pixels x 400 pixels in size. The canvas can be any color you wish, bordered or borderless, with or without a title bar, etc., just some form of obvious canvas.
The user will click on two distinct areas of the canvas and the program must output the Euclidean distance (in pixels) between those two clicks in some fashion (STDOUT, displaying an alert, etc.). The two clicks can be only left clicks, only right clicks, a left click for the first and right click for the second, two double-left-clicks, etc., any combination is acceptable. Special Note: Clicking-and-dragging (e.g., using MOUSEUP as the second point) is specifically not allowed; they must be two distinct clicks.
The user must be able do this multiple times, and must get an output each time, until the program is closed/force-quit/killed/etc. You can choose the method of closure (clicking an X, ctrl-C, etc.), whatever is golfier for your code.
Rules
- Either a full program or a function are acceptable. If a function, however, you must still display the output to the user somehow (simply returning the value is not acceptable).
- Output can be to the console, displayed as an alert, populated onto the canvas, etc.
- Standard loopholes are forbidden.
- This is code-golf so all usual golfing rules apply, and the shortest code (in bytes) wins.
Can we output exact results (which may contain square roots) or does it have to be decimals? – Martin Ender – 2017-11-01T14:00:24.613
@MartinEnder Either/or. Output formatting isn't the interesting part of this challenge. – AdmBorkBork – 2017-11-01T14:13:14.407
1The HTML/JS solution, from the second click, outputs the distance from that click to previous click. (so if the user click
n
times,n-1
instead offloor(n/2)
numbers are printed) Is that allowed? – user202729 – 2017-11-01T15:08:40.910@user202729 It's a little outside of what I was envisioning, but I can see how it complies with the rules I have written above, so I'll allow it. – AdmBorkBork – 2017-11-01T15:11:43.843
1Can we use a smaller canvas size if our language can't support a 400 x 400 canvas? – Scott Milner – 2017-11-01T16:14:56.603
@ScottMilner If your language absolutely cannot support that size, then that's allowed. Please provide documentation showing that limitation. – AdmBorkBork – 2017-11-01T16:53:32.963
Is it allowed to spawn a temporary (visual) turtle for each 'begin' point clicked? Each one is flushed out after the next 'begin' point is selected. – Yytsi – 2017-11-02T12:00:42.330
Regarding clicks, you say
any combination is acceptable
. Does that mean the program can require any specific combination or that it must be able to handle all the combinations? – Engineer Toast – 2017-11-02T12:22:11.443@TuukkaX Sure, that's fine. – AdmBorkBork – 2017-11-02T12:38:36.153
@EngineerToast The first - you can decide what clicks are required for your program to work right. – AdmBorkBork – 2017-11-02T12:39:36.700
Hmm, I'll have to see if I can work up something using Minecraft command blocks...
– BradC – 2017-11-02T17:05:31.983Is touching the screen (on a touchscreen) acceptable? – 12Me21 – 2017-11-03T18:45:08.500
@12Me21 Sure, that's the equivalent of clicking. I'll allow that. – AdmBorkBork – 2017-11-03T19:44:19.597
Would it be allowed to have just a 400x400 pixel area of a bigger canvas work properly and do weird other stuff for the rest of the canvas? – Fabian Röling – 2017-11-08T12:54:26.373
@BradC Did it work? Even if not: What was your idea? – Fabian Röling – 2017-11-08T12:55:09.823
My answer is outputting for points distance (a,b) then distance (c,d). Is it valid if I make it to output every time except the first click of them all? I mean outputting for dist between points (a,b), and (b,c). – sergiol – 2017-11-11T21:17:46.040
@sergiol Yes, outputting after every click except the first (that is, given three clicks A, B, C, treating the C click as the second-click in the pair (B,C)) is perfectly acceptable. There are a few answers already doing that. – AdmBorkBork – 2017-11-14T13:24:19.613