How to write a function which calculate the midpoint between 2 points in python? -


def midpoint(p1, p2): """ pre: p1 , p2 point objects (from graphics module) post: new point equidistant , co-linear p1 , p2 computed , returned 

write function midpoint following specification

what graphics module using? (pypi contains several dozen, none named 'graphics') interface of point like?

if point has named attributes (like p.x, p.y, etc) like

def midpoint(p1, p2):     return point((p1.x+p2.x)/2, (p1.y+p2.y)/2) 

if point can accessed list (like p[0], p[1], etc) instead do

def midpoint(p1, p2):     return point((p1[0]+p2[0])/2, (p1[1]+p2[1])/2) 

if point has point addition , scalar division or multiplication overloaded, do

def midpoint(p1, p2):     return (p1+p2)/2     # or *0.5 

(although strictly speaking adding 2 points should meaningless, , subtracting 1 point should give vector - thus

def midpoint(p1, p2):     return p1 + (p2-p1)/2     # or *0.5 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -