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<Point> TopRightCorners = new List<Point>();
Line[] laHorizontalLines = horizontalLines.OrderByDescending(l => l.EndPoint.X).ThenBy(l => l.EndPoint.Y).ToArray();
Line[] laVerticalLines = verticalLines.OrderByDescending(l => l.StartPoint.X).ThenBy(l => l.StartPoint.Y).ToArray();
foreach (Line verticalLine in laVerticalLines)
{
foreach (Line horizontalLine in laHorizontalLines)
{
if (verticalLine.StartPoint.X >= (horizontalLine.EndPoint.X - _nCornerTolerance) && verticalLine.StartPoint.X <= (horizontalLine.EndPoint.X + _nCornerTolerance))
{
if (horizontalLine.EndPoint.Y <= (verticalLine.StartPoint.Y + _nCornerTolerance) && horizontalLine.EndPoint.Y >= (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;
}
}
Line
arrays as input ? \$\endgroup\$_nConrerTolerance
? Are the vertical lines always equal to the horizontal lines ? \$\endgroup\$private set
in the properties. C# 6 allows you to set the value of a getter only property in the constructor. \$\endgroup\$