Skip to main content

A simple package for creating geometric objects

Project description

README

This project enables the creation of different geometric objects, on which we can perform different operations, like calculation of their perimeter.

Features

  • Create and visualise various geometric objects: Triangle, Rectangle, Polygon, Circle and Ellipse
  • Calculate their perimeter and their area
  • Check if two objects intersect
  • Save and load geometric objects to and from JSON files

Installation

Download the project with pip install SimpleShape and then call it in your python document with from SimpleShape import SimpleShape. Alternatively, download and expand the .zip (.tar.gz) file and insert the SimpleShape.py function, located in the SimpleShape subfolder, to location of your python document and then write import SimpleShape in it.

1. Instructions

To start using this project, create the Geometric object with available classes and perform different methods (see section 1.1). You can also check example.py, located in the SimpleShape subfolder, for a quick overwiev on how to use this package.

This package also provides various functions that are described in section 1.2.

1.1 Geometric Classes and Methods

Class Geometry(coordinates):

Parent class for creating geometric objects. Here, coordinates is 2-D array of arrays that represent points in x-y plane (one example being coordinates = [(0,0), (2,0), (4,4), (0,4)]). In case of Ellipse and Circle, different parameters are inserted (see below), for example the coordinates of their center.

Parent class has following methods:
  • __init__(self, coordinates): Initialization method for the object.
  • __str__(self) -> str: String representation of the object, obtained using print(object)
  • perimeter(self) -> float: Returns the perimeter of the geometric object
  • area(self) -> float: Returns the area of the geometric object
  • intersect(self, other) -> bool: Check if two objects intersect. Note that tangency also counts as intersection
  • visualize(self): Visualize the object using matplotlib module

Aside from these methods, there are various functions to convert geometric objects to and from JSON format. See section 1.2 for more details.

Class Triangle(coordinates):

A class for creating triangles using three points (f.e. coordinates = [(0,0), (2,0), (4,4)]).

Class Rectangle(coordinates):

A class for creating rectangles using four points that need to be inserted in clockwise or counter-clockwise order.

Class Polygon(coordinates):

A class for creating polygons using three or more points that need to be inserted in fixed order. If the polygon is self-intersecting, the .area() method does not return correct values.

Class Circle(r, center = (0, 0)):

A class for creating circles. Here, r is radius (r > 0) and center (default value (0,0)) represents center of the circle on x-y plane.

Class Ellipse(width, height, f = 0, center = (0, 0)):

A class for creating ellipses. Here, width is width of the ellipse, height is its height (width, height > 0), f is the rotation in counter-clockwise direction (in degrees, default value 0) and center represents center of ellipse on x-y plane (default value (0,0)).

1.2 Functions

This package contains functions for working with JSON data as well as other functions that perform different operations on geometric objects.

JSON functions:
  • to_json(object_list): Convert the list of objects to JSON string using object attributes
  • save_shapes(json_data, filename): Write JSON string to a .json file
  • load_shapes(filename): Create JSON string from a .json file
  • from_json(json_data): Create a list of objects from a JSON string
Other functions:
  • random_shape(shape): Create a geometric object of random dimensions. If the shape parameter is not specified, an arbitrary geometric object will be returned. Otherwise, insert one of the following as an argument: "Rectangle", "Triangle", "Circle", "Ellipse", "Polygon".
  • rotate(P1, P2, f): Rotate point P1 (x1,y1) around point P2 (x2,y2) for angle f in radians.
  • intersect(A,B,C,D): Check if two line segments (AB and CD, where each capital letter represents a point (x, y) on x-y plane) intersect
  • parallel(A,B,C,D): Check if two line segments (AB and CD, where each capital letter represents a point (x, y) on x-y plane) are parallel
  • Line_Intersect_Circle(A, B, c, r): Check if line segment AB intersect a circle with radius r > 0 and central point c (x, y).

Note: Tangency also counts as intersection!

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

SimpleShape-1.4.1.tar.gz (8.0 kB view details)

Uploaded Source

File details

Details for the file SimpleShape-1.4.1.tar.gz.

File metadata

  • Download URL: SimpleShape-1.4.1.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.9

File hashes

Hashes for SimpleShape-1.4.1.tar.gz
Algorithm Hash digest
SHA256 89d9d3d46dbe96009a1811877148653db7c9a0662f98c9d6bf43626fad703cf1
MD5 9e9e877cf7e65bc9670464bbf91f4217
BLAKE2b-256 18516f2c8988279e0aea929c202be1e73cd29d84e0525536b36efbed7cb113ad

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