Может кто-то поможет упростить код?

Дан квадрат его пересекают произвольным количеством прямых параллельных основанию, так, что делят на прямоугольники. Найти точки пересечения боковых сторон квадрата с прямыми.

 /// <summary>
/// This class is designed to find the intersection points of lines.
/// </summary>
public class LineInRectangle
{
    /// <summary>
    /// Defold constructor.
    /// </summary>
    public LineInRectangle() { }
    /// <summary>
    /// The constructor takes two values.
    /// </summary>
    /// <param name="rectangle">Rectangle type "Rectangle".</param>
    /// <param name="straightY">Array of int functions.</param>
    public LineInRectangle(Rectangle rectangle, int[] straightY)
    {
        this.rectangle = rectangle;
        this.straightY = straightY;
    }
    /// <summary>
    /// Read Only Properties.
    /// </summary>
    public int[] StraightY { get => straightY; }
    /// <summary>
    /// Read Only Properties.
    /// </summary>
    public Rectangle Rectangle { get => rectangle; }
    /// <summary>
    /// This method searches and records the points of the square that the line intersects.
    /// </summary>
    /// <returns>Array type "Point[]", having the intersection points of straight lines with a square.</returns>
    public Point[] FindPointsLinesIncisionRectangle()
    {
        Point[] PointLeft = new Point[StraightY.Length];
        for (int i = 0; i < StraightY.GetLength(0); i++) //left border
        {
            if (Rectangle.Top < StraightY[i] && Rectangle.Bottom >= StraightY[i]) //Fing intersections.
            {
                PointLeft[i].X = Rectangle.Left;
                PointLeft[i].Y = StraightY[i];
            }
            else
            {
                PointLeft[i].Y = Rectangle.Top; //We fill in the value which then we will delete.
            }
        }
        //repeat
        Point[] PointRight = new Point[StraightY.Length];
        for (int i = 0; i < StraightY.GetLength(0); i++) //right border
        {
            if (Rectangle.Top < StraightY[i] && Rectangle.Bottom >= StraightY[i]) //Fing intersections.
            {
                PointRight[i].X = Rectangle.Right;
                PointRight[i].Y = StraightY[i];
            }
            else
            {
                PointRight[i].Y = Rectangle.Top; //We fill in the value which then we will delete.
            }
        }
        Point[] PointLeftLast = PointLeft.Where(f => f.Y != Rectangle.Top).ToArray(); //Get rid of unnecessary values.
        Point[] PointRightLast = PointRight.Where(f => f.Y != Rectangle.Top).ToArray();//Get rid of unnecessary values.

        int coutntLeft = 0;
        int coutntRight = 0;
        Point[] lastPoint = new Point[PointLeftLast.GetLength(0) + PointRightLast.GetLength(0)];

        for (int i = 0; i < lastPoint.GetLength(0); i += 2) //We connect two arrays in turn.
        {
            lastPoint[i] = PointLeftLast[coutntLeft];
            lastPoint[i + 1] = PointRightLast[coutntRight];
            coutntLeft++;
            coutntRight++;
        }
        return lastPoint;
    }
    /// <summary>
    /// Field.
    /// </summary>
    Rectangle rectangle;

    /// <summary>
    /// Field.
    /// </summary>
    int[] straightY;
}

}


Ответы (0 шт):