PDA

View Full Version : Putting Objects all around the QGraphicsPixmapItem



FlyDoodle
17th February 2020, 16:09
Hello !!!

So i have an island which is a QGraphicsPixmapItem and it looks like this :

13340

And what i'm trying to do is putting rocks all around the island, but i don't know how to do that ( i know how to put them on the scene i just don't know how to position them around the island )... so i was wondering if there is any kind of a way in which i could do this or do i have to figure out an algorithm on how to go all around the island ?

d_stranz
17th February 2020, 16:33
I assume your "island" is actually a rectangular image that you display using a QGraphicsPixmapItem. So if you call any of the methods that return the boundaries (like boundingRect() or shape()) that's just going to return a rectangle. I am guessing that you have already discovered this.

If there is a clear distinction between "land" and "sea" in your island images, you could write some code to determine a QPainterPath that does represent the outline of the model. A generally simple way to do this is to start at one edge of the pixmap and move towards the center. When the pixel value changes from "sea" to "land", you know you have landed on shore and can then begin walking around the island. Your current pixel location is in the center of a 9-membered square (center plus up, down, left, right, and the diagonals between them). You know that behind you is sea and ahead of you is probably land, so go left and check the pixel "above" you. If it is still land, check the pixels around you to make sure you aren't marching further inland but are sticking to shore (one or more of the pixels must always be sea). Make a line segment from the first and current position, add it to the path, and move on. Eventually, if you keep moving left you will end up back where you started and your QPainterPath will represent the actual outline of the island. You can then map the pixel positions in the path to scene coordinates and place your rocks accurately.

If you wanted to, you could derive a IslandPixmapItem from QGraphicsPixmapItem and re-implement the shape() method to return this path. That would make it easier to do other things, like determine when your ship has run aground.