简体   繁体   English

如何找到给定点的最近正方形

[英]How to find the closest square of the given point

I have a 2-D array of squares (square shape), Each square has 50 units length and x, y co-ordinates.我有一个二维正方形数组(正方形),每个正方形有 50 个单位长度和 x、y 坐标。 The distance between the squares is 5 units.正方形之间的距离为 5 个单位。 The x, y co-ordinates are the bottom left corner of the square. x, y 坐标是正方形的左下角。 Now, given any point(x,y co-ordinates) how can i find the closest square to this point.现在,给定任何点(x,y 坐标),我如何找到最接近该点的正方形。

square **sq = new square*[10];
for(int i=0;i<10;++i){
    sq[i]=new square[10];
}

int m=0, n=0;
for(int i=0;i<10;++i){
    m=0;
    for(int j=0;j<10;++j){
        sq[i][j].setCoOrdinates(m,n);
        m+=55;
    }
    n+=55;
}

// Given a point (x, y) how can i find the index (i, j) of closest square to this point. // 给定一个点 (x, y) 我如何找到离该点最近的正方形的索引 (i, j)。

Let's rigorously define the definition of distance from a square to a point: "The minimum of all lengths from the point (x,y) to some point within the rectangle".让我们严格定义从正方形到点的距离:“从点 (x,y) 到矩形内某个点的所有长度的最小值”。 This gives some clear rules for defining the distance to a rectangle.这为定义到矩形的距离提供了一些明确的规则。 For any rectangle, if the point(x,y) is directly above, below, to the left of, or to the right of the sides of the rectangle, the minimum distance to the rectangle is the straight line drawn either vertically or horizontally through the point.对于任何矩形,如果 point(x,y) 直接位于矩形边的上方、下方、左侧或右侧,则到矩形的最小距离是垂直或水平绘制的直线点。 For instance, if your point is (40, 90) and your rectangle's bottom-left is (0,0) (and it is a 50x50 square), you can draw a vertical line through your point and the distance is min(90-(0+50), 90-0)例如,如果您的点是 (40, 90) 并且矩形的左下角是 (0,0) (并且它是一个 50x50 的正方形),您可以通过您的点画一条垂直线,距离是 min(90- (0+50), 90-0)

If the point is not directly above, below, left of, or right of the sides of the square, then the closest distance is to the closest of the four corners.如果该点不在正方形边的正上方、下方、左侧或右侧,则最接近的距离是到四个角中最近的一个。 You can simply take the min of all the distances to the four corners, which can be found by using the distance formula.您可以简单地取四个角的所有距离的最小值,可以使用距离公式找到。

Simply apply this logic to each of your squares in your array and you should be good to go.只需将此逻辑应用于数组中的每个方格,您就可以使用 go。 Time O(NM) where n is the number of rows of squares and M is the number of columns of squares, In other words.时间 O(NM) 其中 n 是正方形的行数,M 是正方形的列数,换句话说。 O(number of squares you have). O(您拥有的方格数)。 Space O(1).空间 O(1)。

Lets start with the simpler problem: All rectangles are 55 units wide, ie there is no empty space between them...让我们从更简单的问题开始:所有矩形都是 55 个单位宽,即它们之间没有空白空间......

The best container is the one that you do not need.最好的容器是您不需要的容器。 There is a simple relation between i and j index of a square in the array and its m and n coordinates:数组中正方形的ij索引与其mn坐标之间有一个简单的关系:

const int distance = 55;
int m(int i) { return i*distance; }
int n(int j) { return m(j); }

As the relation is linear, you can invert it:由于关系是线性的,您可以将其反转:

int i(int m) { return m / distance; }
int j(int n) { return i(n); }

Using integer arithmetics is fine here, because we get the same result as if we had used floating points and then rounded down.在这里使用 integer 算术很好,因为我们得到的结果与使用浮点数然后向下舍入一样。

This is already the full solution for the simpler problem.这已经是更简单问题的完整解决方案。 Given coordinats m and n the closest square is at index i(m),j(n) .给定坐标mn ,最近的正方形在索引i(m),j(n)处。

Now lets introduce the gap:现在让我们介绍一下差距:

cosnt int width = 50;
const int gap = 5;

Now we have to distinguish two possibilities: The given coordinats are inside a square or outside.现在我们必须区分两种可能性:给定的坐标在正方形内部或外部。 When it is outside there are two candidates for the cloest square.当它在外面时,最近的广场有两个候选人。

int i_with_gap(int m) {
     int i = m / distance;

     // point is inside the square?
     if (m % distance <= width) return i;

     // point is closer to square at index i?
     if (m % distance <= width+ gap/2.0) return i;

     // otherwise i+1 is closer
     return i+1;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM