Code Monkey home page Code Monkey logo

earcut-python's Introduction

earcut-python

A pure Python port of the earcut JavaScript triangulation library. The latest version is based off of the earcut 2.1.1 release, and is functionally identical.

The original project can be found here: https://github.com/mapbox/earcut

Usage

triangles = earcut([10,0, 0,50, 60,60, 70,10]) # Returns [1,0,3, 3,2,1]

Signature: earcut(vertices[, holes, dimensions = 2]).

  • vertices is a flat array of vertex coordinates like [x0,y0, x1,y1, x2,y2, ...].
  • holes is an array of hole indices if any (e.g. [5, 8] for a 12-vertex input would mean one hole with vertices 5โ€“7 and another with 8โ€“11).
  • dimensions is the number of coordinates per vertex in the input array (2 by default).

Each group of three vertex indices in the resulting array forms a triangle.

# Triangulating a polygon with a hole
earcut([0,0, 100,0, 100,100, 0,100,  20,20, 80,20, 80,80, 20,80], [4])
# [3,0,4, 5,4,0, 3,4,7, 5,0,1, 2,3,7, 6,5,1, 2,7,6, 6,1,2]

# Triangulating a polygon with 3d coords
earcut([10,0,1, 0,50,2, 60,60,3, 70,10,4], null, 3)
# [1,0,3, 3,2,1]

If you pass a single vertex as a hole, Earcut treats it as a Steiner point.

If your input is a multi-dimensional array, you can convert it to the format expected by Earcut with earcut.flatten:

# The first sequence of vertices is treated as the outer hull, the following sequneces are treated as holes.
data = earcut.flatten([[(0,0), (100,0), (100,100), (0,100)], [(20,20), (80,20), (80,80), (20,80)]])
triangles = earcut(data['vertices'], data['holes'], data['dimensions'])

After getting a triangulation, you can verify its correctness with earcut.deviation:

deviation = earcut.deviation(vertices, holes, dimensions, triangles)

Returns the relative difference between the total area of triangles and the area of the input polygon. 0 means the triangulation is fully correct.

earcut-python's People

Contributors

joshuaskelly avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

earcut-python's Issues

TypeError: unsupported operand type(s) for <<: 'float' and 'int'

awesome repo!!

I am getting the above error when running a fairly large and complex concave polygon with a hole ( full trace is below ).

updating lines 455 and 456 to force the result to an int seems to be sufficient
x = int(32767 * (x - minX) // size)
y = int(32767 * (y - minY) // size)

  File ".../scratch_python/env/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3343, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-c540e453c829>", line 1, in <module>
    runfile(.../scratches/scratch.py', wdir='.../scratches')
  File "/snap/pycharm-professional/230/plugins/python/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "/snap/pycharm-professional/230/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File ".../scratches/scratch.py", line 48, in <module>
    main('.../alabama_smsa.geojson')
  File ".../scratches/scratch.py", line 39, in main
    triangles.extend(triangulate_poly(x['geometry']['coordinates']))
  File ".../scratches/scratch.py", line 14, in triangulate_poly
    earcut_indexes = earcut.earcut(flat_coords['vertices'],flat_coords['holes'],flat_coords['dimensions'])
  File ".../earcut/earcut.py", line 48, in earcut
    earcutLinked(outerNode, triangles, dim, minX, minY, size)
  File ".../earcut/earcut.py", line 106, in earcutLinked
    indexCurve(ear, minX, minY, size)
  File ".../earcut/earcut.py", line 369, in indexCurve
    p.z = zOrder(p.x, p.y, minX, minY, size)
  File ".../earcut/earcut.py", line 458, in zOrder
    x = (x | (x << 8)) & 0x00FF00FF
TypeError: unsupported operand type(s) for <<: 'float' and 'int'```

PyPI release?

Hi - I found your library very useful for my last PyWeek entry What The Frog?!.

But I ended up having to vendor the library (as a git submodule).

Have you thought about creating a PyPI package for it?

TypeError: 'module' object is not callable

Steps to reproduce:
Install the package from pypi:
pip3 install earcut

Import and run example from the README.md file:

>>> import earcut
>>> triangles = earcut([10, 0, 0, 50, 60, 60, 70, 10])
Traceback (most recent call last):
    File "<input>", line 1, in <module>
        earcut([10, 0, 0, 50, 60, 70, 10])
TypeError 'module' object is not callable

Expected output:

[1, 0, 3, 3, 2, 1]

pip show earcut

Name: earcut
Version: 1.1.4
Summary: A pure Python port of the earcut JS triangulation library
Home-page: https://github.com/MetacitySuite/earcut-python
Author: Metacity
Author-email: 
License: ISC
Location: /home/rtfm/.local/lib/python3.10/site-packages
Requires: numpy
Required-by:

The referenced Location discussed in #1 and introduced by @vojtatom is not available any more.

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.