GithubHelp home page GithubHelp logo

anvaka / remove-overlaps Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 1.0 179 KB

Remove overlaps between circles

Home Page: https://anvaka.github.io/remove-overlaps/demo/

License: MIT License

JavaScript 100.00%

remove-overlaps's Introduction

remove-overlaps build status

Given set of N circles at predefined positions attempts to remove overlap between circles. Demo.

usage

By default remove overlaps will remove overlaps from circles:

var removeOverlaps = require('remove-overlaps')
var circles = [
  {x: 0, y: 0, r: 10},
  {x: 1, y: 0, r: 3}
]
var lastMove = removeOverlaps(circles)
var dist = distance(circles[0], circles[1])
assert(dist >= 13, 'it moved circles far enough')
assert(lastMove < 1, 'it converged!')

If you have rectangular objects, then to remove overlaps:

var removeOverlaps = require('remove-overlaps')

// Each rectangle has x,y properties - they are centers of the rectangles
// Width and height are width and height. So if you want to have a standard
// left/top/right/bottom approach then:
//
// left = x - width/2
// right = x + width/2
// top = y - height/2
// bottom = y + height/2
var rectangles = [
  {x: 0, y: 0, width: 10, height: 10},
  {x: 0, y: 0, width: 3, height: 4}
]
var lastMove = removeOverlaps(rectangles, {method: 'rectangle'})
assert(lastMove < 1, 'it converged!')
assert(!rectangleOverlaps(rectangles[0], rectangles[1]), 'Rectangles do not overlap anymore')

Performance considerations

The library is not RAM optimized, but uses QuadTrees to optimize CPU. Under the hood it constructs a quad tree from given set of points, and then performs several iterations of overlap lookup:

1. put each node into a quad tree
2. for each node:
3.  if node A overlaps other node B, then
4.    Remove overlap between A and B
5. Go to #2 and repeat until either no overlaps found or
   maximum amount of iterations reached.

In theory since line #4 removes overlap only between two nodes, there is no guarantee that algorithm converges. In practice it converges most of the time in very few steps (thus step #5 repeats algorithm several times)

license

MIT

remove-overlaps's People

Contributors

anvaka avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.