Finding the center point between two points on a plane

-3

0

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.

Caleb Kleveter

Posted 2017-04-20T21:43:34.000

Reputation: 647

6Some 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 – 2017-04-20T21:52:55.380

2May input be two complex numbers? May output be a complex number? – Jonathan Allan – 2017-04-20T22:25:12.893

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 – 2017-04-20T22:29:55.843

2@JulianWolf I take "point on a plane" to signify a 2d coordinate system. – Sparr – 2017-04-20T23:32:00.150

@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 – 2017-04-20T23:34:45.187

contrast to "point on a line" or "point in a volume/space" – Sparr – 2017-04-20T23:37:53.900

Can we take input as a list containing both pairs? – Rɪᴋᴇʀ – 2017-04-21T02:55:40.273

@Riker. No, there must be two input values, each representing a single point. – Caleb Kleveter – 2017-04-21T12:29:04.610

Answers

4

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)

Jonathan Allan

Posted 2017-04-20T21:43:34.000

Reputation: 67 804

4

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.)

Greg Martin

Posted 2017-04-20T21:43:34.000

Reputation: 13 940

2

Python 3, 39 35 bytes

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

Try it online!

Adnan

Posted 2017-04-20T21:43:34.000

Reputation: 41 965

1If 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 – 2017-04-20T22:53:34.813

@JonathanAllan Thanks! I didn't know that was possible, but it does look neater and more clean. – Adnan – 2017-04-20T23:11:04.667

1

Haskell, 22 19 bytes

zipWith$((/2).).(+)

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

Julian Wolf

Posted 2017-04-20T21:43:34.000

Reputation: 1 139

1

TI-BASIC, 8 bytes

.5(L₁+L₂

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

Scott Milner

Posted 2017-04-20T21:43:34.000

Reputation: 1 806

1

Julia 0.6, 13 bytes

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

Julian Wolf

Posted 2017-04-20T21:43:34.000

Reputation: 1 139

0

Common Lisp, 70 54 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!

Steadybox

Posted 2017-04-20T21:43:34.000

Reputation: 15 798

1car and cadr can shave a bunch of bytes off of those calls to nth. mapcar is also your friend. – Julian Wolf – 2017-04-20T22:42:35.797

@JulianWolf Thanks! I don't really know Lisp, so that was of a lot of help. – Steadybox – 2017-04-20T23:23:57.423

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 – 2017-04-20T23:32:40.483

0

JavaScript , 30 bytes

valid for any number of dimensions

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

Try it online!

fəˈnɛtɪk

Posted 2017-04-20T21:43:34.000

Reputation: 4 166

0

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)

MickyT

Posted 2017-04-20T21:43:34.000

Reputation: 11 735

0

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!

Julian Wolf

Posted 2017-04-20T21:43:34.000

Reputation: 1 139

Thanks for the TIO link, @Riker! I really ought to get into the habit of adding those. – Julian Wolf – 2017-04-21T02:56:29.163

0

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!

ATaco

Posted 2017-04-20T21:43:34.000

Reputation: 7 898

0

Julia, 13 bytes

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

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

Rɪᴋᴇʀ

Posted 2017-04-20T21:43:34.000

Reputation: 7 410

Shoot, didn't see that you'd beat me to this one – Julian Wolf – 2017-04-21T02:58:29.750

@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ɪᴋᴇʀ – 2017-04-21T02:59:33.610

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 – 2017-04-21T03:02:43.657

Not familiar with Julia specifically, but could you save a byte with currying to a->b->(a+b)/2? – Cyoce – 2017-05-08T22:53:14.980

@Cyoce yes, thanks! – Rɪᴋᴇʀ – 2017-05-09T01:20:52.257

0

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!

Rɪᴋᴇʀ

Posted 2017-04-20T21:43:34.000

Reputation: 7 410

Does this not work in normal Python? – Cyoce – 2017-05-08T22:55:49.433

@Cyoce Python doesn't support division across a vector. – Rɪᴋᴇʀ – 2017-05-09T01:23:18.650

nvm. I see it now. – Cyoce – 2017-05-09T01:24:45.137