Did you miss your activation email?

• November 16, 2018, 07:24 AM
• Proudly celebrating 13 years online.
• Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.

### Author Topic: How to divide an image into random sized parts?  (Read 6823 times)

#### crono

• Charter Honorary Member
• Joined in 2005
• Posts: 179
##### How to divide an image into random sized parts?
« on: April 03, 2008, 06:29 PM »
Hi,

I want to divide an image into random sized parts. I need this for some kind of puzzle game I'm writing with a friend. Dividing an image into even sized parts like this:

[attach=#1][/attach]

is not a problem at all. But how could I divide in into randomsized parts like this:

[attach=#2][/attach]

The algorithm I'm searching must be capeable of slicing any image in a variable amount (which is set by the player) of parts. The resulting images (parts) should not be smaller than X pixels and not larger than Y Pixels. The parts may not overlap each other. I have no idea how to do this.

#### mouser

• First Author
• Joined in 2005
• Posts: 38,960
##### Re: How to divide an image into random sized parts?
« Reply #1 on: April 03, 2008, 06:39 PM »
a non-intuitive approach might work well ---
start by imagining a very large grid that divides the entire picture into a grid where each square is X by Y.
now simply REMOVE random edges until you have the desired # of pieces.

(note you'll have to do this a bit differently if you only want rectangular shapes pieces)

#### crono

• Charter Honorary Member
• Joined in 2005
• Posts: 179
##### Re: How to divide an image into random sized parts?
« Reply #2 on: April 03, 2008, 06:58 PM »
Good idea Mouser But I think keeping track of only producing rect-shapes will be very tricky. I have to use only rect-shapes, because the puzzle will be jpg + javascript based, so there is no way to overlay other shaped images (because of the missing transparency). I think I will try to prototype this Idea on the weekend - Thanks

#### Ampa

• Charter Member
• Joined in 2005
• Posts: 592
• I am cute ;)
##### Re: How to divide an image into random sized parts?
« Reply #3 on: April 04, 2008, 07:08 AM »

Code: Text [Select]
1. Begin with a single Slice (this will be the whole image)
2. Repeat until you have enough slices {
3.   Randomly choose a slice (check that it is not already too small to further divide)
4.   Randomly choos an orientation (vertical or horizontal)
5.   Divide the Slice it into two parts (this need not be two even parts)
6. }

This will always ensure you have rectangles.

#### crono

• Charter Honorary Member
• Joined in 2005
• Posts: 179
##### Re: How to divide an image into random sized parts?
« Reply #4 on: April 10, 2008, 10:41 AM »
Thanks for your input Ampa. I came to the exact same methode while I was in the shower  It works very well

#### Silverchip

• Supporting Member
• Joined in 2006
• Posts: 9
##### Re: How to divide an image into random sized parts?
« Reply #5 on: April 23, 2008, 07:48 PM »
It's probably not irrelevant but you could print, tear it to size desired then scan. I had to post it lol

#### crono

• Charter Honorary Member
• Joined in 2005
• Posts: 179
##### Re: How to divide an image into random sized parts?
« Reply #6 on: May 08, 2008, 06:15 AM »
It's probably not irrelevant but you could print, tear it to size desired then scan. I had to post it lol
-Silverchip
I will remember this if there is a table-top version of the game