Coder Social home page Coder Social logo

yanto-manta / bentley-ottman Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ggolikov/bentley-ottman

0.0 0.0 0.0 1.07 MB

Bentley-Ottman sweepline algorithm implementation in js

Home Page: https://ggolikov.github.io/bentley-ottman/

License: MIT License

JavaScript 100.00%

bentley-ottman's Introduction

Bentley-Ottman sweepline

This is a Bentley-Ottman sweepline algorithm implementation, both for Node.js and browser. It finds all intersection in a set of 2D segments, uses balanced avl tree internally.

Demo

var findIntersections = require('bentley-ottman-sweepline');

var segments = [
    [[0, 1], [3, 1]],
    [[2, 0], [2, 2]]
]

console.log(findIntersections(segments));

Segment traceability

There are several implementations of this algorithm available in JavaScript, see the NPM repository. This is neither the fastest, nor the most reliable (it's known to fail for multiple cartesian intersections; this is obviously fixable with a little TLC). Having said all that, this particular implementation is the only one which offers segment traceability. That is, you can define your segments as

let segments = [
	[[0,0], [1,1]],
	[[1,0], [0,1]]
];

console.log(JSON.stringify(findIntersections(segments)));

// [{"x":0.5,"y":0.5,"segmentID":["0","1"]}]

but you can also define your segments as

let segments = {
	MySpecialSegmentA: [[0,0], [1,1]],
	MySpecialSegmentB: [[1,0], [0,1]],
};

console.log(JSON.stringify(findIntersections(segments)));

// [{"x":0.5,"y":0.5,"segmentID":["MySpecialSegmentA","MySpecialSegmentB"]}]

This is the only Bentley-Ottman implementation that offers this feature.

Browser build

In order to use the library in the browser, simply open a console in the root folder and execute

npx browserify src/findIntersections.js -t babelify -o findIntersectionsBundle.js -s findIntersections

You can now use findIntersectionsBundle.js in the browser, and you have access to findIntersections(). Change -o findIntersectionsBundle.js to a different filename if you want to change the name of the output file, and change -s findIntersections to some other name if you want to alias function findIntersections() for some reason.

bentley-ottman's People

Contributors

ggolikov avatar bogdan-stancescu-rbc avatar gutza avatar dependabot[bot] 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.