Finding the center point between two points on a plane

Challenge:

To take two points on the plane and figure out the position of the point that's a) on the line that connects them, and b) equally distant from each of the original points.

Example:

(0,0), (3,3) -> (1.5,1.5)

(-1,-1), (1,1) -> (0,0)

(-7,3), (2,-2) -> (-2.5,0.5)

(3, 1), (-5,9) -> (-1,5)


Rules:

• The input and output can be a tuple, array, list, or any other type of sequence.
• There must be two inputs, each representing a point.
• The output has to support floating point numbers.

Shortest solution in bytes wins.

• Some more test cases would probably be a good idea. Maybe ((-2,3), (5,4)), ((-7,-5),(-7,-5)) would be a good start. – Jonathan Allan Apr 20 '17 at 21:52
• May input be two complex numbers? May output be a complex number? – Jonathan Allan Apr 20 '17 at 22:25
• Is there any relevance of the "on a plane" part? Any two arbitrary points share infinitely many planes, and the desired point is shared by all of them. – Julian Wolf Apr 20 '17 at 22:29
• @JulianWolf I take "point on a plane" to signify a 2d coordinate system. – Sparr Apr 20 '17 at 23:32
• @Sparr: that makes sense, thanks. I guess my mind treats "on a plane" as being distinct from "on the plane". (This is probably due to me not talking to enough actual mathematicians.) – Julian Wolf Apr 20 '17 at 23:34

Jelly, 7 2 bytes

-5 bytes (ha ha, yes I am an idiot) thanks to FryAmTheEggman!

+H


Takes two lists each of length d and returns one list of length d where d is the dimensions of the Cartesian space (question is posed for 2 dimensions, but should work for any d).

Try it online!

How?

+H - Main link: a, b (lists of coordinates)
+  - add (vectorises across the dimensions)
H - halve (vectorises across the result)


Mathematica, 4 bytes

Mean


Works on lists of vectors: for example, Mean[{{-7, 3}, {2, -2}}] returns {-5/2, 1/2}. (If the input consists of floating-point numbers, so will the output.)

Python 3, 39 35 bytes

lambda*x:[sum(a)/2for a in zip(*x)]


Try it online!

• If you place an * before the first x (for 0 bytes) it will actually take multiple (and variable) arguments rather than the single (wrapped) list. – Jonathan Allan Apr 20 '17 at 22:53
• @JonathanAllan Thanks! I didn't know that was possible, but it does look neater and more clean. – Adnan Apr 20 '17 at 23:11

zipWith$((/2).).(+)  Can be called with (zipWith$((/2).).(+)) (p1::[a]) (p2::[a]) where a is any type deriving Fractional. Works in arbitrary dimensions.

TI-BASIC, 8 bytes

.5(L₁+L₂


Takes input on List 1 and List 2. Works for all dimensions.

Julia 0.6, 13 bytes

(x->x/2)∘(+)


JavaScript , 30 bytes

valid for any number of dimensions

a=>b=>a.map((c,i)=>(c+b[i])/2)


Try it online!

R, 22 bytes

As an unnamed function that takes 2 vectors as parameters. Simply returns the mean of input vectors the same as a lot of answers.

function(a,b)((a+b)/2)


Try it online!

And 16 bytes using the pryr library

pryr::f((a+b)/2)


Common Lisp, 7054 49 bytes

Thanks to @Julian Wolf for helping to save a bunch of bytes!

(defun f(a b)(mapcar(lambda(c d)(/(+ c d)2))a b))


Try it online!

• car and cadr can shave a bunch of bytes off of those calls to nth. mapcar is also your friend. – Julian Wolf Apr 20 '17 at 22:42
• @JulianWolf Thanks! I don't really know Lisp, so that was of a lot of help. – Steadybox Apr 20 '17 at 23:23
• No problem! LISP is super duper awesome once you get the hang of it. The next step would be putting both operations into a single lambda—no need to call mapcar separately for addition and division. – Julian Wolf Apr 20 '17 at 23:32

Math.JS, 14 bytes

f(a,b)=(a+b)/2


Creates function f which takes 2 One Dimensional Matricies as input.

As expected, this just adds the two, then halfs the result.

Try it!

Python + NumPy, 18 bytes

lambda a,b:(a+b)/2


Works in two dimensions for points inputted as imaginary numbers, or (since OP specifies that inputs must be taken as sequences) in arbitrary dimensions for points inputted as NumPy arrays.

Try it online!

• Thanks for the TIO link, @Riker! I really ought to get into the habit of adding those. – Julian Wolf Apr 21 '17 at 2:56

Python + NumPy, 17 bytes

lambda a:sum(a)/2


A slightly less boring version of the other python answer. Takes args as 1 numpy array, containing both pairs.

Try it online!

• Does this not work in normal Python? – Cyoce May 8 '17 at 22:55
• @Cyoce Python doesn't support division across a vector. – Rɪᴋᴇʀ May 9 '17 at 1:23
• nvm. I see it now. – Cyoce May 9 '17 at 1:24

Julia, 13 bytes

a->b->(a+b)/2


Call with currying syntax, i.e. (a->b->(a+b)/2)([1,2])([3,4]).

• Shoot, didn't see that you'd beat me to this one – Julian Wolf Apr 21 '17 at 2:58
• @JulianWolf it's not a problem! Yours is actually a byte shorter, so you can definitely leave it up. I don't even have v0.6, so I think tha'ts pretty cool. – Rɪᴋᴇʀ Apr 21 '17 at 2:59
• Yeah, I'm definitely leaving the composition solution up (they seem sufficiently different); there's no need for the two equivalent v0.5 solutions to stay though – Julian Wolf Apr 21 '17 at 3:02
• Not familiar with Julia specifically, but could you save a byte with currying to a->b->(a+b)/2? – Cyoce May 8 '17 at 22:53
• @Cyoce yes, thanks! – Rɪᴋᴇʀ May 9 '17 at 1:20