Package: point2d
point2dThepoint2dmodule 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 newx,randawill be updated.InstalationFrom this folder runpython setup.py install.UsageYou canimport point2dand use the class viapoint2d.Point2Dorfrom point2d import Point2Dand usePoint2Ddirectly from your code.AttributesThePoint2Dclass has Cartesian coordinates represented byxandy, and polar coordinates (radius and angle) represented byranda. If you change the value ofxory,randawill be updated automatically, and vice-versa.Creating a Point2DThere are five ways of creating a Point2D:Without any arguments. This will initialize it as the point (0, 0).>>>Point2D()Point2D(0.0,0.0)(0.0,0.0)Withxandyas separate arguments.>>>Point2D(-1.0,0.0)Point2D(-1.0,0.0)(1.0,3.141592653589793)Withxandyas a single tuple.>>>Point2D((-1.0,0.0))Point2D(-1.0,0.0)(1.0,3.141592653589793)Withrandaas separate arguments.>>>Point2D(r=1.0,a=math.pi)Point2D(-1.0,1.2246467991473532e-16)(1.0,3.141592653589793)Copying another Point2D:>>>Point2D(Point2D(-1.0,0.0))Point2D(-1.0,0.0)(1.0,3.141592653589793)FunctionalitiesAs 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+p2Point2D(10.0,10.0)(14.142135623730951,0.7853981633974483)>>>p1-p2Point2D(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>>>p1Point2D(10.0,10.0)(14.142135623730951,0.7853981633974483)>>>p1-=p2>>>p1Point2D(10.0,0.0)(10.0,0.0)The class also implements operation between points and scalars.>>>Point2D(1.0,0.0)*10Point2D(10.0,0.0)(10.0,0.0)Getting and settingYou 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 CoordinatesThecartesian()method can set Cartesian coordinates or return them. You can set them withxandyas separate arguments, with a tuple containingxandy, or use this method to get a tuple containingxandy.>>>p=Point2D()>>>p.cartesian(1.0,0.0)>>>pPoint2D(0.0,0.0)(0.0,0.0)>>>p.cartesian((-1.0,0.0))>>>pPoint2D(-1.0,0.0)(1.0,3.141592653589793)>>>p.cartesian()(-1.0,0.0)You can also use the methodints()to get a tuple of ints with the cartesian coordinates. This is especially useful if you're working with Pygame.Polar CoordinatesThepolar()method does the same thing for polar coordinates. You can set them withrandaas separate arguments, with a tuple containingranda, or use this method to get a tuple containingranda.>>>p=Point2D()>>>p.polar(1.0,0.0)>>>pPoint2D(1.0,0.0)(1.0,0.0)>>>p.polar((1.0,math.pi))>>>pPoint2D(-1.0,1.2246467991473532e-16)(1.0,3.141592653589793)>>>p.polar()(1.0,3.141592653589793)Useful TricksYou can work solely with Cartesian coordinates with this class and it's still useful. Here are some nice "tricks".Length of a vectorRemember 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.Usemypoint.rto find out its length.>>>p=Point2D(3,4)>>>p.r5.0Distance between pointsSubtracting 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).r10.0Angle between vectorsAgain, 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 - p2is not the same asp2 - 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.0Scaling vectorsThere 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>>>pPoint2D(3.0,0.0)(3.0,0.0)>>>p=Point2D(1,0)>>>p.r*=3>>>pPoint2D(3.0,0.0)(3.0,0.0)Unit vectorThe 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>>>pPoint2D(0.21237248410903914,0.9771887883072318)(1.0,1.3567941381565736)LinksCode:https://github.com/SplinterDev/point2d/License:GNU General Public License v3.0Created By:Fabrício J.C. Montenegro(2018)