Message Boards Message Boards

2 Replies
2 Total Likes
View groups...
Share this post:

SortBy does not do anything before Locator points are clicked on

Posted 10 years ago

Can you please help me with the following code (I am trying to order points in a counterclockwise way according their relative position to a fixed point):

  opts = SortBy[Rest[pts], 
    Arg[(Complex @@ #) - (Complex @@ pts[[1]])] &];
  Column[{Graphics[{Line[opts]}, PlotRange -> {{0, 45}, {0, 30}}, 
     ImageSize -> {450, 300}], pts, opts}]],
 {{pts, {{10, 10}, {35, 10}, {20, 20}, {20, 15}}}, Locator}]

After running it I get the output attached as Problem1.jpg

On the other hand, when I click on the point at (20,15), I get the output attached as Problem2.jpg

SortBy did not do anything until the Locator was clicked. And until I click on all the Locator points, the ordering is not correct. Can you tell me the reason for this, and how to fix it?

POSTED BY: Ferenc Beleznay
2 Replies
Posted 10 years ago

Yes indeed, thank you very much for your explanation. After reading your comment, I also tried changing the points from {10,10} to {10.,10.}, which also gave the correct ordering. SortBy did something afterall, just not what I was expecting. Thanks again.

POSTED BY: Ferenc Beleznay

The Manipulate / Locator stuff is mostly irrelevant to understand what's going on here.

Given exact input, your SortBy function returns exact expressions, which may be sorted according to their symbolic form, not their numeric value.

In[1]:= Arg[(Complex @@ #) - (Complex @@ {10, 10})] & /@ {{35, 10}, {20, 20}, {20, 15}}

Out[1]= {0, Pi/4, ArcTan[1/2]}

In[2]:= Sort[%]

Out[2]= {0, Pi/4, ArcTan[1/2]}

If you adjusted your SortBy function to always return real numbers, they would be sorted numerically.

In[3]:= N[Arg[(Complex @@ #) - (Complex @@ {10, 10})]] & /@ {{35, 10}, {20, 20}, {20, 15}}

Out[3]= {0., 0.785398, 0.463648}

In[4]:= Sort[%]

Out[4]= {0., 0.463648, 0.785398}

After dragging a locator, the corresponding value in pts goes from being exact to being real, so the result of applying the SortBy function to that point will be inexact, and thus be sorted numerically.

POSTED BY: Lou D'Andria
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract