Skip to main content

2D Geometry Library in Python

en pt-br

This is a lightweight and simple library for handling 2D geometries using only Python's native libraries. It allows performing common geometric operations such as distance calculations, intersections, rotations, and other geometric transformations.

Main Features

  • Point manipulation (XY class).
  • Creation and operations with lines (Line).
  • Support for arcs (Arc) and circles (Circle).
  • Bounding box generation (BoundingBox).
  • Handling polylines (Polyline) and rectangles (Rectangle).
  • Geometric utilities such as angle normalization, orthogonality checking, shape intersections and polyline optimization.

Installation

Install the library via pip:

pip install pygeometry2d

Then, import it into your project:

from pygeometry2d import XY, Line, Circle, Polyline

Basic Usage

Create points and lines

from pygeometry2d import XY, Line

p1 = XY(0, 0)
p2 = XY(4, 4)
line = Line(p1, p2)
print(line.length)  # 5.65685

Examples

Join disordered connected segments into polylines

from pygeometry2d import XY, Line, GeomUtils

segments = [
    Line(XY(1, 0), XY(2, 0)),
    Line(XY(10, 10), XY(11, 10)),
    Line(XY(0, 0), XY(1, 0)),
    Line(XY(2, 0), XY(3, 5))
]

joined_polylines = GeomUtils.join(segments)
for polyline in joined_polylines:
    print(polyline.points)

Output:

[(10, 10), (11, 10)]
[(0, 0), (1, 0), (2, 0), (3, 5)]

Find circle passing through three points

from pygeometry2d import XY, GeomUtils

p1 = XY(0, 1)
p2 = XY(1, 0)
p3 = XY(2, 1)

center, radius = GeomUtils.circle_by_3_points(p1, p2, p3)
print(f"Center: {center}, Radius: {radius}")

Output:

Center: (1.0, 1.0), Radius: 1.0

Optimize connected line segments

from pygeometry2d import XY, Line, GeomUtils

segments = [
    Line(XY(0, 0), XY(1, 0)),
    Line(XY(2, 0), XY(3, 0)),
    Line(XY(1, 0), XY(2, 0)),
]

optimized = GeomUtils.optimize_segments(segments)
for line in optimized:
    print(line.start, line.end)

Output:

(0,0) (3,0)

Find intersection between two lines

from pygeometry2d import XY, Line

line1 = Line(XY(0, 0), XY(1, 1))
line2 = Line(XY(0, 1), XY(1, 0))

intersection = line1.intersection(line2)
print(intersection)

Output:

(0.5, 0.5)

Get the general equation of a line (Ax + By + C = 0)

from pygeometry2d import XY, Line

line = Line(XY(0, 0), XY(2, 2))
a, b, c = line.general_equation_coefficients
print(f"{a}x + {b}y + {c} = 0")

Output:

1x + -1.0y + 0.0 = 0

Documentation

Full documentation is available on readthedocs.

Contribution

If you would like to contribute with improvements or suggest new features, feel free to open a PR or create an issue on the GitHub repository.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Metadata

Release files for pygeometry2d 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pygeometry2d 1.2.0
File Size Uploaded Provenance Actions
pygeometry2d-1.2.0.tar.gz 16.7 kB Without provenance Details for pygeometry2d-1.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pygeometry2d 1.2.0
File Interpreter ABI Platform Actions
pygeometry2d-1.2.0-py3-none-any.whl Python 3 none any Details for pygeometry2d-1.2.0-py3-none-any.whl

Total release size: 29.6 kB

Release files / pygeometry2d-1.2.0.tar.gz

Download URL pygeometry2d-1.2.0.tar.gz
Size 16.7 kB
Tags Source
SHA-256 checksum
How to use checksums
5d584a42d02dbc14a2c0c87e3d0b77ff2165267f5c8c4cda0defa2f63f00787a
BLAKE2b-256 checksum
How to use checksums
a30cd1d4934f73979150c942d9f7319b8528d100a32fa87714768744cd360dbf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.3

Release files / pygeometry2d-1.2.0-py3-none-any.whl

Download URL pygeometry2d-1.2.0-py3-none-any.whl
Size 12.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
61f04e8d891fad7442a1422d198451fc5d1629050b3859d50a41b6d2de859a2d
BLAKE2b-256 checksum
How to use checksums
226886092fde93f899fdc22dc2c05e5bcc2b854e5fc589d1c9317dda96f46ce3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.3

Security for pygeometry2d 1.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page