Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   python link picture. (http://www.programmingforums.org/showthread.php?t=14323)

pyngod Nov 2nd, 2007 10:32 AM

python link picture.
 
I am trying to code a jigsaw puzzle in python and c but i am unable to find a way to link the split images..... meaning that after the image is split i am unable to find a way that would link the image to the part of the image where it broke from the main and make it stick to the piece.

I am thinking of making a grid to place the piece in but then that would give the the place a way.... any help would be use full.

Sane Nov 2nd, 2007 11:20 AM

Re: python link picture.
 
You make a "grid", but don't show it. There are many way of accomplishing this. I think the easiest way to do this is, every time someone places a piece, just snap it to the closest co-ordinates, that is a multiple of the size of each piece.

E.G.
:

width = 20  # This is the width of each piece
height = 20 # This is the height of each piece

x = 44 # This is the x co-ordinate of where the piece was placed
y = 71 # This is the y co-ordinate of where the piece was placed

def new_round(value, multiple):
    # Some dirty math to round "value" to the closest multiple of "multiple"
    return int(round(value / float(multiple)) * multiple)

newx = new_round(x, width)  # Round x to the closest width
newy = new_round(y, height) # Round y to the closest height


By the way, just curious. How are you displaying the images? Pygame? WxPython?

pyngod Nov 2nd, 2007 11:55 AM

Re: python link picture.
 
Thanks that makes sense.... But i have another issue that would there be any way to have the user chose the size and shape of the puzzle piece with that.....

i am new to python and this is a program for my son to learn shapes and things... so i would him to be able to chose the shape.

Sane Nov 2nd, 2007 12:30 PM

Re: python link picture.
 
You're not giving me much to go on here, but the size is going to be defined as the (width, height) in the code. The user can choose what that is, and it should be the size of the piece, without the connecting part of the piece.

http://img211.imageshack.us/img211/1264/jigsawkr6.png

DaWei Nov 2nd, 2007 12:34 PM

Re: python link picture.
 
With ordinary puzzle pieces you're going to have to deal with rotation, also. Are you really looking for a puzzle, or are you looking more for a round-peg in round-hole or "where does the sheep go?" type of training device?

pyngod Nov 2nd, 2007 12:56 PM

Re: python link picture.
 
Ok sorry about that it is a little hard to explain as i am a little new. I am in the concept stage of the program. I am trying to build something where we can take a picture of say the united states and be able to split the pieces as i or some one else choses i.e each states could be its own piece or divide the us by two main parts and so on..... does that make a little more sense.

i have most of the code already designed, alot of the user interface the ability to randomize the pieces but i am stuck on how to make it where a user can select size, and shape of the puzzle piece. hope that help a little more.

Sane Nov 3rd, 2007 10:41 AM

Re: python link picture.
 
So the pieces can vary in size? Ahh. That's a little tricky.

I think your best bet would be to join two pieces together if they are the right fit, and no more than (max_distance_x, max_distance_y) apart.

Then once they are recognized by your program as one piece again, everything takes care of itself: If you click to move that piece, both pieces will move together. If you connect another piece to it, it's just as if you're connecting one with one again.

Then I wouldn't even worry at all about snapping the pieces to any sort of grid. As long as they snap together, it should work out fine as a jigsaw.

To let the user select the sizes and cut up the puzzle manually, you're on your own with this one. I'm thinking you could let the user select an area of the puzzle with an equivilent of a "rectangular marquee", and then cut out and shove the selected area into a different area beside the picture. Clicking the cut piece would move it back. Then it's finished when all of the pieces have been cut out. This definitely depends on the amount of power you want your program to give to the user...

Code-wise, this is what immediately pops to my mind structurally for trying to put a puzzle (of varying size pieces) back together. I'll assume you're quite adept with Python, or else you wouldn't be tackling such a difficult project.

:

  1. class puzzlePiece:
  2.  
  3.     def __init__(self, parent, x, y, width, height):
  4.  
  5.         self.parent = parent
  6.         self.x = x # Piece's co-ordinates with respect to the origin of the image
  7.         self.y = y
  8.         self.width = width  # Width and height of the particular puzzle piece
  9.         self.height = height
  10.  
  11.     def setPos(self, x, y):
  12.  
  13.         # The position with respect to the screen
  14.         self.posx = x
  15.         self.posy = y
  16.  
  17.     def setPrimaryAdjacencies(self, top=None, right=None, bottom=None, left=None):
  18.  
  19.         # Set the adjacency hash given that the puzzle piece is only just one piece
  20.         # The adjacency hash defines what pieces CAN be connected to THIS puzzlePiece
  21.         # Input is 4 other 'puzzlePiece' instances
  22.         self.adjacencies = {}
  23.         if top:
  24.             self.adjacencies[top] = # Use some unique point of 'top' with respect to 'self', that can tell you when the pieces are close enough to connect :: This depends on *how* they are adjacent
  25.         if right:
  26.             # ...
  27.         if bottom:  # yaddi yaddi yaddi ...
  28.  
  29.     def mergePieces(self, mergeWith):
  30.  
  31.         # Merge puzzlePiece instance 'self' with puzzlePiece instance 'mergeWith'
  32.         # Remove mergeWith from self's adjacencies and vice-versa
  33.         # Merge the two self.adjacencies
  34.         # Assign a new width and height
  35.         # Destroy mergeWith
  36.  
  37.     def render(self, x, y):
  38.  
  39.         self.parent.myParentsDrawOnScreenRoutine((self.x, self.y), (self.width, self.height))
  40.  
  41.  
  42. class main:
  43.  
  44.     def isCloseEnough(self, a, b):
  45.  
  46.         # Check the adjacency list of 'a' to see if it is supposed to be adjacent to 'b', and close enough!
  47.         if not a.adjacencies.has_key(b):
  48.             return False
  49.         connectPoint = a.adjacencies[b]
  50.  
  51.         # Check x proximity
  52.         if b.posx > connectPoint[0] + a.posx - self.max_distance and b.posx < connectPoint[0] + a.posx + self.max_distance:
  53.             # Check y proximity
  54.             # Blah blah blah, etc.


pyngod Nov 3rd, 2007 11:14 AM

Re: python link picture.
 
thanks this helps a lot...... i am not technically adept but i know perl, php, and c so th language structure i know.

pyngod Nov 7th, 2007 9:20 AM

Re: python link picture.
 
ok Hey guys i am back...... well i was trying to gather up some code mainly what you showed me earlier and thought of a better way to do it.... i am honestly just asking anyone if this makes sense and if so how plausible would it be to do.

her goes.... i thought that since i want them to chose the size and the general shape of the puzzle piece... i thought it would be nice to give them something like a cookie cutter to "cut" the image to the size (with some pre determined choices) that they want and then have it go through a module that would randomize the pieces depending on the level of difficulty they choose.... say easy would just separate the pieces and leave them in the general area then say have it increase by rotating the pieces and randomizing the pieces farther from the start point.

does that make sense to any one else or do you think that would create to many problems.


All times are GMT -5. The time now is 3:25 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC