Skip to main content

point2d

The point2d module contains only on class: Point2D.

This class describes Cartesian and polar coordinates for 2D points. It simultaneously represents the behavior of points (vectors) in a 2D space with Cartesian and polar coordinates, so that those can be used interchangeably. Changing one coordinate will change the value of the other, i.e., when changing a Cartesian coordinate, the polar coordinates will be recalculated, and vice-versa.

No methods need to be called to recalculate any of the coordinates, this is done automatically, e.g., if you set a new x, r and a will be updated.

Instalation

From this folder run python setup.py install.

Usage

You can import point2d and use the class via point2d.Point2D or from point2d import Point2D and use Point2D directly from your code.

Attributes

The Point2D class has Cartesian coordinates represented by x and y, and polar coordinates (radius and angle) represented by r and a. If you change the value of x or y, r and a will be updated automatically, and vice-versa.

Creating a Point2D

There are five ways of creating a Point2D:

  1. Without any arguments. This will initialize it as the point (0, 0).
>>> Point2D()
Point2D(0.0, 0.0)(0.0, 0.0)
  1. With x and y as separate arguments.
>>> Point2D(-1.0, 0.0)
Point2D(-1.0, 0.0)(1.0, 3.141592653589793)
  1. With x and y as a single tuple.
>>> Point2D((-1.0, 0.0))
Point2D(-1.0, 0.0)(1.0, 3.141592653589793)
  1. With r and a as separate arguments.
>>> Point2D(r=1.0, a=math.pi)
Point2D(-1.0, 1.2246467991473532e-16)(1.0, 3.141592653589793)
  1. Copying another Point2D:
>>> Point2D(Point2D(-1.0, 0.0))
Point2D(-1.0, 0.0)(1.0, 3.141592653589793)

Functionalities

As said earlier, the class automatically keeps track of cartesian and polar coordinates.

>>> p = Point2D()
>>> print(p) 
Point2D(0.0, 0.0)(0.0, 0.0)
>>> p.x = -1.0
>>> print(p)
Point2D(-1.0, 0.0)(1.0, 3.141592653589793)
>>> p.r = 5.0
>>> print(p)
Point2D(-5.0, 6.123233995736766e-16)(5.0, 3.141592653589793)

The class also implements operation between points (they return a new Point2D).

>>> p1 = Point2D(10.0, 0.0)
>>> p2 = Point2D(0.0, 10.0)
>>> p1 + p2
Point2D(10.0, 10.0)(14.142135623730951, 0.7853981633974483)
>>> p1 - p2
Point2D(10.0, -10.0)(14.142135623730951, -0.7853981633974483)

You can update a point through a sum or subtraction between points if you do it like this:

>>> p1 += p2
>>> p1
Point2D(10.0, 10.0)(14.142135623730951, 0.7853981633974483)
>>> p1 -= p2
>>> p1
Point2D(10.0, 0.0)(10.0, 0.0)

The class also implements operation between points and scalars.

>>> Point2D(1.0, 0.0) * 10
Point2D(10.0, 0.0)(10.0, 0.0)

Getting and setting

You can get and set values to the coordinates directly through their names (x, y, r, a), but there are some useful methods to help you on the way.

Cartesian Coordinates

The cartesian() method can set Cartesian coordinates or return them. You can set them with x and y as separate arguments, with a tuple containing xand y, or use this method to get a tuple containing x and y.

>>> p = Point2D()
>>> p.cartesian(1.0, 0.0)
>>> p
Point2D(0.0, 0.0)(0.0, 0.0)
>>> p.cartesian((-1.0, 0.0))
>>> p
Point2D(-1.0, 0.0)(1.0, 3.141592653589793)
>>> p.cartesian()
(-1.0, 0.0)

You can also use the method ints() to get a tuple of ints with the cartesian coordinates. This is especially useful if you're working with Pygame.

Polar Coordinates

The polar() method does the same thing for polar coordinates. You can set them with r and a as separate arguments, with a tuple containing rand a, or use this method to get a tuple containing r and a.

>>> p = Point2D()
>>> p.polar(1.0, 0.0)
>>> p
Point2D(1.0, 0.0)(1.0, 0.0)
>>> p.polar((1.0, math.pi))
>>> p
Point2D(-1.0, 1.2246467991473532e-16)(1.0, 3.141592653589793)
>>> p.polar()
(1.0, 3.141592653589793)

Useful Tricks

You can work solely with Cartesian coordinates with this class and it's still useful. Here are some nice "tricks".

Length of a vector

Remember that the radius of a point, in polar coordinates, is its distance to the origin, which is the same as the length of a vector located at the origin.

Use mypoint.r to find out its length.

>>> p = Point2D(3, 4)
>>> p.r
5.0

Distance between points

Subtracting two points will give you a third point. The length of this third point is the distance between the two first points. Use its radius to get the length.

>>> p1 = Point2D(10, 0)
>>> p2 = Point2D(10, 10)
>>> (p2 - p1).r
10.0

Angle between vectors

Again, subtracting two vectors will give you a third. The angle of this third vector is the angle between the two first vectors. Be careful, though, p1 - p2 is not the same as p2 - p1. Although they will yield supplementary angles (their sum is 180°).

>>> p1 = Point2D(10, 0)
>>> p2 = Point2D(10, 10)
>>> math.degrees((p2 - p1).a)
90.0
>>> math.degrees((p1 - p2).a)
-90.0

Scaling vectors

There are two ways to do this. One of them is to multiply the vector by the number that describes its scaling factor. The other way is multiplying its radius by the number.

>>> p = Point2D(1,0)
>>> p *= 3
>>> p
Point2D(3.0, 0.0)(3.0, 0.0)
>>> p = Point2D(1,0)
>>> p.r *= 3
>>> p
Point2D(3.0, 0.0)(3.0, 0.0)

Unit vector

The unit vector is a vector of magnitude 1. To find the unit vector of a vector, you need to multiply the vector by its length, which can be kind of annoying. Well, it's easy now. Just set its radius to 1 and it's done. You're welcome.

>>> p = Point2D(12.34, 56.78)
>>> p.r = 1
>>> p
Point2D(0.21237248410903914, 0.9771887883072318)(1.0, 1.3567941381565736)

Code: https://github.com/SplinterDev/point2d/

License: GNU General Public License v3.0

Created By: Fabrício J.C. Montenegro (2018)

Metadata

Release files for point2d 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for point2d 0.0.1
File Size Uploaded Provenance Actions
point2d-0.0.1.tar.gz 5.3 kB Without provenance Details for point2d-0.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for point2d 0.0.1
File Interpreter ABI Platform Actions
point2d-0.0.1-py3-none-any.whl Python 3 none any Details for point2d-0.0.1-py3-none-any.whl

Total release size: 23.0 kB

Release files / point2d-0.0.1.tar.gz

Download URL point2d-0.0.1.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
1c3a64b2dc3200e7a40eff846ebe76c5fb7dd8bc1f80a1e524cc7a9fdebc4997
BLAKE2b-256 checksum
How to use checksums
dc80b92cdaa3f0325214a9b45f5abda0b08740d38b1b2912bfbd45184bc4bdd4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.5.2

Release files / point2d-0.0.1-py3-none-any.whl

Download URL point2d-0.0.1-py3-none-any.whl
Size 17.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
192a5a1b1f5c628b08577574f8949ac18cbad7b647c1b5f0135e2071377f58f6
BLAKE2b-256 checksum
How to use checksums
386d4f55fc1fe656a90d360114e3ed72aff59166034c436e8512163dfb44fa42
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.5.2

Security for point2d 0.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page