Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

jquery to position elements randomly, but without overlap

Posted on 2009-05-05
2
3,012 Views
Last Modified: 2012-06-21
I am placing a dynamic amount of elements in random positions in a parent element. Each element is 75px square and position:absolute and I'd like to make sure that their random x and y co-ordinates don't overlap those of any already positioned elements.

Points for urgency...
$('.className').each(function(){
$(this).css({ 'top':randomXToY(0,375),
	 'left': randomXToY(0,450),
	});
});
 
// in case it's useful, my random function is just:
 
function randomXToY(minVal,maxVal,floatVal){
  var randVal = minVal+(Math.random()*(maxVal-minVal));
  return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
}

Open in new window

0
Comment
Question by:steam23
2 Comments
 
LVL 12

Accepted Solution

by:
alien109 earned 500 total points
ID: 24307439
What you're asking for is actually a fairly complex algorithm. You might want to research "Rectangle Packing". There are numerous methods each has trade offs in speed and the tightness of the packing.

Layout algorithms are tough. Another method you might consider is a force directed layout or force based layout -> http://en.wikipedia.org/wiki/Force-based_algorithms

I was working on a force based layout for an experiment in flash (if you want to see what the end result might be)
http://blog.alien109.com/2009/02/17/force-directed-node-interface-2/

The force based method probably is going to provide a more structured layout however which may not work if you need a more scattered aesthetic.

0
 

Author Closing Comment

by:steam23
ID: 31578017
Sorry I hadn't accepted sooner... been wrapping my brain around it...

Thanks
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
In this tutorial viewers will learn how to style rounded corners for elements in CSS using the border-radius property Begin with a normal styled element such as a div: To style all four corners of the div to be the same degree of roundness, use the …
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question