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 and expand the zip (.tar.gz) file and insert the SimpleShape.py function, located in the SimpleShape subfolder, to location of your python document, or use pip install SimpleShape. Then, write import SimpleShape in your python document to begin using the package.

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(a, b, f = 0, center = (0, 0)):

A class for creating ellipses. Here, a is the width of the ellipse, b is its height (a,b > 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.3.1.tar.gz (7.7 kB view details)

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for SimpleShape-1.3.1.tar.gz
Algorithm Hash digest
SHA256 95ab6178997e65721f86696eabd5a71847fe3e71a8be0248109eea45a1d53443
MD5 035be28e648964441d6fdf0ade608d6a
BLAKE2b-256 2986bc6df6b035c5d5f32e6268149694f0e7bb8e25ec92c66c8215d8ca4d8a89

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