Skip to main content

Provides a cartesian plane canvas able to move and zooming in pygame.

Project description

Cartesian Plane

Provides a cartesian plane canvas able to move and zooming in pygame.

Key topics

Overview

With this module you can use cartesian cordinates to draw forms and create objects in a cartesian plane. Which is useful for simple 2D graphics.

Install

pip install cartesian-plane

CartesianPlane

cartesian_plane = CartesianPlane(screen)

Creates a CartesianPlane canvas.

Arguments:

  • screen: pygame screen - The screen where the canvas will be drawn.

Returns a CartesianPlane object.

plane_to_screen()

screen_pos = cartesian_plane.plane_to_screen(point)

Converts a cartesian point to a screen position.

Arguments:

  • point: (x, y) | [x, y] - The cartesian point to convert.

Returns returns a screen position as a list [x, y].

screen_to_plane()

cartesian_point = cartesian_plane.screen_to_plane(screen_pos)

Converts a screen position to a cartesian point.

Arguments:

  • screen_pos: (x, y) | [x, y] - The screen position to convert.

Returns a cartesian point as a list [x, y].

scale()

scaled_dimention = cartesian_plane.scale(dimension)

Scales a dimension to the current zoom level.

Arguments:

  • dimension: int | float - The dimension to scale.

Returns a scaled dimension.

debug

cartesian_plane.debug(name1=value1, name2=value2, ...)

Add debug information to the Cartesian Plane. On each update() call, the debug information will be drawn.

event_handling()

for event in pygame.event.get():
    # event handling
    ...

    cartesian_plane.event_handling(event)

This function is used on the event loop. It will handle the events of move and zooming the Cartesian Plane.

update()

cartesian_plane.update()

updates the Cartesian Plane and draws the objects.

Circle

circle = Circle(plane, color, center, radius, width)

Creates a Circle object. The circle is automated drawn on each call of the update() function.

Arguments:

  • plane: CartesianPlane - The Cartesian Plane where the circle will be drawn.
  • color: (r, g, b) | (r, g, b, a) - The color of the circle.
  • center: (x, y) | [x, y] - The center of the circle.
  • radius: int | float - The radius of the circle.
  • width: optional, int - The width of the circle. 0 to fill. default to 0.

Rectangle

rect = Rect(plane, color, rect, width)

Creates a Rectangle object. The rectangle is automated drawn on each call of the update() function.

Arguments:

  • plane: CartesianPlane - The Cartesian Plane where the rectangle will be drawn.
  • color: (r, g, b) | (r, g, b, a) - The color of the rectangle.
  • rect: (x, y, width, height) | [x, y, width, height] - The rectangle.
  • width: optional, int - The width of the rectangle. 0 to fill. default to 0.

Line

line = Line(plane, color, start, end, width)

Creates a Line object. The line is automated drawn on each call of the update() function.

Arguments:

  • plane: CartesianPlane - The Cartesian Plane where the line will be drawn.
  • color: (r, g, b) | (r, g, b, a) - The color of the line.
  • start: (x, y) | [x, y] - The start of the line.
  • end: (x, y) | [x, y] - The end of the line.
  • width: optional, int - The width of the line. default to 1.

Path

path = Path(plane, color, *points, length = 100)

Creates a Path object. The path is automated drawn on each call of the update() function.

Arguments:

  • plane: CartesianPlane - The Cartesian Plane where the path will be drawn.
  • color: (r, g, b) | (r, g, b, a) - The color of the path.
  • points: (x, y) | [x, y] - The points of the path.
  • length: optional, int - The max length of the path. 0 or less to unlimited lenght (not recommended). default to 1000.

add_points()

path.add_points(*points)

adds points to the path.

Arguments:

  • points: (x, y) | [x, y] - The points to add.

draw

This class is used to draw a shape on the screen.

circle()

draw.circle(plane, color, center, radius, width)

Draws a circle on the screen.

Arguments:

  • plane: CartesianPlane - The Cartesian Plane where the circle will be drawn.
  • color: (r, g, b) | (r, g, b, a) - The color of the circle.
  • center: (x, y) | [x, y] - The center of the circle.
  • width: optional, int - The width of the circle. 0 to fill. default to 0.

rect()

draw.rect(plane, color, rect, width)

Draws a rectangle on the screen.

Arguments:

  • plane: CartesianPlane - The Cartesian Plane where the rectangle will be drawn.
  • color: (r, g, b) | (r, g, b, a) - The color of the rectangle.
  • rect: (x, y, width, height) | [x, y, width, height] - The rectangle.
  • width: optional, int - The width of the rectangle. 0 to fill. default to 0.

line()

draw.line(plane, color, start, end, width)

Draws a line on the screen.

Arguments:

  • plane: CartesianPlane - The Cartesian Plane where the line will be drawn.
  • color: (r, g, b) | (r, g, b, a) - The color of the line.
  • start: (x, y) | [x, y] - The start of the line.
  • end: (x, y) | [x, y] - The end of the line.
  • width: optional, int - The width of the line. default to 1.

Example codes

Objects.py

# Objects.py

import pygame
import cartesianPlane

RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)

# Set up the window
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption('Cartesian Plane')
clock = pygame.time.Clock()

plane = cartesianPlane.CartesianPlane(screen)

circle = cartesianPlane.Circle(plane, RED, (0, 0), 10)
rect = cartesianPlane.Rect(plane, GREEN, [20, 10, 10, 10])
line = cartesianPlane.Line(plane, BLUE, [40, 0], [10, 10])
path = cartesianPlane.Path(plane, YELLOW, [0, 0], [10, 10], [20, 20])

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT or event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            pygame.quit()
            quit()

        plane.event_handling(event)

    plane.debug(
        fps=f'{clock.get_fps():.1f}'
    )

    # Update the plane
    plane.update()

    cartesianPlane.draw.circle(plane, GREEN, (15, 15), 1)
    cartesianPlane.draw.rect(plane, BLUE, (25, 25, 10, 10), 1)
    cartesianPlane.draw.line(plane, YELLOW, (35, 35), (45, 45))

    clock.tick()

    # Update the screen
    pygame.display.update()

Senoid.py

# Senoid.py
import pygame
import cartesianPlane
from math import sin

RED = (255, 0, 0)

pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption('Senoid')
clock = pygame.time.Clock()

plane = cartesianPlane.CartesianPlane(screen)

senoid = cartesianPlane.Path(plane, RED, [0, 0], length=0)

x = 0

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT or event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            pygame.quit()
            quit()

        plane.event_handling(event)

    plane.debug(
        fps=f'{clock.get_fps():.1f}',
        points=len(senoid.path)
    )

    # Update the plane
    plane.update()

    clock.tick(60)

    senoid.add_points([x * 100, sin(x) * 100])

    x += 0.01

    # Update the screen
    pygame.display.update()

Todo:

  • add a layer system to the canvas

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

cartesian-plane-1.0.1.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

cartesian_plane-1.0.1-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file cartesian-plane-1.0.1.tar.gz.

File metadata

  • Download URL: cartesian-plane-1.0.1.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for cartesian-plane-1.0.1.tar.gz
Algorithm Hash digest
SHA256 544b42a2bca7974a0f238eb2b9b7231396e6b1f355e7e8243c6bf9585ec67f76
MD5 3f8a120f8a086c6517636dcccad95dbe
BLAKE2b-256 9a870880b74369e911e64e5413d2b1819299ca0d137c153603e30f577257e0de

See more details on using hashes here.

File details

Details for the file cartesian_plane-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for cartesian_plane-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2472311d17cbcf2a9579f1be8c230c33fa0b119bc319f4e6827bd25a302feff5
MD5 89975f6496327d41cbbd44f6ba6b41fe
BLAKE2b-256 7d8bea0d7532386e9d4a4220f85079248c532349a590da5af811a9aca055ea04

See more details on using hashes here.

Supported by

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