How can I manipulate cartesian coordinates in Python? -


i have collection of basic cartesian coordinates , i'd manipulate them python. example, have following box (with coordinates show corners):

0,4---4,4

0,0---4,0

i'd able find row starts (0,2) , goes (4,2). need break each coordinate separate x , y values , use basic math, or there way process coordinates (x,y) pair? example, i'd say:

new_row_start_coordinate = (0,2) + (0,0) new_row_end_coordinate = new_row_start_coordinate + (0,4) 

sounds you're looking point class. here's simple one:

class point:   def __init__(self, x, y):     self.x, self.y = x, y    def __str__(self):     return "{}, {}".format(self.x, self.y)    def __neg__(self):     return point(-self.x, -self.y)    def __add__(self, point):     return point(self.x+point.x, self.y+point.y)    def __sub__(self, point):     return self + -point 

you can things this:

>>> p1 = point(1,1) >>> p2 = point(3,4) >>> print p1 + p2 4, 5 

you can add many other operations need. list of of methods can implement, see python docs.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -