4
$\begingroup$

Say I have a list of Lines:

lines = {Line[{{Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5])}, {Sqrt[
    5/8 + Sqrt[5]/8], 1/4 (-1 + Sqrt[5])}}], 
 Line[{{Sqrt[5/8 + Sqrt[5]/8], 1/4 (-1 + Sqrt[5])}, {0, 1}}], 
 Line[{{0, 1}, {-Sqrt[5/8 + Sqrt[5]/8], 1/4 (-1 + Sqrt[5])}}], 
 Line[{{-Sqrt[5/8 + Sqrt[5]/8], 
    1/4 (-1 + Sqrt[5])}, {-Sqrt[5/8 - Sqrt[5]/8], 
    1/4 (-1 - Sqrt[5])}}], 
 Line[{{-Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5])}, {Sqrt[
    5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5])}}], 
 Line[{{Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5])}, {0, 1}}], 
 Line[{{Sqrt[5/8 - Sqrt[5]/8], 
    1/4 (-1 - Sqrt[5])}, {-Sqrt[5/8 + Sqrt[5]/8], 
    1/4 (-1 + Sqrt[5])}}], 
 Line[{{Sqrt[5/8 - Sqrt[5]/8], 
    1/4 (-1 - Sqrt[5])}, {-Sqrt[5/8 - Sqrt[5]/8], 
    1/4 (-1 - Sqrt[5])}}]}

We can see that lines[[5]] and lines[[8]] are the same:

lines[[5]]

Line[{{-Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5])}, {Sqrt[ 5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5])}}]

lines[[8]]

Line[{{Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5])}, {-Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5])}}]

I tried to use Union and DeleteDuplicates to get rid of a duplicate, but none of them worked. Is it because they can't deal with Line environment? What are the alternatives?

$\endgroup$
  • $\begingroup$ they are not the same. There is a minus before Sqrt $\endgroup$ – J42161217 May 15 at 12:02
  • $\begingroup$ the order of the points in each line is different but the points are the same, i.e. the lines are the same $\endgroup$ – amator2357 May 15 at 12:05
6
$\begingroup$
DeleteDuplicatesBy[lines, Sort @@ # &]

or

DeleteDuplicates[Sort @@@ lines]

The key is to realize that we have to Apply Sort one level deeper because of the head Line.

$\endgroup$
  • $\begingroup$ I see. Thank you @Henrik ! $\endgroup$ – amator2357 May 15 at 12:18
  • $\begingroup$ You're welcome. $\endgroup$ – Henrik Schumacher May 15 at 12:19
5
$\begingroup$

Another possibility is to use RegionEqual:

Length @ lines
Length @ DeleteDuplicates[lines, RegionEqual]

8

7

If your lines had more than 2 segments, than using Sort to canonicalize wouldn't work.

$\endgroup$
4
$\begingroup$

It is because the arguments of the 5-th and 8-th terms go in different order. Indeed, let us take only the arguments:

arg = lines[[All, 1]];

and let us try to compare the arguments of the 5th and 8th terms:

arg[[5]] === arg[[8]]

(*  False  *)

Now let us sort the arguments:

newArg = Sort /@ lines[[All, 1]]

and let us check the 5th and 8th terms

newArg[[5]] === newArg[[8]]

(*  True  *)

Now the duplicates will be deleted, and here is the result:

Line /@ DeleteDuplicates[newArg]

Have fun!

$\endgroup$
  • $\begingroup$ Nice! Thank you! $\endgroup$ – amator2357 May 15 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.