Skip to main content

2D Geometry Library

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pygeometry2d-1.2.0.tar.gz (16.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pygeometry2d-1.2.0-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

Details for the file pygeometry2d-1.2.0.tar.gz.

File metadata

  • Download URL: pygeometry2d-1.2.0.tar.gz
  • Upload date:
  • Size: 16.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for pygeometry2d-1.2.0.tar.gz
Algorithm Hash digest
SHA256 5d584a42d02dbc14a2c0c87e3d0b77ff2165267f5c8c4cda0defa2f63f00787a
MD5 a4d4d376bac929f0a7154a3ad7e4bfc8
BLAKE2b-256 a30cd1d4934f73979150c942d9f7319b8528d100a32fa87714768744cd360dbf

See more details on using hashes here.

File details

Details for the file pygeometry2d-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: pygeometry2d-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 12.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for pygeometry2d-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 61f04e8d891fad7442a1422d198451fc5d1629050b3859d50a41b6d2de859a2d
MD5 764a530587b3da30266869380a76c671
BLAKE2b-256 226886092fde93f899fdc22dc2c05e5bcc2b854e5fc589d1c9317dda96f46ce3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page