I’m trying to work with a given list of 10 two-item lists, or a list of coordinates if you will. For instance:
set list1 to {{0, 0}, {-1, 0}, {-2, 0}, {-2, -1}, {-1, -1}, {0, -1}, {0, -2}, {-1, -2}, {-2, -2}, {-3, -2}}
If you plot these with the Cartesian system, you get this:
What I want a script to be able to do is create the following from the previous list, or another given list of adjacent coordinates:
set list 2 to {{{-2, 0}, {-1, 0}, {0, 0}}, {{-2, -1}, {-1, -1}, {0, -1}}, {{-3, -2}, {-2, -2}, {-1, -2}, {0, -2}}}
list2 contains the same coordinates as list1 except they are categorized by their Y value, in descending order, then again by their X value in ascending order.
So I want the script to find the coordinate with the largest Y value, then put it and all coordinates with the same Y value in a list, ordered from their smallest X value to their largest X value. Then the script finds the next highest Y value and repeats the process. At the end, the script puts all those lists into one overarching list, in descending Y value order.
I made a stab at this but I couldn’t do it. Here’s what I got, but it doesn’t work. If anyone has any ideas or tips, I would be exceedingly grateful.
--A list of 10 adjacent X/Y coordinates
set list1 to {{0, 0}, {-1, 0}, {-2, 0}, {-2, -1}, {-1, -1}, {0, -1}, {0, -2}, {-1, -2}, {-2, -2}, {-3, -2}}
--placeholder for largest item 2 (Y value) among the coordinates in list1
set yMargin to ""
--For new lists
set list2 to {}
set list3 to {}
--returns a new list made from the fromList, excluding items it finds in the toList
on updateListItems(fromList, toList)
set newList to {}
repeat with i in fromList
if i is not toList then
copy i to end of newList
end if
end repeat
return newList
end updateListItems
--sets yMargin to the highest item 2 (Y value) of the items in list1
repeat with i in list1
set yValue to (item 2 of i)
if yMargin is "" then
set yMargin to yValue
else if yValue is greater than yMargin then
set yMargin to yValue
end if
end repeat
--all coordinates in list1 whose item 2 is equal to yMargin are copied into list2
repeat with i in list1
if (item 2 of i) is yMargin then
copy i to end of list2
end if
end repeat
--all items that are now in list2 are deleted from list1
set list1 to updateListItems(list1, list2)
--reorders items in list2 by putting them into list3 in order of smallest X value to largest X value
repeat (count list2) times
--the smallest item 1 (X value) among the coordinates in list2
set xMargin to ""
repeat with i in list2
set xValue to (item 1 of i)
if xMargin is "" then
set xMargin to xValue
set leastX to i
else if xValue is less than xMargin then
set xMargin to xValue
set leastX to i
end if
end repeat
copy leastX to end of list3
--remove items copied to list3 from list2
set list2 to updateListItems(list2, list3)
end repeat
return list3
Model: MacBook Pro 15"
AppleScript: AppleScript 2.3.1
Browser: Safari 537.74.9
Operating System: Mac OS X (10.8)