javascript - Calculate the mid point of latitude and longitude co-ordinates -


does know best way go getting mid-point of pair of latitude , longitude points?

i mm using d3.js draw points on map , need draw curved line between 2 points, need create mid point draw curve in lines.

please see image below better understanding of trying do:

enter image description here

apologies long script - seemed fun draw stuff :-). i've marked off sections not required

// latitude / longitude var co2 = [70, 48]; var co1 = [-70, -28];   // not required var ctx = document.getelementbyid("mychart").getcontext("2d");  function drawpoint(color, point) {     ctx.fillstyle = color;     ctx.beginpath();     ctx.arc(point.x, point.y, 5, 0, 2 * math.pi, false);     ctx.fill(); }  function drawcircle(point, r) {     ctx.strokestyle = 'gray';     ctx.setlinedash([5, 5]);     ctx.beginpath();     ctx.arc(point.x, point.y, r, 0, 2 * math.pi, false);     ctx.stroke(); }   // required // convert cartesian var projection = d3.geo.equirectangular()  var cot1 = projection(co1); var cot2 = projection(co2);  var p0 = { x: cot1[0], y: cot1[1] }; var p1 = { x: cot2[0], y: cot2[1] };   // not required drawpoint('green', p0); drawpoint('green', p1);   // required function dfn(p0, p1) {     return math.pow(math.pow(p0.x - p1.x, 2) + math.pow(p0.y - p1.y, 2), 0.5); }  // http://math.stackexchange.com/a/87374 var d = dfn(p0, p1); var m = {     x: (p0.x + p1.x) / 2,     y: (p0.y + p1.y) / 2, }  var u = (p1.x - p0.x) / d var v = (p1.y - p0.y) / d;  // increase 1, if want larger curvature var r = d * 1; var h = math.pow(math.pow(r, 2) - math.pow(d, 2) / 4, 0.5);  // 2 possible centers var c1 = {     x: m.x - h * v,     y: m.y + h * u } var c2 = {     x: m.x + h * v,     y: m.y - h * u }   // not required drawpoint('gray', c1) drawpoint('gray', c2)  drawcircle(c1, r) drawcircle(c2, r)   // required  // http://math.stackexchange.com/a/919423 function mfn(p0, p1, c) {     // -c1 moving center 0 , again     var mt1 = {         x: r * (p0.x + p1.x - c.x * 2) / math.pow(math.pow(p0.x + p1.x - c.x * 2, 2) + math.pow(p0.y + p1.y - c.y * 2, 2), 0.5)     };     mt1.y = (p0.y + p1.y - c.y * 2) / (p0.x + p1.x - c.x * 2) * mt1.x;      var ma = {         x: mt1.x + c.x,         y: mt1.y + c.y,     }      var mb = {         x: -mt1.x + c.x,         y: -mt1.y + c.y,     }      return (dfn(ma, p0) < dfn(mb, p0)) ? ma : mb; }  var m1 = mfn(p0, p1, c1); var m2 = mfn(p0, p1, c2);  var mo1 = projection.invert([m1.x, m1.y]); var mo2 = projection.invert([m2.x, m2.y]);   // not required drawpoint('blue', m1); drawpoint('blue', m2);  // final output (in lat long) console.log(mo1); console.log(mo2); 

fiddle - https://jsfiddle.net/srjuc2gd/


enter image description here


and here's relevant portion (most of copy-pasta beginning of answer)

var q31428016 = (function () {      // adjust curvature     var curvature = 1;       // project convert lat / long cartesian     var projection = d3.geo.equirectangular();      // distance between p0 , p1     function dfn(p0, p1) {         return math.pow(math.pow(p0.x - p1.x, 2) + math.pow(p0.y - p1.y, 2), 0.5);     }      // mid point between p0 , p1     function cfn(p0, p1) {         return {             x: (p0.x + p1.x) / 2,             y: (p0.y + p1.y) / 2,         }     }      // arc midpoint given end points, center , radius - http://math.stackexchange.com/a/919423     function mfn(p0, p1, c, r) {          var m = cfn(p0, p1);          // -c1 moving center 0 , again         var mt1 = {             x: r * (m.x - c.x) / math.pow(math.pow(m.x - c.x, 2) + math.pow(m.y - c.y, 2), 0.5)         };         mt1.y = (m.y - c.y) / (m.x - c.x) * mt1.x;          var ma = {             x: mt1.x + c.x,             y: mt1.y + c.y,         }          var mb = {             x: -mt1.x + c.x,             y: -mt1.y + c.y,         }          return (dfn(ma, p0) < dfn(mb, p0)) ? ma : mb;     }      var q31428016 = {};     q31428016.convert = function (co1, co2) {          // convert cartesian         var cot1 = projection(co1);         var cot2 = projection(co2);          var p0 = { x: cot1[0], y: cot1[1] };         var p1 = { x: cot2[0], y: cot2[1] };           // center - http://math.stackexchange.com/a/87374         var d = dfn(p0, p1);         var m = cfn(p0, p1);          var u = (p1.x - p0.x) / d         var v = (p1.y - p0.y) / d;          var r = d * curvature;         var h = math.pow(math.pow(r, 2) - math.pow(d, 2) / 4, 0.5);          // 2 possible centers         var c1 = {             x: m.x - h * v,             y: m.y + h * u         }         var c2 = {             x: m.x + h * v,             y: m.y - h * u         }           // arc midpoints         var m1 = mfn(p0, p1, c1, r);         var m2 = mfn(p0, p1, c2, r);           // convert lat / long         var mo1 = projection.invert([m1.x, m1.y]);         var mo2 = projection.invert([m2.x, m2.y]);          return [mo1, mo2]     }      return q31428016; })();   // latitude / longitude var co1 = [-70, -28]; var co2 = [70, 48];  var mo = q31428016.convert(co1, co2)  // output console.log(mo[0]); console.log(mo[1]); 

Comments