Smallest distance between a point and a rectangle - MATLAB Cody - MATLAB Central

Problem 193. Smallest distance between a point and a rectangle

Difficulty:Rate

Given two points x and y placed at opposite corners of a rectangle, find the minimal euclidean distance between another point z and every point within this rectangle.

For example, the two points

     x = [-1,-1];
     y = [1,1];

define a square centered at the origin. The distance between the point

   z = [4,5];

and this square is

   d = 5;

(the closest point in the square is at [1,1])

The distance between the point z = [0,2] and this same square is d = 1 (closest point at [0,1])

The distance between the point z = [0,0] and this same square is d = 0 (inside the square)

Notes:

  • you can always assume that x < y (element-wise)
  • The function should work for points x,y,z in an arbitrary n-dimensional space (with n>1)

Solution Stats

40.24% Correct | 59.76% Incorrect
Last Solution submitted on Feb 21, 2025

Problem Comments

Solution Comments

Show comments
PIVlab surpasses 100K all-time File Exchange downloads
During the past twelve months, PIVlab, a MATLAB Community Toolbox for particle...
4
8

Problem Recent Solvers271

Suggested Problems

More from this Author38

Problem Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!