循环遍历矩形上两点之间的矩形边界?

发布于 2024-11-04 17:11:10 字数 336 浏览 0 评论 0原文

哇,那是一件大事。
图表
是的,所以我有两个点,在矩形的边界上,也在从原点投射的两条线上。为了简单起见,P1/P2的排列是任意的。

我的问题是,如何循环遍历矩形的绿色区域(基本上是最小的区域)?

实现:我想在我正在玩的游戏中创建视野效果创造。原点是玩家,可以位于当前视口(矩形)上的任何位置。线的方向偏离玩家面对的方向。我打算从原点追踪绿色区域上的所有位置,检查是否有障碍物。

我更喜欢代码示例答案,任何语言,但最好是 C#

Whew, that was a big one.
Diagram
Right, so I have two points, on the boundary of the rectangle and also on the two lines, which are cast from the origin. The arrangement of P1/P2 is arbitrary, for the sake of simplicity.

My question is, how can I loop through the green area (The smallest area, basically) of the rectangle?

The implementation: I want to create a field-of-vision effect in a game that I am creating. The origin is the player, who can be anywhere on the current viewport (The rectangle). The lines' directions are offset from the direction that the player is facing. I intend to trace all positions on the green area from the origin, checking for obstructions.

I would prefer a code example answer, any language, but preferably C#

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

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

发布评论

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

评论(3

雨后咖啡店 2024-11-11 17:11:10

我认为,您想要的是这样的:

具有以下条件的所有坐标 C 都位于矩形 R 的一部分的绿线上:

(P1.y == P2.y)
?
(
    C.x >= P1.x && C.x <= P2.x
)
:
(
  (C.x >= P2.x && C.x <= R.right && C.y == P2.y) || 
  (C.x >= P1.x && C.x <= R.right && C.y == P1.y) || 
  (C.x == R.x && C.y <= P1.y && C.y >= P2.y)
)

这假设 P1 将低于 P2,以防它们不在同一直线上,并且如果 P1 位于同一行,则 P1 将位于 P2 之前。

I think, what you want is something like this:

All coordinates C with the following criteria are on the green line that is part of the rectangle R:

(P1.y == P2.y)
?
(
    C.x >= P1.x && C.x <= P2.x
)
:
(
  (C.x >= P2.x && C.x <= R.right && C.y == P2.y) || 
  (C.x >= P1.x && C.x <= R.right && C.y == P1.y) || 
  (C.x == R.x && C.y <= P1.y && C.y >= P2.y)
)

This assumes that P1 will be below P2 in case they are not on the same line and that P1 will be before P2 if they are on the same line.

白昼 2024-11-11 17:11:10

如果你想找出哪些对象在“绿色区域”...

假设你知道矩形的大小,你可以计算你感兴趣的多边形的顶点(原点、P1、P2 和可见的矩形角) )然后您可以循环遍历对象以使用多边形检测中的点查找内部对象。

以伦道夫·富兰克林为例。对于内部点返回 1,对于外部点返回 0...

int pnpoly(int npol, float *xp, float *yp, float x, float y)
{
  int i, j, c = 0;
  for (i = 0, j = npol-1; i < npol; j = i++) {
    if ((((yp[i] <= y) && (y < yp[j])) ||
         ((yp[j] <= y) && (y < yp[i]))) &&
        (x < (xp[j] - xp[i]) * (y - yp[i]) / (yp[j] - yp[i]) + xp[i]))
      c = !c;
  }
  return c;
}

If you want to find out which objects are in the "green area"...

Assuming you know the size of the rectangle you can calculate the vertices of the polygon you're interested in (origin, P1, P2 and the visible rectangle corners) then you can loop through your objects to find which are inside using point in polygon detection.

Randolph Franklyn's for example. Returns 1 for interior points and 0 for exterior points...

int pnpoly(int npol, float *xp, float *yp, float x, float y)
{
  int i, j, c = 0;
  for (i = 0, j = npol-1; i < npol; j = i++) {
    if ((((yp[i] <= y) && (y < yp[j])) ||
         ((yp[j] <= y) && (y < yp[i]))) &&
        (x < (xp[j] - xp[i]) * (y - yp[i]) / (yp[j] - yp[i]) + xp[i]))
      c = !c;
  }
  return c;
}
无远思近则忧 2024-11-11 17:11:10

我想象做你想要的事情(视野)的最简单方法是首先将所有内容绘制到屏幕上,然后在你不希望看到的地方绘制黑色。

所以基本上是在没有战争迷雾的屏幕上绘制黑暗。

I'd imagine the simplest way to do what you want(field of view) would be to first draw everything to screen, then draw black everywhere you don't want things to be seen.

So basically drawing darkness onto the no fog-of-war screen.

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