Logo

Programming-Idioms

History of Idiom 179 > diff from v8 to v9

Edit summary for version 9 by random:
New Python implementation by user [random]

Version 8

2019-09-26, 13:33:57

Version 9

2019-09-26, 13:37:07

Idiom #179 Get center of a rectangle

Return the center c of the rectangle with coördinates(x1,y1,x2,y2)

Idiom #179 Get center of a rectangle

Return the center c of the rectangle with coördinates(x1,y1,x2,y2)

Imports
from collections import namedtuple
Code
Point = namedtuple('Point', 'x y')
center = Point((x1+x2)/2, (y1+y2)/2)
Comments bubble
center is a namedtuple, that can be accessed either using x and y or an index (0,1)

e.g. center.x or center[0]
Doc URL
https://docs.python.org/2/library/collections.html#collections.namedtuple