?
Solved

jquery to position elements randomly, but without overlap

Posted on 2009-05-05
2
Medium Priority
?
3,125 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
[X]
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
2 Comments
 
LVL 12

Accepted Solution

by:
alien109 earned 2000 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

Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

Question has a verified solution.

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

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Suggested Courses

752 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