Problem 193. Smallest distance between a point and a rectangle
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
Problem Comments
-
3 Comments
For the n dimensional case it would be better to say that x and y lie on opposite vertices of the n-hypercuboid such that each edge is parallel to a coordinate axis.
Two points do not define a rectangle. This is especially true in 3D space. A correct answer to this question would be 0 or the point closest to the circle defined by the two points on the diameter, depending on the rectangle you chose to make. (Every rectangle formed from two points defining opposite corners makes a circle, and in 3D, a sphere). I genuinely do not know how you want me to handle the 3D cases.
I agree to Brandon's comment. In a 2D space, there should be infinite numbers of rectangles, which makes a circle. In a 3D space, the infinite numbers of rectangle makes a sphere.
Solution Comments
Show commentsGroup

2021 New Year Cody Competition
- 30 Problems
- 3 Finishers
- Make a random, non-repeating vector.
- Pizza!
- Triangle Numbers
- Generate a vector like 1,2,2,3,3,3,4,4,4,4
- Find nth maximum
- surrounded matrix
- Calculate the area of a triangle between three points
- Return the first and last characters of a character array
- Smallest distance between a point and a rectangle
- N-Queens Checker
- Minefield Sonar
- Get the length of a given vector
- Select every other element of a vector
- Create a vector
- Convert from Fahrenheit to Celsius
- Set the array elements whose value is 13 to 0
- Remove NaN ?
- Find matching string from a list of strings
- The sliding puzzle: 3D
- Draw 'E'
- Find the maximum number of decimal places in a set of numbers
- calculate the day of the year from a date string.
- Reverse the vector
- Matlab Basics - Convert a row vector to a column vector
- Times 2 - START HERE
- Which coins to give
- Fibonacci sequence
- Find max prime number
- Longest Divisor Run
- Perfect Square or not
Problem Recent Solvers271
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!