Note that there are some explanatory texts on larger screens.

plurals
  1. POAdvanced Sorting Algorithm
    primarykey
    data
    text
    <p>I'm writing an app in Javascript that uses Google maps and calculates a full route between a series of legs based off closest legs.</p> <p>Say I've got 4 legs on the map, each leg has a start and end Latitude and Longitude. The user specifies which two legs are the start and end of the route. Each end of a leg can only connect to another legs start. Then the end of that leg connects to another legs start, and so on. The code determines which legs start to connect to based off the closest leg it can find.</p> <p>Something like this for example (the dashed lines are the legs):</p> <pre><code>start-----------end &lt;-connector-&gt; start----------end &lt;-connector-&gt; start----------end </code></pre> <p>I've got an array of all of the leg coordinates, and I want to sort this array so that it follows the proper progression of connections. Then I can utilize the array to generate the connectors by linearly looping through it.</p> <p>The array looks something like this:</p> <pre><code>[ {start_lat: X, start_lng: X, end_lat: X, end_lng: X}, {start_lat: X, start_lng: X, end_lat: X, end_lng: X}, ] </code></pre> <p>Those will be the inner legs. And then I'll have the outer legs (the two legs that are the start and the end of the entire route) stored in variables:</p> <pre><code>var start = {end_lat: X, end_lng: X} var end = {start_lat: X, start_lng: X} </code></pre> <p>As an example it might end up something like:</p> <pre><code>start -&gt; array[0] -&gt; array[1] -&gt; end </code></pre> <p>Or it might end up like:</p> <pre><code>start -&gt; array[1] -&gt; array[0] -&gt; end </code></pre> <p>The algorithm needs to sort the array based off the start legs end_lat,end_lng and the end legs end_lat,end_lng.</p> <p>The end result will be a big route connected together with the shortest path.</p> <p>I'm struggling to think of a way of writing the sorting algorithm that takes these unique factors into consideration.</p> <p>It's difficult to put this into words, and I'm not really sure how I can help make it clearer, but I'll edit this post if I can think of anything useful to add. Thanks.</p> <p><strong>Edit:</strong> </p> <p>Here's a picture of what I'm talking about: </p> <p><img src="https://i.imgur.com/k8VZenS.png" alt="map"></p> <p>The black lines are the legs, the red lines are the connectors I will need to generate after I've sorted the array of leg coordinates into the correct order. The generating the connectors isn't part of this algorithm, but it's just an example of what I'm trying to accomplish so you can understand the big picture. As you can see there are gaps between the legs, none of the coordinates overlap.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload