根据坐标确定矩形的算法

发布于 2024-09-30 14:30:59 字数 689 浏览 0 评论 0原文

我有由绘制的矩形(自由式)组成的用户输入。现在这个绘制的图形并不完美,所以我想根据算法重新绘制它们的形状。

我有一堆来自用户绘图的坐标。我想找到最大的 (x,y) 和最小的 (x,y) 坐标,并使用它们之间的距离来确定矩形的对角线。

但我很难确定最大 (x,y) 坐标和最小 (x,y) 坐标。

例如,我不能将最大的 y 与最大的 x 或最大的 x 与最大的 y 一起使用,因为也许用户只是在他们的行中意外地突出了。 (这有意义吗?)

假设下面是用户绘制的线..如果我使用最大的 y 和最大的 x,我将不会得到所需的坐标(因为它会在意外突出的地方找到坐标)

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

希望你理解 我想表达

的另一种方式是,我想要最接近 (0,0) 的坐标,如果我的画布是 1000 x 1000,我希望第二个坐标最接近 (1000 ,1000)。 (两个极端坐标)

任何人都可以帮助解决这个算法吗?

提前致谢!

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.

But I'm having difficulty determining the greatest (x,y) coordinate and the lowest (x,y) coordinate.

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. (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)

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

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). (the two extreme coordinates)

Can anyone help with this algorithm?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

樱花落人离去 2024-10-07 14:30:59

根据您希望算法生成的矩形适应用户输入的程度,您可以尝试以下操作:

  1. 对所有 x 和 y 坐标求平均值,得到矩形的中心 (Xc, Yc)。
  2. 找到最高和最低的 x 值,用最高值减去最低值,然后除以二。对 y 值重复此操作。我们将这些称为 X 和 Y(s 代表“侧面”)。
  3. 重要的角点(左上角和右下角)将变为 (Xc - Xs, Yc - Ys) 和 (Xc + Xs, Yc + Ys)。
  4. 适当地画线。

现在,这将为您提供一个边界框,其中包含所有用户给定的点。如果您正在寻找更多的最佳拟合类型算法,请将第二步中的 (max - min) / 2 函数替换为平均函数。一个简单的方法可能涉及仅对中心点一侧的点(上方/下方或左/右)进行平均,并将它们用作距中心的偏移量。请注意,这将为您提供四个偏移量,但在任何给定时间您只会使用其中两个。

这里提出的粗略想法可以根据您期望的用户输入类型(例如,您期望它可能有多扭曲)进行调整。假设您能够通过点本身或用户输入方法来区分边(例如,使用离散操作而不是一次绘制矩形的每条边),可以使用线性回归线进行进一步的改进。

希望这个简单的例子能为您指明正确的方向。

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).
  2. Find your highest and lowest x value, subtract the lowest from the highest and divide by two. Repeat for the y values. Let us call these Xs and Ys (s is for 'side').
  3. The important corners (upper left and lower right) would then become (Xc - Xs, Yc - Ys) and (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. 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 (e.g. 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.

天涯沦落人 2024-10-07 14:30:59

如果你想找到距离(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;
}

您可以轻松修改算法以找到最接近屏幕边缘其余部分的点。

if you want to find the closest point to (0,0), then just find it!

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.

挽手叙旧 2024-10-07 14:30:59

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

Just do an average over all points and use it as the position of rectangle sides.. of course this assumes you are able to distinguish the four sides of the rectangle otherwise you could try a way to split the coordinates into 4 sides (by checking the horizontal and vertical variation with some threshold) and then compute the average for each side and adjust it to link sides.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文