3
$\begingroup$
RegionNearest[Disk[{0, 0}, 10], {15, 3}]
RegionNearest[Disk[{0, 0}, 10], {0, 1.5}]

The RegionNearest function can find the nearest point from a region to a specified point ,But the results it returned was real numbers.

I want to find out the nearest integer coordinate of the region from point P.How can I do this?

$\endgroup$
1
  • 1
    $\begingroup$ Could use Minimize as: In[10]:= Minimize[{(x - 15)^2 + (y - 3)^2, x^2 + y^2 <= 100, Element[{x, y}, Integers]}, {x, y}] Out[10]= {34, {x -> 10, y -> 0}}. $\endgroup$ Commented Apr 25, 2020 at 18:40

1 Answer 1

6
$\begingroup$
pnts = RegionIntersection[Disk[{0, 0}, 10], 
   Point[Tuples[Range[Ceiling @ #, Floor @ #2] & @@@ RegionBounds @ Disk[{0, 0}, 10]]]];

RegionNearest[pnts, {15, 3}]

{10, 0}

RegionNearest[pnts, {0, 1.5}]

{0, 2}

Row[Graphics[{PointSize[Large], Red, Point @ #, 
     Blue, Point @ RegionNearest[Disk[{0, 0}, 10], #], Black, 
     AbsolutePointSize[10], {Point @ #, Text[#, Offset[{10, 0}, #], Left]} &@
      RegionNearest[pnts, #], 
     Opacity[.5, Green], Disk[{0, 0}, 10]}, 
    ImageSize -> 300, PlotRange -> {{-20, 20}, {-15, 15}}] & /@ 
  {{15, 3}, -{15, 3}, {0, 1.5}},
 Spacer[1]]

enter image description here

Alternatively, you can use Nearest:

nF = Nearest[Select[RegionMember[#]]@
      Tuples[Range[Ceiling@#, Floor@#2] & @@@ RegionBounds@#] & @ Disk[{0, 0}, 10]];

nF /@ {{15, 3}, {0, 1.5}}

{{{10, 0}},
{{0, 1}, {0, 2}}}

Finally, you can use Minimize:

Quiet @ Minimize[{Norm[{p, q} - #], 
   Element[{p, q}, Disk[{0, 0}, 10]]}, 
 {p, q}, 
 Integers] & /@ {{15, 3}, {0, 1.5}}

{{Sqrt[34], {p -> 10, q -> 0}},
{0.5, {p -> 0, q -> 1}}}

$\endgroup$

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.