1

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

ChewyCarrot
  • 222
  • 2
  • 12
  • I dont think so. Polygon rect classes to exist in other frameworks, but not sdl and therefore pygame. – Mercury Platinum May 28 '18 at 16:41
  • I'm not sure if I understand your question, specifically what you mean by _"have only circles to be blit inside a polygon"._ Where and what you decide to blit is up to you. So if you decide you only want to blit your circles in places where you have blit polygons, I don't see what could cause problems. Or am I misunderstanding? – Ted Klein Bergman May 28 '18 at 17:48
  • I want circles to appear on the screen at random positions, but only appears within a polygon. – ChewyCarrot May 29 '18 at 02:34
  • Please add more details about the game and show us some code. How do you create the polygons? Maybe an image (or animated gif) would make the question clearer. – skrx May 29 '18 at 12:26
  • Like a map, I found an image online, but I can redraw it as a polygon easily. It is kind of like a virus simulator, but the viruses will not be in the water, I will somehow need to set restrictions for the dots to appear only within the countries. The red dots represent infected people. – ChewyCarrot May 30 '18 at 13:03

2 Answers2

2

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.

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?

user3757614
  • 1,776
  • 12
  • 10
0

Use matplotlib for that. 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")
Kalma
  • 1