Skip to main content

A lightweight and intuitive Python package for applying 2D geometric transformations—such as translation, scaling, rotation, shearing, and reflection—to points and polygons. Ideal for graphics, simulations, and educational use.

Project description

transformations_2d

A Python package for performing 2D geometric transformations (translation, scaling, rotation, shearing, reflection) on points and polygons using an object-oriented and immutable design.

✨ Features

  • Immutable Point and Polygon classes
  • Transformation methods return new instances (non-destructive)
  • Supports:
    • Translation
    • Scaling (from origin or custom point)
    • Rotation (around origin or custom point)
    • Shearing (with reference axis)
    • Reflections (across X, Y, origin, or line y = x)
  • Built-in methods for:
    • String representation (__str__)
    • Dictionary serialization (to_dict, from_dict)

📦 Installation

Install via PyPI:

pip install transformations_2d

If you want to install directly from source for development:

git clone https://github.com/mainak-debnath/transformations_2d.git
cd transformations_2d
pip install .

🚀 Usage

The package provides Point and Polygon classes, each with methods for various 2D transformations. All transformation methods return a new transformed object, leaving the original unchanged (immutability).

Point Transformations

from transformations_2d.geometry import Point

# --- Create a point ---
p = Point(10, 20)
print(f"Original Point: {p}")
# → Point(10.00, 20.00)

# --- Access coordinates ---
print(f"x = {p.x}, y = {p.y}")
# → x = 10.0, y = 20.0

# --- Serialize to dict ---
print("Point as dict:", p.to_dict())
# → Point as dict: {'x': 10.0, 'y': 20.0}

# --- Apply transformations ---

# Translate by (5, -3)
translated = p.translate(5, -3)
print(f"Translated (+5, -3): {translated}")
# → Point(15.00, 17.00)

# Scale from origin by (2, 1.5)
scaled = p.scale(2, 1.5)
print(f"Scaled (2x, 1.5x) from origin: {scaled}")
# → Point(20.00, 30.00)

# Scale around custom origin (5, 5)
origin = Point(5, 5)
scaled_custom = p.scale(2, 1.5, origin=origin)
print(f"Scaled (2x, 1.5x) from origin {origin}: {scaled_custom}")
# → Point(15.00, 27.50)

# Rotate 90° counter-clockwise
rotated = p.rotate(90)
print(f"Rotated 90° CCW: {rotated}")
# → Point(-20.00, 10.00)

# Rotate -45° clockwise around custom origin
rotated_custom = p.rotate(-45, origin=origin)
print(f"Rotated -45° CW around {origin}: {rotated_custom}")
# → Point(19.14, 12.07)

# Shear along X-axis with factor 0.5
sheared_x = p.shear_x(0.5)
print(f"Sheared X (0.5): {sheared_x}")
# → Point(20.00, 20.00)

# Shear along Y-axis with factor 0.2
sheared_y = p.shear_y(0.2)
print(f"Sheared Y (0.2): {sheared_y}")
# → Point(10.00, 22.00)

# Reflect across X-axis
reflected_x = p.reflect_x()
print(f"Reflected across X-axis: {reflected_x}")
# → Point(10.00, -20.00)

# Reflect across Y-axis
reflected_y = p.reflect_y()
print(f"Reflected across Y-axis: {reflected_y}")
# → Point(-10.00, 20.00)

# Reflect about the origin
reflected_origin = p.reflect_origin()
print(f"Reflected about origin: {reflected_origin}")
# → Point(-10.00, -20.00)

# Reflect across the line y = x
reflected_xy = p.reflect_xy_line()
print(f"Reflected across y = x: {reflected_xy}")
# → Point(20.00, 10.00)

Polygon Transformations

from transformations_2d.geometry import Point, Polygon

# --- Create a triangle using 3 points ---
p1 = Point(0, 0)
p2 = Point(5, 0)
p3 = Point(0, 5)
triangle = Polygon([p1, p2, p3])

# --- Print original polygon and access its points ---
print(f"Original Triangle: {triangle}")
# Output: Original Triangle: Polygon([(0.00, 0.00), (5.00, 0.00), (0.00, 5.00)])

# --- Access individual point coordinates ---
print("Triangle vertices:")
for pt in triangle.points:
    print(f"({pt.x}, {pt.y})")
# Output:
# (0.0, 0.0)
# (5.0, 0.0)
# (0.0, 5.0)

# --- Serialize to dictionary ---
print("Triangle as dict:", triangle.to_dict())
# Output:
# Triangle as dict: {'points': [{'x': 0.0, 'y': 0.0}, {'x': 5.0, 'y': 0.0}, {'x': 0.0, 'y': 5.0}]}

# --- Apply transformations ---

# Translate by (2, 2)
translated = triangle.translate(2, 2)
print(f"Translated Triangle (by +2,+2): {translated}")
# → Polygon([(2.00, 2.00), (7.00, 2.00), (2.00, 7.00)])

# Scale by 2x from origin
scaled = triangle.scale(2, 2)
print(f"Scaled Triangle (2x from origin): {scaled}")
# → Polygon([(0.00, 0.00), (10.00, 0.00), (0.00, 10.00)])

# Rotate 90° counter-clockwise about origin
rotated = triangle.rotate(90)
print(f"Rotated Triangle (90° CCW): {rotated}")
# → Polygon([(0.00, 0.00), (0.00, 5.00), (-5.00, 0.00)])

# Reflect across the Y-axis
reflected = triangle.reflect_y()
print(f"Reflected Triangle (across Y-axis): {reflected}")
# → Polygon([(0.00, 0.00), (-5.00, 0.00), (0.00, 5.00)])

Development

🧪 Running Tests

To run the tests (once implemented in tests/test_geometry.py):

pip install pytest
pytest tests/

🤝 Contributing

Feel free to open issues or submit pull requests.

📄 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

transformations_2d-0.2.0.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

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

transformations_2d-0.2.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file transformations_2d-0.2.0.tar.gz.

File metadata

  • Download URL: transformations_2d-0.2.0.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for transformations_2d-0.2.0.tar.gz
Algorithm Hash digest
SHA256 4ed0fafc06e17d2d275b0d31650b47c59b8f906a831d3500b7a3837703528f86
MD5 8a04ee013653fcc2e140411d5d399443
BLAKE2b-256 09fa9c498dd4737bbdaed74cf049406e77d722aaa1a2cb9046d4cfda0be42abf

See more details on using hashes here.

File details

Details for the file transformations_2d-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for transformations_2d-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 812ca4fef7c0b5e29d72d86fc88ef8b376725bd86bc37b851da831581200ed1b
MD5 f8e0240479e2c708f6d05f891796a58d
BLAKE2b-256 f7cdafc5cb3f18e41998410d6161e20f8729c58316ec921829368ca0b1aa74a6

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