Solved

jquery to position elements randomly, but without overlap

Posted on 2009-05-05
2
3,064 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 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In this Micro Tutorial viewers will learn how to create a CSS image sprite (In a later tutorial, viewers will learn how to use CSS and HTML to create a navigation menu using this sprite) Open a new Photoshop document with a width of (Icon width)x(N…
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…

739 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