We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Help sorting array of four 2d points - Wiimote Fun
Page Index Toggle Pages: 1
Help sorting array of four 2d points - Wiimote Fun (Read 884 times)
Help sorting array of four 2d points - Wiimote Fun
Nov 5th, 2008, 12:09am
 
I'm working on a "foldable display" style program to hopefully be used in live stage performances.  For those of you who aren't familiar here's a link to the lauded Johnny Lee and what I'm talking about.

http://www.youtube.com/watch?v=nhSR_6-Y5Kg

I'm setup with a wiimote, got my IR enabled bread board and I've got no problem pulling in my 4 IR points.  My issue is that whenever the wiimote loses track of more than one point it reaquires them in a different order.  So basically I need to setup a function that sorts the four sets of X,Y points and checks to make sure that it resorts if a point gets lost.

For the life of me I can not find a simple way to sort the four sets of points.  I know its possible, and I've seen a variety of people who've done it, but any help would be awesome.  Think basically that someone is randomly giving you the four points of a rectangle and you need to put them in order so you can draw lines between them in some kind of order.

I know there are sort commands for a single array of points but I haven't been able to sort pairs of points.  I thought about sorting based on the distance from the origin, but then I would end up with that value and I don't think I can simply get back to my X,Y coordinates.


Thanks,
Duncan
Re: Help sorting array of four 2d points - Wiimote
Reply #1 - Nov 5th, 2008, 7:27am
 
I recall a similar question was asked in StackOverflow: Sort Four Points in Clockwise Order. The solution isn't trivial... Smiley

It might be simpler if the rectangle you talk about have sides aligned to axes.
Re: Help sorting array of four 2d points - Wiimote
Reply #2 - Nov 5th, 2008, 2:57pm
 
an idea:

find the centre of the four points

work out the angles between a line from the centre going straight up (possibly involves atan2, most of these things do 8)

sort these angles so you have 4 points ordered clockwise around this centre.

simple case, points at (1,1), (-1,1), (-1,-1) and (1,-1)
origin obviously 0,0

x, y: atan2(x, y) (radians)
1, 1: 0.78539816339745
1, -1: 2.3561944901923
-1, 1: -0.78539816339745
-1, -1: -2.3561944901923

which gives the order (largest first)
(1, -1), (1, 1), (-1, 1), (-1, -1)

(which is the points sorted anticlockwise from bottom right)

any use?
Page Index Toggle Pages: 1