循环遍历矩形上两点之间的矩形边界?
哇,那是一件大事。
是的,所以我有两个点,在矩形的边界上,也在从原点投射的两条线上。为了简单起见,P1/P2的排列是任意的。
我的问题是,如何循环遍历矩形的绿色区域(基本上是最小的区域)?
实现:我想在我正在玩的游戏中创建视野效果创造。原点是玩家,可以位于当前视口(矩形)上的任何位置。线的方向偏离玩家面对的方向。我打算从原点追踪绿色区域上的所有位置,检查是否有障碍物。
我更喜欢代码示例答案,任何语言,但最好是 C#
Whew, that was a big one.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为,您想要的是这样的:
具有以下条件的所有坐标 C 都位于矩形 R 的一部分的绿线上:
这假设 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:
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.
如果你想找出哪些对象在“绿色区域”...
假设你知道矩形的大小,你可以计算你感兴趣的多边形的顶点(原点、P1、P2 和可见的矩形角) )然后您可以循环遍历对象以使用多边形检测中的点查找内部对象。
以伦道夫·富兰克林为例。对于内部点返回 1,对于外部点返回 0...
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...
我想象做你想要的事情(视野)的最简单方法是首先将所有内容绘制到屏幕上,然后在你不希望看到的地方绘制黑色。
所以基本上是在没有战争迷雾的屏幕上绘制黑暗。
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.