Smallest Integer Disk

23

2

This challenge is about finding the smallest disk that contains some given points. This is made somewhat trickier, however, by the fact that in this challenge, the disk's coordinates and radius must both be integers.

Your input will be a list of points with integer coordinates x and y. You can take this as a list of tuples, a list of lists, or any other way to represent a collection of pairs. x and y will both be (possibly negative) integers. Every point is guaranteed to be unique, and there will be at least one point.

Your output will be a disk in the form of three numbers, X, Y, and R. X, Y, and R are all integers, X and Y represent the disk's center and R represents its radius. The distance between every given point and the center must be less than or equal to R, and there must not exist such a disk with a smaller R that also satisfies this condition.

It is possible that there will be multiple possible solutions for a given input, your code must output at least one of them in this case.

You can use any kinds of geometry builtins your language supports if there are any, and input/output may be through built-in point/disk objects instead of just numbers.

Test Cases

Input   (Possible) Output(s)
(x,y)   (X,Y,R)
-------------------------
(0,0)   (0,0,0)
-------------------------
(0,1)   (0,0,1)
(1,0)   (1,1,1)
-------------------------
(1,4)   (4,4,3)
(3,2)
(4,1)
(4,5)
(5,2)
(7,4)
-------------------------
(-1,0)  (0,0,2)
(2,0)   (1,0,2)
-------------------------
(-1,0)  (1,0,2)
(2,1)   (0,1,2)
-------------------------
(0,0)   (1,0,1)
(1,1)   (0,1,1)

Fewest bytes wins.

Pavel

Posted 2018-11-06T16:39:42.087

Reputation: 8 585

Sandbox – Pavel – 2018-11-06T16:39:50.553

Found a couple typos, if you don't mind me pointing them out: "This is made somewhat trickier..."; "...represents the disk's center and R represents its radius..."; "...and there must not exist such a disk..." – J. Sallé – 2018-11-06T17:06:02.127

2Usually making things integer just make code-golf easier. – user202729 – 2018-11-06T17:13:55.280

@KevinCruijssen That's by coincidence. Inputs can be in any order. – Pavel – 2018-11-06T18:04:53.690

1@Pavel The input can be in any order, or we can take the input in any order? As in, the input can be in any order and we should manually sort first in our solution, or can we already take the input pre-sorted? – Kevin Cruijssen – 2018-11-06T18:07:54.927

@KevinCruijssen Any order implies not sorted. You should not make any assumptions about the input already following a certain rule. – Pavel – 2018-11-06T18:10:05.310

Answers

6

Jelly, 25 24 22 21 20 18 bytes

«/r»/Œpµ³_²§½ṀĊ,)Ṃ

Thanks to @EriktheOutgolfer for letting me know about ), saving 1 byte.

Thanks to @Dennis for saving 2 bytes.

Try it online!

Explanation

«/r»/Œpµ³_²§½ṀĊ,)Ṃ      Main link. Arg: points
                        e.g. [[1,4],[3,2],[3,1]]
«/                      Find minimums by coordinate
                        e.g. [1,1]
   »/                   Find maximums by coordinate
                        e.g. [3,4]
  r                     Inclusive ranges by coordinate
                        e.g. [[1,2,3],[1,2,3,4]]
     Œp                 Cartesian product of the x and y ranges
                        e.g. [[1,1],[1,2],[1,3],[1,4],...,[3,4]]
       µ                    Chain, arg: center
                            e.g. [1,3]
        ³                   Get the original points
                            e.g. [[1,4],[3,2],[3,1]]
         _                  Subtract the center from each
                            e.g. [[0,1],[2,-1],[2,-2]]
          ²                 Square each number
                            e.g. [[0,1],[4,1],[4,4]]
           §                Sum each sublist
                            e.g. [1,5,8]
            ½               Square root of each number
                            e.g. [1,2.24,2.83]
             Ṁ              Find the maximum
                            e.g. 2.83
              Ċ             Round up
                            e.g. 3
               ,            Pair with the center point
                            e.g. [3,[1,3]]
                )       Do the above for all points
                        e.g. [[3,[1,1]],[3,[1,2]],[3,[1,3]],...,[3,[3,4]]]
                 Ṃ      Find the lexicographically smallest pair
                        e.g. [3,[1,1]]

PurkkaKoodari

Posted 2018-11-06T16:39:42.087

Reputation: 16 699

@Dennis Thanks! Since when did Jelly's vectorization repeat the shorter list, or am I misinterpreting the removal of ? – PurkkaKoodari – 2018-11-06T18:33:04.440

Depths are matched first. If you a pair and an array of pairs, the pair gets matched with all pairs. – Dennis – 2018-11-06T18:34:40.200

8

Brachylog v2, 19 bytes

;Az{\-ᵐ~√ᵐ+}ᵐ≤ᵛ√;A≜

Try it online!

This program was easy to write ­– Brachylog's almost perfect for this sort of problem – but hard to golf. It wouldn't surprise me if there was a byte saving somewhere here, as few things I did seemed to have any effect (and it contains nested map instructions, normally a sign that you should be using member/findall, but I can't see a way to do it).

This is a function submission. Input is from the left argument to the function in the format [[x,y],[x,y],…], output from the right argument in the form [r,[[x,y]]]. (If you want to try negative numbers in the input, note that Brachylog uses _ for the minus sign, not -. This is confusing because the function → full program wrapper that Brachylog ships with, requested using the command-line argument Z, will present negative numbers in the output with a regular minus sign.)

Explanation

;Az{\-ᵐ~√ᵐ+}ᵐ≤ᵛ√;A≜
;A                   Append something
  z                    to every element of the input
   {       }ᵐ        such that for each resulting element:
     -                 Subtracting
    \ ᵐ                  corresponding elements {of the (input, appended) element}
       ~√              and un-squarerooting
         ᵐ               {the result of} each {subtraction}
          +            and summing {the resulting square numbers}
             ≤       {lets us find} a number at least as large as
              ᵛ        every element {of the list of sums}
               √     which can be square-rooted;
                ;A   append the same list as initially to it.
                  ≜  Find the first integer solution to the above, lexicographically.

This is interesting in that we're asking Brachylog to find a value of certain properties (in this case, the radius of a disk centred at point A that fits all the input points), but hardly placing any requirements on it (all we require is that the radius is a number). However, Brachylog will internally calculate the radius in question symbolically rather than trying to use concrete numbers, so when the final is reached, it accomplishes two things at once: first, it ensures that only integers are used for the coordinates of A and for the radius (forcing the squared radius to be a square number, and explaining the use of ≤ᵛ to find a "maximum or greater"); second, it finds the smallest possible viable radius (as the radius comes first in the output).

One thing that isn't specified in the program at all is that all the points are measured against the same centre of a disk; as written, there are no constraints that we don't use a different centre for each point. However, the tiebreak order (which in this case is set by the third , and which as a structure constraint will be evaluated before the value constraint implied by ) wants A to be as short as possible (i.e. a single element, so we use the same centre each time; it tries a zero-length A first but that obviously doesn't work, so it tries a singleton list next). As a result, we end up getting a useful constraint (that we only have one disk) "for free".

This solution happens to generalise to any number of dimensions, with no changes to the source code; there are no assumptions here that things are two-dimensional. So if you happen to need the smallest integer sphere, you can have that too.

ais523

Posted 2018-11-06T16:39:42.087

Reputation: 11

3

Perl 6, 81 bytes

{min [X]([Z]($^p)>>.minmax).map:{$p.map({(@_ Z-$_)>>².sum**.5}).max.ceiling,$_}}

Try it online!

Takes a list of points as 2-element lists ((X1, Y1), (X2, Y2), ...). Returns a list (R, (X, Y)). Uses the same approach as Pietu1998's Jelly answer:

[X]([Z]($^p)>>.minmax)  # All possible centers within the bounding box
.map:{ ... }            # mapped to
$p.map({(@_ Z-$_)>>².sum**.5}).max  # maximum distance from any point
.ceiling                # rounded up,
,$_                     # paired with center.
min                     # Find minimum by distance.

The minmax method is useful here as it returns a Range. The Cartesian product of ranges directly yields all points with integer coordinates.

nwellnhof

Posted 2018-11-06T16:39:42.087

Reputation: 10 037

2

05AB1E, 26 bytes

øεWsàŸ}`âεUIεX-nOt}àîX‚}{н

Port of @Pietu1998's Jelly answer.

Try it online or verify all test cases.

Explanation:

ø                    # Zip the (implicit) input, swapping the rows and column
                     #  i.e. [[1,4],[3,2],[3,1]] → [[1,3,3],[4,2,1]]
 ε    }              # Map each to:
  W                  #  Push the smallest value (without popping the list)
                     #   i.e. [[1,3,3],[4,2,1]] → [1,1]
   s                 #  Swap so the list is at the top of the stack again
    à                #  Pop the list and push the largest value
                     #   i.e. [[1,3,3],[4,2,1]] → [3,4]
     Ÿ               #  Take the inclusive range of the min and max
                     #   i.e. [[1,2,3],[1,2,3,4]]
`                    # After the map, push both lists separated to the stack
 â                   # And take the cartesian product of the two lists
                     #  i.e. [[1,2,3],[1,2,3,4]]
                     #   → [[1,1],[1,2],[1,3],[1,4],[2,1],[2,2],[2,3],[2,4],[3,1],[3,2],[3,3],[3,4]]
  ε             }    # Map each pair to:
   U                 #  Pop and store the current value in variable `X`
    I                #  Push the input
     ε     }         #  Map each pair in the input to:
      X              #   Push variable `X`
       -             #   Subtract it from the current pair
                     #    i.e. [3,2] - [1,3] → [2,-1]
        n            #   Take the square of each
                     #    i.e. [2,-1] → [4,1]
         O           #   Sum the lists
                     #    i.e. [4,1] → 5
          t          #   Take the square-root of each
                     #    i.e. 5 → 2.23606797749979
            à        #  Pop the converted list, and push its largest value
                     #   i.e. [[3.0,2.23606797749979,2.0],[2.0,2.0,2.23606797749979],...,[2.0,2.0,3.0]]
                     #    → [3.0,2.23606797749979,...,3.0]
             î       #  Round it up
                     #   i.e. [3.0,2.23606797749979,...,3.0] → [3.0,3.0,3.0,4.0,4.0,3.0,3.0,4.0,4.0,3.0,3.0,3.0]
              X‚     #  Pair it with variable `X`
                     #   i.e. [[3.0,[1,1]],[3.0,[1,2]],...,[3.0,[3,4]]]
                 {   # After the map, sort the list
                  н  # And take the first item (which is output implicitly)
                     #  i.e. [[3.0,[1,1]],[3.0,[1,2]],...,[3.0,[3,4]]] → [3.0,[1,1]]

Kevin Cruijssen

Posted 2018-11-06T16:39:42.087

Reputation: 67 575

2

Matlab, 73 bytes

function g(x);[r,~,d]=fminimax(@(a)pdist2(x,a),[0 0]);[round(r) ceil(d)]

Simply find the smallest solution (floating point) and round to nearest point and ceil the radius (worst case for the minimax problem). I don't know for sure if that yields the correct solution for all possible cases (within the precision), but for the test cases it should work (if I didn't make a typing error).

Call it with

g([1 4;3 2;4 1;4 5;5 2;7 4])

Jonas

Posted 2018-11-06T16:39:42.087

Reputation: 177

wouldn't this fail for $(0,0),(1,1)$? the obvious solution returned by fminimax is the midpoint $(0.5,0.5)$, which MATLAB rounds to $(1,1)$. The radius returned is also $\sqrt 2 / 2$ which rounds up to $1$, but that then excludes $(0,0)$.

– Giuseppe – 2018-11-07T20:45:16.547

You are correct, but the output of fminimax is [0.500000211699422 0.499999788525202], so it rounds correctly. So I'm lucky here. I'm currently thinking how to sidestep this problem (as it only work by pure luck). – Jonas – 2018-11-08T21:20:13.733

2

Java 10, 283 279 277 257 bytes

C->{int M=1<<31,m=M,X=M,Y=M,x=M-1,y=x,t,a,b,r[]={x};for(var c:C){x=(t=c[0])<x?t:x;X=t>X?t:X;y=(t=c[1])<y?t:y;Y=t>Y?t:Y;}for(;y<=Y;y++)for(t=x;t<=X;r=m<r[0]?new int[]{m,t,y}:r,m=M,t++)for(var c:C){a=c[0]-t;b=c[1]-y;a*=a;m=(a=(int)Math.ceil(Math.sqrt(a+=b*=b)))>m?a:m;}return r;}

-20 bytes thanks to @nwellnhof's tip of using Math.hypot.

The result-array is in the order [R,X,Y].

Try it online.

Explanation:

C->{                  // Method with 2D int-array as parameter & int-array as return-type
  int M=1<<31,        //  Minimum `M`, starting at -2,147,483,648
      m=M,            //  Temp integer, starting at -2,147,483,648 as well
      X=M,            //  Largest X coordinate, starting at -2,147,483,648 as well
      Y=M,            //  Largest Y coordinate, starting at -2,147,483,648 as well
      x=M-1,          //  Smallest X coordinate, starting at 2,147,483,647
      y=x,            //  Smallest Y coordinate, starting at 2,147,483,647 as well
      t,a,            //  Temp integers, starting uninitialized
      r[]={x};        //  Result-array, starting at one 2,147,483,647
  for(var c:C){       //  Loop over the input-coordinates
    x=(t=c[0])<x?t:x; //   If the X coordinate is smaller than `x`, change it
    X=t>X?t:X;        //   If the X coordinate is larger than `X`, change it
    y=(t=c[1])<y?t:y; //   If the Y coordinate is smaller than `y`, change it
    Y=t>Y?t:Y;}       //   If the Y coordinate is larger than `Y`, change it
 for(;y<=Y;y++)       //  Loop `y` in the range [`y`,`Y`]:
   for(t=x;t<=X       //   Inner loop `t` in the range [`x`,`X`]:
          ;           //     After every iteration:
           r=m<r[0]?  //      If `m` is smaller than the first value:
              new int[]{m,t,y}
                      //       Replace the result with `m,t,y`
             :        //      Else:
              r,      //       Leave `r` unchanged
           m=M,       //      Reset `m` to -2,147,483,648 for the next iteration
           t++)       //      And increase `t` by 1
     for(var c:C)     //    Inner loop over the input-coordinates
       m=(a=(int)Math.ceil(Math.hypot(c[0]-t,c[1]-y)))
                      //     Subtract `t` from the X coordinate;
                      //     subtract `y` from the Y coordinate;
                      //     take the hypot (<- sqrt(x*x+y*y)) of those
                      //     ceil it
                      //     And set `a` to this value
          >m?         //     If `a` is larger than `m`:
           a          //      Set `m` to `a`
          :           //     Else:
           m;         //      Leave `m` unchanged
  return r;}          //  Return the result `r`

Kevin Cruijssen

Posted 2018-11-06T16:39:42.087

Reputation: 67 575

1You can save at least 8 bytes with Math.hypot. – nwellnhof – 2018-11-07T19:44:50.723

@nwellnhof Ah, completely forgot about Math.hypot, which is perfect for this challenge! -20 bytes right there. Thanks. :) – Kevin Cruijssen – 2018-11-07T20:27:19.453

2

Pyth, 34 33 bytes

hSm+.EeSm@s^R2-Vdk2Qd*Fm}FhM_BSdC

Output is in the form [R,x,y]

Try it online here, or verify all the test cases at once here.

hSm+.EeSm@s^R2-Vdk2Qd*Fm}FhM_BSdCQ   Implicit: Q=eval(input())
                                     Trailing Q inferred
                                CQ   Transpose (group x and y coordinates separately)
                       m             Map each in the above, as d, using:
                              Sd       Sort d
                            _B         Pair with its own reverse
                          hM           Take the first element of each, yielding [min, max]
                        }F             Generate inclusive range
                     *F              Cartesian product of the above two lists, yielding all coordinates in range
  m                                  Map each coordinate in the above, as d, using:
        m          Q                   Map each coordinate in input, as k, using:
              -Vdk                       Take the difference between x and y coordinates in d and k
           ^R2                           Square each
          s                              Sum
         @        2                      Take the square root
      eS                               Take the largest of the result
    .E                                 Rounded up
   +                d                  Prepend to d
 S                                   Sort the result, first element as most significant
h                                    Take first element

Edit: Saved a byte by rearranging output format, previous version:

heDm+d.EeSm@s^R2-Vdk2Q*Fm}FhM_BSdC

Sok

Posted 2018-11-06T16:39:42.087

Reputation: 5 592

2

Wolfram Language (Mathematica), 66 bytes

Here's a brute force approach. I considered the much shorter BoundingRegion[#,"MinDisk"]& function but there is no way to force integer coordinates & radius.

Minimize[{r,RegionWithin[{x,y}~Disk~r,Point@#]},{x,y,r},Integers]&

Try it online!

Kelly Lowder

Posted 2018-11-06T16:39:42.087

Reputation: 3 225

Nice. But, how about {Round@#[[1]], Ceiling@#[[2]]} &@BoundingRegion[#, "MinDisk"]&? – DavidC – 2018-11-07T19:55:06.613

@DavidC, the rounding of the center might move it by up to Sqrt[2]/2=.707 but taking the ceiling won't necessarily add enough length to the radius to counteract that. I think an example of that failing would be just the two points {{0,0},{11,11}} – Kelly Lowder – 2018-11-07T20:27:58.797

I see what you mean! – DavidC – 2018-11-07T23:19:04.307

1

Javascript, 245 bytes

a=>{[b,c,d,e]=a.reduce(([j,k,l,m],[h,i])=>[j>h?j:h,k<h?k:h,l>i?l:i,m<i?m:i],[,,,,]);for(f=c;f<b;f++){for(g=e;g<d;g++){s=a.reduce((o,[p,q])=>o>(r=(p-f)**2+(q-g)**2)?o:r);n=n?n[2]>s?[f,g,s]:n:[f,g,s]}}return [n[0],n[1],Math.ceil(Math.sqrt(n[2]))]}

(Somewhat) more readable version:

a=>{
    [b,c,d,e]=a.reduce(([j,k,l,m],[h,i])=>[j>h?j:h,k<h?k:h,l>i?l:i,m<i?m:i],[,,,,]);
    for(f=c;f<b;f++){
        for(g=e;g<d;g++){
            s=a.reduce((o,[p,q])=>o>(r=(p-f)**2+(q-g)**2)?o:r);
            n=n?n[2]>s?[f,g,s]:n:[f,g,s]
        }
    }
    return [n[0],n[1],Math.ceil(Math.sqrt(n[2]))]
}

Just finds the bounding box, and tests each coordinate in that box for whether it's the best.

I could save 8 bytes with an approximate answer, by replacing:

Math.ceil(Math.sqrt(n[2])) with ~~(n[2]+1-1e-9)

Spitemaster

Posted 2018-11-06T16:39:42.087

Reputation: 695

There are for sure more things to golf, but JS isn't my strong suite. Still, you can golf for(f=c;f<b;f++){for(g=e;g<d;g++){s=a.reduce((o,[p,q])=>o>(r=(p-f)**2+(q-g)**2)?o:r);n=n?n[2]>s?[f,g,s]:n:[f,g,s]}} to for(f=c;f<b;f++)for(g=e;g<d;n=n?n[2]>s?[f,g,s]:n:[f,g,s],g++)s=a.reduce((o,[p,q])=>o>(r=(p-f)**2+(q-g)**2)?o:r);. And I'm pretty sure you can remove the space at return[. – Kevin Cruijssen – 2018-11-06T21:59:42.263

1You can probably save a few bytes using Math.hypot. – nwellnhof – 2018-11-07T19:48:51.553

1

Ruby, 113 bytes

->l{a=l.flatten;(z=*a.min..a.max).product(z).map{|x,y|[l.map{|u,v|(((x-u)**2+(y-v)**2)**0.5).ceil}.max,x,y]}.min}

Try it online!

G B

Posted 2018-11-06T16:39:42.087

Reputation: 11 099

1

Charcoal, 65 bytes

≔Eθ§ι¹ζ≔Eθ§ι⁰ηF…·⌊η⌈ηF…·⌊ζ⌈ζ⊞υ⟦ικ⟧≔Eυ⌈EθΣEιX⁻§λξν²ηI§υ⌕η⌊ηI⌈X⌊η·⁵

Try it online! Link is to verbose version of code. Explanation:

≔Eθ§ι¹ζ

Get the y-coordinates into z.

≔Eθ§ι⁰η

Get the x-coordinates into h.

F…·⌊η⌈ηF…·⌊ζ⌈ζ⊞υ⟦ικ⟧

Loop over the inclusive ranges from the minimums to the maximums of h and z generating the list of all potential disc centres.

≔Eυ⌈EθΣEιX⁻§λξν²η

Loop over all the disc centres, then loop over all of the original points, then loop over both coordinates, subtract, square, sum, take the maximum, and save the resulting list.

I§υ⌕η⌊η

Find the position of the minimal maximum diameter and print the corresponding disc centre.

I⌈X⌊η·⁵

Print the minimal maximum diameter, but round it up to the next integer.

Neil

Posted 2018-11-06T16:39:42.087

Reputation: 95 035