How to write a function which calculate the midpoint between 2 points in python?
def midpoint(p1, p2):
"""
PRE: p1 and p2 are Point objects (from the graphics module)
POST: a new Point equidistant f开发者_高级运维rom and co-linear with p1 and p2
is computed and returned
Write a function midpoint with the following specification
What graphics module are you using? (pypi contains several dozen, none named 'graphics') What does the interface of a Point look like?
If Point has named attributes (like p.x, p.y, etc) you could do something like
def midpoint(p1, p2):
return Point((p1.x+p2.x)/2, (p1.y+p2.y)/2)
If Point can be accessed as a list (like p[0], p[1], etc) you could instead do
def midpoint(p1, p2):
return Point((p1[0]+p2[0])/2, (p1[1]+p2[1])/2)
If Point has Point addition and scalar division or multiplication overloaded, you could do
def midpoint(p1, p2):
return (p1+p2)/2 # or *0.5
(although strictly speaking adding two Points should be meaningless, and subtracting one point from another should give you a Vector - thus
def midpoint(p1, p2):
return p1 + (p2-p1)/2 # or *0.5
You'd write it the same way you write any other function:
def midpoint(x1, y1, x2, y2):
return ((x1 + x2)/2, (y1 + y2)/2)
or something like that. It depends on whether you represent points as individual coordinates or objects or lists etc.
Calculate them yourself?
The midway point between two points is their average. That is,
P_mid = (P1 + P2) / 2
It's up to you how a "point" is represented. It could be 2D, 3D, or even nD. You might want to implement __add__
and __div__
and other "numerical" operations.
How about using Numpy?
If the points are represented by numpy.array, their dimension doesn't matter.
For example, consider 2D.
import numpy as np
x = np.array([-1, -1])
y = np.array([1, 1])
mid1 = (x + y)/2
# or
mid2 = np.mean([x, y])
NumPy is the fundamental package needed for scientific computing with Python.
visit Link
I am assuming the question is about "Point" objects in the module named graphics
.
As per the documentation I am providing in the reference, the Point class has getX()
and `getY() accessors to fetch the X and Y coordinates of the point respectively.
So the solution could be like this
def midpoint(p1, p2):
mx = (p1.getX() + p2.getX())/2.0
my = (p1.getY() + p2.getY())/2.0
return Point(mx,my)
or
def midpoint(p1,p2):
l = Line(p1,p2)
return l.getCenter()
Reference- http://mcsp.wartburg.edu/zelle/python/graphics/graphics.pdf
from __future__ import division
def midpoint(p1, p2):
if len(p1) != len(p2):
raise TypeError('Points are from differing dimensions')
return map(lambda (e1, e2): (e1 + e2) / 2, zip(p1, p2))
Update0
Since it seems nobody actually answered the question in the form you wanted (except me ;)
), here's the simplified version for 2 dimensional points:
def midpoint(p1, p2):
return (p1[0] + p2[0]) / 2, (p1[1] + p2[1]) / 2
I've noticed that the answers so far have involved averaging. Here are two somewhat novel approaches (based on Prof Daniel Lemire's article) using bitwise operations that take advantage of the fact the point will be integers. If you're using floating point math, these won't work.
def midpoint (x, y):
return ((x^y)>>1) + (x&y)
def midpoint2 (x, y):
return (x|y) - ((x^y)>>1)
精彩评论