简体   繁体   English

算法从坐标确定矩形

[英]algorithm to determine a rectangle from coordinates

I have user input that consists of a drawn rectangle (freestyle). 我有用户输入,包含一个绘制的矩形(自由泳)。 Now this drawn figure isn't perfect, so I would like to redraw the shape for them based on an algorithm. 现在这个绘制的图形并不完美,所以我想基于算法重绘它们的形状。

I have a bunch of coordinates from the user's drawing. 我有一堆来自用户绘图的坐标。 I would like to find the greatest (x,y) and the lowest (x,y) coordinates and use the distance between those to determine the diagonal of the rectangle. 我想找到最大的(x,y)和最低(x,y)坐标,并使用它们之间的距离来确定矩形的对角线。

But I'm having difficulty determining the greatest (x,y) coordinate and the lowest (x,y) coordinate. 但是我很难确定最大(x,y)坐标和最低(x,y)坐标。

I can't take the greatest y with the greatest x, or the greatest x with the greatest y for example because maybe the user just made an accidental jut out in their line. 我不能用最大的x取最大的y,或者用最大的y取最大的x,例如因为用户可能只是在他们的行中突然出现了。 (Does that make sense?) (那有意义吗?)

Pretend below is a user drawn line.. if i used the greatest y with the greatest x, I would not have the desired coordinate (because it would find the coordinate in the accidental jut out) 假设下面是用户绘制的线..如果我使用最大的y和最大的x,我将没有所需的坐标(因为它会在意外突出中找到坐标)

                       ----
                      /    \
                 ----/      \--------                 -----        --
  --------------/                    \---------------/     \------/  \--

Hope you understand what I'm getting at.. 希望你能理解我的目标......

I guess another way of putting it is that I would like the coordinate closest to (0,0) and if my canvas was 1000 x 1000, I would like the second coordinate to be closest to (1000,1000). 我想另一种方法是我希望坐标最接近(0,0),如果我的画布是1000 x 1000,我希望第二个坐标最接近(1000,1000)。 (the two extreme coordinates) (两个极端坐标)

Can anyone help with this algorithm? 任何人都可以帮助这个算法吗?

Thanks in advance! 提前致谢!

Depending on how well you want the algorithm-generated rectangle to fit the user input, you might try the following: 根据您希望算法生成的矩形适合用户输入的程度,您可以尝试以下操作:

  1. Average all x and y coordinates to give you the center of your rectangle, (Xc, Yc). 平均所有x和y坐标,为您提供矩形的中心,(Xc,Yc)。
  2. Find your highest and lowest x value, subtract the lowest from the highest and divide by two. 找到最高和最低x值,从最高值中减去最低值并除以2。 Repeat for the y values. 重复y值。 Let us call these Xs and Ys (s is for 'side'). 让我们称这些X和Y(s代表'侧')。
  3. The important corners (upper left and lower right) would then become (Xc - Xs, Yc - Ys) and (Xc + Xs, Yc + Ys). 然后重要的角(左上和右下)将变为(Xc-Xs,Yc-Ys)和(Xc + Xs,Yc + Ys)。
  4. Draw lines as appropriate. 适当地画线。

Now, this will give you a bounding-box wherein all user given points are contained. 现在,这将为您提供一个包含所有用户给定点的边界框。 If you are looking for more of a best-fit type algorithm, replace the (max - min) / 2 function in step two with an averaging function. 如果您正在寻找更多最佳拟合类型算法,请使用平均功能替换第二步中的(max - min)/ 2函数。 A simple one might involve averaging only points to one side of the center point (either above / below or left / right) and using those as offsets from center. 一个简单的方法可能只涉及平均点到中心点的一侧(上/下或左/右),并使用这些点作为中心偏移。 Note that this will give you four offsets, only two of which you will use at any given time. 请注意,这将为您提供四个偏移,其中只有两个将在任何给定时间使用。

The rough idea presented here can be tuned to taste, depending on what kind of user input you are expecting (eg how distorted you expect it might be). 这里提出的粗略想法可以根据您期望的用户输入类型进行调整(例如,您期望的扭曲程度如何)。 Further improvements can be made using linear regression lines, assuming you are able to distinguish sides either via the points themselves or by user input methods (ex. drawing each side of the rectangle with a discrete action rather than all at once). 使用线性回归线可以进一步改进,假设您能够通过点本身或用户输入方法区分边(例如,使用离散动作绘制矩形的每一边而不是一次性绘制)。

Hope this quick example will point you in the right direction. 希望这个简单的例子能指出你正确的方向。

if you want to find the closest point to (0,0), then just find it! 如果你想找到最接近(0,0)的点,那就找吧!

point FindClosestToOrigin(point[] P)
{
  point closest = P[0];
  foreach(point p in P)
  {
      if (DistanceOriginS(p) < DistanceOriginS(closest)) closest = p;
  }
  return closest;
}
float DistanceOriginS(point p)
{
   return p.x*p.x + p.y*p.y;
}

you can easily modify the algo to find points closest to the rest of screen edges. 您可以轻松修改算法以找到最接近屏幕边缘其余部分的点。

只需对所有点做一个平均值并将其用作矩形边的位置..当然这假设您能够区分矩形的四边,否则您可以尝试将坐标分成4边(通过检查水平和垂直变化与一些阈值),然后计算每一侧的平均值,并将其调整为链接边。

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

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