Finding corners from Line arrays I have the following code that takes input of Line arrays and attempts to find the intersections on the end points between them. The value _nConrerTolerance is an int of a value of around 10% of the average Line length to allow the lines to not need to be exactly on each other. Now obvisouly this code will be quite slow, especially as I have similar methods for top left, bottom left and bottom right corners. Can anyone suggest a way to improve this?

Also if you have any tips on how I can improve my coding style, if needed, that would be great.

private Point[] FindTopRightCorners(Line[] horizontalLines, Line[] verticalLines)
{
    List&lt;Point&gt; TopRightCorners = new List&lt;Point&gt;();

    Line[] laHorizontalLines = horizontalLines.OrderByDescending(l =&gt; l.EndPoint.X).ThenBy(l =&gt; l.EndPoint.Y).ToArray();
    Line[] laVerticalLines = verticalLines.OrderByDescending(l =&gt; l.StartPoint.X).ThenBy(l =&gt; l.StartPoint.Y).ToArray();

    foreach (Line verticalLine in laVerticalLines)
    {
        foreach (Line horizontalLine in laHorizontalLines)
        {
            if (verticalLine.StartPoint.X &gt;= (horizontalLine.EndPoint.X - _nCornerTolerance) &amp;&amp; verticalLine.StartPoint.X &lt;= (horizontalLine.EndPoint.X + _nCornerTolerance))
            {
                if (horizontalLine.EndPoint.Y &lt;= (verticalLine.StartPoint.Y + _nCornerTolerance) &amp;&amp; horizontalLine.EndPoint.Y &gt;= (verticalLine.StartPoint.Y - _nCornerTolerance))
                {
                    int nX = Math.Abs((verticalLine.StartPoint.X + horizontalLine.EndPoint.X) / 2);
                    int nY = Math.Abs((verticalLine.StartPoint.Y + horizontalLine.EndPoint.Y) / 2);

                    TopRightCorners.Add(new Point(nX, nY));
                    break;
                }
            }
        }
    }

    return TopRightCorners.Distinct().ToArray();
}


And the Line class is defined as follows:

public class Line
{
    public Point StartPoint { get; private set; }

    public Point EndPoint { get; private set; }

    public Line(Point startPoint, Point endPoint)
    {
        this.StartPoint = startPoint;
        this.EndPoint = endPoint;
    }
}
You have similar code and similar performance issue in your other question about points instead of lines.  I answered that one here. Some of my answers can apply here particularly as to the repeated use of .ToArray()?

If you skipped everything as arrays and went with IEnumerable&lt;Line&gt; or IEnumerable&lt;Point&gt; for inputs, output, and anything inbetween then you might see some performance benefit.  Might.  Don't know until you run it.  But sounds like you are talking about big collections.

If you try using IEnumerable and things are still slow, I'd try to use Parallel.For with a range partitioner.

That should address your major concern.  On a minor concern, the Line class is good enough and looks like something I once wrote.  I eventually switched from StartPoint and EndPoint to Point1 and Point2 since some readers may think StartPoint should somehow be 'less than' when compared to EndPoint or that it should be drawn like a vector that somehow starts at StartPoint and is then drawn to EndPoint.  To avoid such needless notions, I went with the generic Point1 and Point2.