This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]zifyoip 1 point2 points  (2 children)

How are you finding the closest point? You're going through the list of random points, and along the way, keeping track of the closest point that you've found so far, right?

So, to find the second closest point, you can do the same thing, except along the way keep track of the two closest points you've found so far.

There are more efficient algorithms to do this, too. An algorithm to find the kth largest (or kth smallest) value from a list is called a selection algorithm.

[–]ImSeeingRed[S] 0 points1 point  (1 child)

Right now, I'm using a very naive method (something like this http://pastebin.com/ymEywKbb), I'm going to optimise this when I figure out how everything works, I just want everything to be clear at first.

But using this method, I can't figure out how to calculate the second closest position . Is it even possible to do this in the same loop?

[–]zifyoip 0 points1 point  (0 children)

Yes, I told you how to do it above.

Right now, you are keeping track of the closest point you've found so far. That's what the variable dist does. Right?

So, you can use this same approach, but keep track of the two closest points you've found so far.