Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 2nd, 2007, 9:32 AM   #1
pyngod
Newbie
 
Join Date: Nov 2007
Posts: 6
Rep Power: 0 pyngod is on a distinguished road
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.
pyngod is offline   Reply With Quote
Old Nov 2nd, 2007, 10:20 AM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,798
Rep Power: 5 Sane will become famous soon enough
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?

Last edited by Sane; Nov 2nd, 2007 at 10:37 AM.
Sane is offline   Reply With Quote
Old Nov 2nd, 2007, 10:55 AM   #3
pyngod
Newbie
 
Join Date: Nov 2007
Posts: 6
Rep Power: 0 pyngod is on a distinguished road
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.
pyngod is offline   Reply With Quote
Old Nov 2nd, 2007, 11:30 AM   #4
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,798
Rep Power: 5 Sane will become famous soon enough
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.

Sane is offline   Reply With Quote
Old Nov 2nd, 2007, 11:34 AM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 2nd, 2007, 11:56 AM   #6
pyngod
Newbie
 
Join Date: Nov 2007
Posts: 6
Rep Power: 0 pyngod is on a distinguished road
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.
pyngod is offline   Reply With Quote
Old Nov 3rd, 2007, 9:41 AM   #7
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,798
Rep Power: 5 Sane will become famous soon enough
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.

Python Syntax (Toggle Plain Text)
  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.

Last edited by Sane; Nov 3rd, 2007 at 9:53 AM.
Sane is offline   Reply With Quote
Old Nov 3rd, 2007, 10:14 AM   #8
pyngod
Newbie
 
Join Date: Nov 2007
Posts: 6
Rep Power: 0 pyngod is on a distinguished road
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 is offline   Reply With Quote
Old Nov 7th, 2007, 8:20 AM   #9
pyngod
Newbie
 
Join Date: Nov 2007
Posts: 6
Rep Power: 0 pyngod is on a distinguished road
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.
pyngod is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
python for newbie wizzkid Python 3 Jun 7th, 2006 7:31 PM
[tutorial] Python for programming beginners coldDeath Python 30 Dec 14th, 2005 11:35 AM
Convert Python script to C++ code clanotheduck Python 17 Sep 25th, 2005 8:55 AM
Advanced Python Tricks Arevos Python 19 Sep 24th, 2005 7:39 AM
Python - A Programmers Introduction coldDeath Python 17 Aug 19th, 2005 12:41 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:06 PM.

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