Packing rectangles into a rectangle

Once, I have been given the following task: make a JavaScript plugin that allows to fit a variable number of rectangles of different sizes into another rectangle.
Or better, you have a variable number of surfaces to cover with iron, iron is provided in panels of size depending on the factory: calculate the minimum number of panels that you need to fulfill the request. In this calculation, you need to include a margin between the rectangles as well – that corresponds to the line of cut.

It was a quite interesting challenge. Finding the perfect algorithm in a short time was quite difficult. Finally I have realized that you cannot rely on a unique procedure to find the best result, because it will not satisfy any possible combination of differently sized rectangles; so it is reasonable to think that the best way is to use different procedures, or better procedures that inspect the panel in different way (or direction), and then ultimately check the one that is giving the best fit.

The routine

Anyway, all of them need to rely on a recursive routine, because using a simple cycle would make things very difficult to manage.
After deciding the starting point (in our case, let’s say top-left) and the direction (from left to right), the procedure consist in inserting the rectangles recursively, starting from the biggest ones: one of the biggest rectangles is inserted at the top-left, then the remaining space of the panel is divided in two sections (it would be like having 2 new panels to be fitted) and we are going to see if the next rectangle is going to fit in one of them. If not, we pass to the next rectangle (smaller in size) and check for it.
Of course, previously, I had sorted the rectangles in descending order, from the biggest to the smallest.
When a rectangle is too big to fit in the sections, it is left aside for a new check in another panel.
If a rectangle cannot be contained in one panel, then it is split in more panels – or better we are taking pieces of more panels to cover the area occupied by that rectangle.

So, as I said, after fitting one rectangle, the remaining space is divided in 2 ways, like showed in the images: basically, one right area and one inferior area, but with different borders. So I made a routine where the space is divided in horizontal way and a routine where space is divided in vertical way.

dividing_area1   dividing_area2

Every time another rectangle is inserted, the area where is has been inserted is divided again in 2 sections, where we are going check if the next rectangle in our array is fitting. For example, in the next image, where we are performing a division in horizontal sense, we can see the green (new) rectangle is placed in the right area and after that the area is divided in 2 sections: right area and inferior area. In these 2 new areas the routine will try to fit the rest of rectangles.


A working example of this routine is at the following link: packing rectangles within panels.


array_insert for PHP

Similar to array_splice, this array_insert function allows to insert an object in an array – thing that unfortunately is not possible with array_splice that deals only with array parameters. This function offers an option for replacing the item correspondent to the offset.

function array_insert($array, $offset, $object[, $replace=false])

$array is the array to be modified
$offset is the position where we want to insert our object. It needs to be positive and within the limits given by the dimension of the array. If it is less than 0, the object will be inserted as the first element; if it is over the maximum dimension, the object will be inserted as the final element.
$object is the object to be inserted.
$replace is an optional parameter that indicates if the array element is going to be replaced. Default is false.

Follows the code.
Continue reading “array_insert for PHP”

How to create a reverse slideDown-slideUp jQuery animation

For those who love the slideDown and slideUp animation effect and would like to know how to create the reverse of this effect, here is a nice trick to simulate it. SlideDown will be the effect that makes the object disappear, while SlideUp will be the effect that makes the object re-appear. I called this effect blink, because it reminds me the blinking of an eye (demo links at the bottom).

If you try to use simply jQuery animate function, the result is to obtain an object that effectively slide down until is completely gone out of the view; but it displays in a different way from the usual slideUp that hides the object starting from the bottom up to the top, without any effective “sliding” of the object itself.

So the trick is using some CSS settings.
Let’s consider the (common) case that our object is an image. It will be contained in a layer that we can call “blink-box” and this layer will be contained in another one that we can call “wrap”.


Now the trick is to set the position of the image as absolute with bottom equals to zero; then the same settings will be applied to its container “blind-box”, whose overflow needs to be hidden of course. Another important setting is the width and height of the object (our image) that needs to be assigned to both the containing layers.

   position: absolute;
   overflow: hidden;
div.blink-box img{
   position: absolute;

The jQuery function will simply be two animate functions, one after the other with height equals to “toggle”.

jQuery.fn.blink = function(speed, easing, callback, delay){
   var $this = $(this);
   return $this.animate({
      height: 'toggle'
   }, speed, easing, function(){
      $this.delay(delay || 1000).animate({
          height: 'toggle'

At the following links you will see two demos. The first one sees the blinking of one image and the half blinking. The second link will display a blinking with multiple images and slideshow with blinking effect.
The code is included in the files themselves.

  1. Blinking effect animation with single image
  2. Slideshow of more images with blinking effect animation

How to change the WordPress upload directory

Have you ever wondered if it is possible to change the place where your media files are stored?
Well, there is a way and it is very simple.
Just go into the admin panel
Then go Settings->Media.  As you have opened the page, scroll down to Uploading Files. Everything happens there!

Wordpress uploading files section
Continue reading “How to change the WordPress upload directory”

Quick delicious pasta for busy designers

quick and delicious pastaThere are some moments when deadlines are so tight that do not grant us a break for a snack, surely unimaginable for a meal.
So let’s see how to prepare a delicious and quick dish with spaghetti and an incredibly easy sauce.


Spaghetti (in the picture you can see spelt pasta, but you can use common wheat pasta)
Olive Oil (possibly Extra vergine)


Put some water in a pot and add some salt (quantity depending on the quantity of water). When the water boil, add the spaghetti and let them cook as long as indicated on the wrapper (usually around 10 minutes).
When the pasta is ready, drain them and put them in a plate. Then add some lemon (a tea spoon), some oil (a spoon) and mix them with the spaghetti using a fork.
Finally grate some Parmesan over them and that’s all, your meal is ready!

Treatment for kitchen burns

Has anybody ever wondered how to prevent our skin from becoming red or blistered after getting burn?

burn blisterLet’s suppose you are preparing a delicious sauce, you are so happy and cannot wait to taste it on your pasta. But, ouch!, accidentally some drops of your sauce, or of hot oil or hot water have splashed on your hand. Or let’s suppose that you have touched the hot pot… it is not only a painful thing, but after few instances the affected part will become red and then a blister will nicely blow there. Of course you do not want to spoil your happiness because of that unfortunate accident, of course not!


And so, what to do?

No worries, there is a simple and quick way to prevent this effect to occur: cover the affected part with some wheat flour! You will be amazed that no red or blister will appear.
Just that simple!


Of course this treatment is NOT advisable in serious burn cases; in those situations it is better to go to a doctor.

Never forget to keep some flour in your kitchen, ok?

How to add auto-playing music to a webpage in HTML5

We will see how to add dynamically an invisible music auto-player to a web page and a switch to turn it on/off

My musical web pageIn this article we will see how to add  music player to a web page  that will start automatically at the loading of the page. We will use the Javascript audio object so that the player will be invisible. We will also create and add a switch to our page with JQuery, for toggling the music on and off.
All the functions of our example are included in a zip file that can be downloaded.

Continue reading “How to add auto-playing music to a webpage in HTML5”