简体   繁体   English

只能在多边形内部进行平移(Pygame)

[英]Things to be blit only inside a polygon (Pygame)

Is it possible to have red circles to only appear inside a polygon. 是否有可能使红色圆圈仅出现在多边形内部。 For a rectangle, you can determine the height and width. 对于矩形,可以确定高度和宽度。

I'm planning to make like a virus simulator, and that there are red circles that only appear only inside the countries. 我打算制作像病毒模拟器一样的程序,并且有仅在国家内部出现的红色圆圈。 But the countries aren't rectangles, but polygons/images. 但是国家/地区不是矩形,而是多边形/图像。

I was wondering if it was possible to have only circles to be blit inside a polygon or image. 我想知道是否有可能在多边形或图像内只画圆。 Thanks 谢谢

Let's say you have a map as an image, and you want to determine if a random point is on land or not. 假设您有一张地图作为图像,并且想要确定随机点是否在陆地上。 What you need for that is an image redrawn to have only black and white pixels. 为此,您需要将图像重绘为仅具有黑白像素。 You can then use Pygame's Surface.get_at() command to see what color the pixel there is, and make a decision based on that. 然后,您可以使用Pygame的Surface.get_at()命令查看像素的颜色,并据此做出决定。

Alternately, let's say you have a map as a bunch of polygons, and you want to determine if a random point is on land or not. 或者,假设您有一张地图,是一堆多边形,并且要确定随机点是否在陆地上。 The logic there is called ray tracing, and is explained better over here: How can I determine whether a 2D Point is within a Polygon? 逻辑被称为光线追踪,在此进行了更好的解释: 如何确定2D点是否在多边形内?

Use matplotlib for that. 为此使用matplotlib。 A very simple example (Give values to the vertices of the square): 一个非常简单的示例(将值赋予正方形的顶点):

  from matplotlib.path import Path

  verts = [
             left_bottom,
             left_top,
             right_top,
             right_bottom,
             (0, 0),
            ]

    codes = [
             Path.MOVETO,
             Path.LINETO,
             Path.LINETO,
             Path.LINETO,
             Path.CLOSEPOLY,
             ]

    path = Path(verts, codes)

   if path.contains_point((x, y)):
       print("True")

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

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