Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8574477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:29:00+00:00 2026-06-11T19:29:00+00:00

I have an array of points, where each point is a coordinate on a

  • 0

I have an array of points, where each point is a coordinate on a map. I’d like to make it so that when I add a point to the map, it’s added to my array in between the closest two points.

Additionally, I’d like to render the points, so that there are never any crossovers between the different points. New points should be added to the outside edge of the resulting polygon, and connected to the closest two.

Is there an algorithm for doing this?

Edit:

For clarity, I have the following screenshot. I’d like to achieve method B:

enter image description here

Edit 2:

Here’s a bunch of code I wrote to try and solve the problem. Assume that MBCoordinates are just that, coordinates:

//
//  Detect which coordinate is the closest to the supplied coordinate
//

- (void) insertCoordinateBetweenClosestNeighbors:(MBCoordinate *)coordinate{

if (![self points] || [[self points] count] < 3) {
    [[self points] addObject:coordinate];
    return;
}

NSMutableSet *pointSet = [NSMutableSet setWithArray:[self points]];

MBCoordinate *closestCoordinate = [self closestCoordinateToCoordinate:coordinate inSet:pointSet];
[pointSet removeObject:closestCoordinate];

MBCoordinate *nextClosestCoordinate = [self closestCoordinateToCoordinate:coordinate inSet:pointSet];

NSUInteger indexOfClosestCoordinate, indexOfSecondClosestCoordinate, insertionIndex;


for (NSUInteger i=0; i < [[self points] count]; i++) {

    if ([[self points][i] isEqual:closestCoordinate]) {
        indexOfClosestCoordinate = i;
    }

    if ([[self points][i] isEqual:nextClosestCoordinate]) {
        indexOfSecondClosestCoordinate = i;
    }
}

if(indexOfSecondClosestCoordinate == indexOfClosestCoordinate-1){
    insertionIndex = indexOfSecondClosestCoordinate+1;
}else{
    insertionIndex = indexOfClosestCoordinate+1;
}

[[self points] insertObject:coordinate atIndex:insertionIndex];

 /*

 Not in use in my program, but an alternate attempt:
[[self points] addObject:coordinate];
[self sortPointsByDistance];
 */
}

- (void) sortPointsByDistance{

//
//  Points that need sorting
//

NSMutableSet *unprocessedPoints = [NSMutableSet setWithArray:[self points]];

//
//  All of the unsorted points
//

NSMutableSet *unsortedPoints = [NSMutableSet setWithArray:[self points]];

//
//  The unsorted points minus the closest one
//

NSMutableSet *unsortedPointsExceptClosest = [NSMutableSet setWithArray:[self points]];

//
//  We put the point into here in the correct order
//

NSMutableArray *sortedPoints = [@[] mutableCopy];


//
//  Prime the pump
//

MBCoordinate *workingCoordinate = [self points][0];
[sortedPoints addObject:workingCoordinate];
[unprocessedPoints removeObject:workingCoordinate];

while([unprocessedPoints count] > 0){

    MBCoordinate *closestCoordinate = [self closestCoordinateToCoordinate:workingCoordinate inSet:unsortedPoints];
    MBCoordinate *secondClosestCoordinate = nil;

    //
    //  The closest point might be sorted already!
    //
    //  If it is, then we have to find the closest point.
    //

    if ([sortedPoints containsObject:closestCoordinate]) {

        NSInteger indexOfClosest = [sortedPoints indexOfObject:closestCoordinate];
        NSInteger indexOfSecondClosest = indexOfClosest;
        NSInteger targetIndex = indexOfClosest+1;

        if (!secondClosestCoordinate) {
            [unsortedPoints removeObject:closestCoordinate];
            secondClosestCoordinate = [self closestCoordinateToCoordinate:workingCoordinate inSet:unsortedPointsExceptClosest];

            if ([sortedPoints containsObject:secondClosestCoordinate]) {

                //
                //  Insert between the two points
                //

                indexOfSecondClosest = [sortedPoints indexOfObject:secondClosestCoordinate];

            }
        }

        if (indexOfSecondClosest < indexOfClosest) {
            targetIndex = indexOfSecondClosest + 1;
        }

        [sortedPoints insertObject:workingCoordinate atIndex:targetIndex];
        workingCoordinate = [unprocessedPoints anyObject];

        break;

    }else{
        workingCoordinate = closestCoordinate;
    }

    [sortedPoints addObject:workingCoordinate];
    [unprocessedPoints removeObject:workingCoordinate];
    unsortedPointsExceptClosest = [unsortedPoints copy];
    secondClosestCoordinate = nil;

}

[self setPoints:sortedPoints];
}

- (MBCoordinate *) closestCoordinateToCoordinate:(MBCoordinate *)coordinate inSet:(NSSet *)aSet{

MBCoordinate *closest = nil;
CGFloat closestDistance;

for (MBCoordinate *coordinateInSet in aSet) {

    if ([coordinateInSet isEqual:coordinate]) {
        continue;
    }

    if (!closest) {
        closest = coordinateInSet;
        closestDistance = [self distanceBetweenCoordinate:coordinate andCoordinate:coordinateInSet];
    }

    CGFloat distanceBetweenPoints = [self distanceBetweenCoordinate:coordinate andCoordinate:coordinateInSet];

    if (distanceBetweenPoints < closestDistance) {
        closest = coordinateInSet;
        closestDistance = distanceBetweenPoints;
    }


}

return closest;
}

//
//  Determines the distance between two coordinates
//

- (CGFloat) distanceBetweenCoordinate:(MBCoordinate *)coordinate andCoordinate:(MBCoordinate *)anotherCoordinate{

CGFloat xDistance, yDistance;

xDistance = coordinate.latitude-anotherCoordinate.latitude;
yDistance = coordinate.longitude-anotherCoordinate.longitude;

float distance = xDistance/yDistance;

//
//  Absolute value of floats...
//


if (distance < 0) {
    distance *= -1;
}

return distance;

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T19:29:01+00:00Added an answer on June 11, 2026 at 7:29 pm

    Well, I don’t know how your coordinate system works, but what you want to do is calculate the Euclidean Distance between your new point and the other points in the array. Then just insert your new point between the two that have the shortest euclidean distances to your new point.

    Just keep in mind that the surface of the Earth is not flat. The Euclidean distance linked above assumes flatness in two dimensions. So the farther apart your points are, the less accurate that formula will be.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say you have 100000000 32-bit floating point values in an array, and each of
We have an array of points through which we would like to draw a
I have a string like this: TEST.DATA.Data.COR.Point,2;TEST.DATA.Data.COR.Point,5;TEST.DATA.Data.COR.Point,12;TEST.DATA.Data.COR.Point,12;TEST.DATA.Data.COR.WordTOFIND,18 I have a list of array with
I have an array of point data, the values of points are represented as
I have an array of points in unknown dimensional space, such as: data=numpy.array( [[
Possible Duplicate: checking if pointer points within an array If I have an array
I have a very simple array (please focus on the object with "points.bean.pointsBase" as
I have created an array in php that prints this Array ( [mark] =>
I have the following class: #include <array> template<unsigned short D> class Point { private:
I use google maps API to display markers on a map. Each marker points

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.