Author Topic: How to sort Line by its angle with the X axis in descending order.  (Read 3642 times)

0 Members and 1 Guest are viewing this topic.

waterharbin

  • Guest
Hello, I have a  List<Line>. I want to sort the lines by their angles with the X axis in descending order.Some lines'angle with the X axis are in range [0,90), and all the other lines' angle are in range [270,360). But I want the Lines in [0,90) to be in the front part and in descending order,the [270,360) should be in the back part and in descending order.  Here is all I can do:
Code: [Select]
public int sortLinesByAngle(Line a, Line b)
        {
            if (a.StartPoint.GetVectorTo(a.EndPoint).GetAngleTo(Vector3d.XAxis, Vector3d.ZAxis.Negate()) ==
                b.StartPoint.GetVectorTo(b.EndPoint).GetAngleTo(Vector3d.XAxis, Vector3d.ZAxis.Negate()))
            {
                //return a.StartPoint.Y.CompareTo(b.StartPoint.Y);
                return a.StartPoint.X.CompareTo(b.StartPoint.X);
            }           
            return b.StartPoint.GetVectorTo(b.EndPoint).GetAngleTo(Vector3d.XAxis, Vector3d.ZAxis.Negate()).CompareTo(
                   a.StartPoint.GetVectorTo(a.EndPoint).GetAngleTo(Vector3d.XAxis, Vector3d.ZAxis.Negate()));
        }
LinesList.Sort(sortLinesByAngle);
But it does not work!!! Anything wrong? How to change?
« Last Edit: October 12, 2011, 02:23:56 AM by waterharbin »

n.yuan

  • Bull Frog
  • Posts: 348
Re: How to sort Line by its angle with the X axis in descending order.
« Reply #1 on: October 12, 2011, 10:45:44 AM »
Unless you still target .NET2.0, you can use LINQ to do the sorting without bothering to implement IComparable interface by your own code, well, in most cases.

Assume you have a list of Line entities (List<Line>), you can easily sort the list of Line by any property of the Line entity:

List<Line> MyLines=[Obtain your lines and place them into a list];

MyLines.OrderBy(l=>l.Angle);
or
MyLines.OrderByDescending(l=>l.Angle);
or
var sortedLine=(from l in MyLines orderby l.Angle ascending select l).ToList()/ToArray();

hellios8502

  • Guest
Re: How to sort Line by its angle with the X axis in descending order.
« Reply #2 on: October 12, 2011, 11:15:14 AM »
I cant seem to understand your question. For example if Line1.x = 10 and Line2.x = 15, but Line1.angle=0.1 and Line2.angle = 0.05 should Line2 be in front of Line1?

P.S. Lines have a property named angle, which returns the line inclination angle.

waterharbin

  • Guest
Re: How to sort Line by its angle with the X axis in descending order.
« Reply #3 on: October 18, 2011, 11:04:57 PM »
Hello.I am sorry my English is bad. I give an example list here. Line1.Angle = Line2.Angle = 75,but Line1.StartPoint.X = 100,Line2.StartPoint.X = 200. Line3.Angle = Line4.Angle = 300,but Line3.StartPoint.X = 400,Line4.StartPoint.X = 500. And the order after sorting should be Line1,Line2,Line3,Line4.
The Line.Angle's definition is the vector'Angle with the X axis. the vector = Line.StartPoint->line.EndPoint.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: How to sort Line by its angle with the X axis in descending order.
« Reply #4 on: October 19, 2011, 01:03:20 AM »
Hi,

As said n.yuan, it's very easy using Linq :
Code: [Select]
LinesList.OrderBy(l => l.Angle).ThenBy(l => l.StartPoint.X)
Speaking English as a French Frog