Я получил System.IndexOutOfRangeException в части метода SearchForTheNextDotsTopRight ()
if ((pointsArray[i].X == j) &&
((pointsArray[i].Y) <= p.Y))
Я просто не знаю, почему в этой ситуации массив не работает. С нетерпением жду помощи, большое вам спасибо!
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
namespace Draw_Test
{
public partial class Form1 : Form
{
public static int sizeOfArray = 20;
int TopBorderY = 100;
int LeftBorderX = 100;
int BottomBorderY = 800;
int RightBorderX = 800;
Point Base = new Point(0, 0);
Point[] pointsArray = new Point[sizeOfArray];
public Form1()
{
InitializeComponent();
}
private void PointsArray(PaintEventArgs e)
{
var rand = new Random();
for (int i = 0; i < sizeOfArray; i++)
{
pointsArray[i].X = rand.Next(100, 800);
pointsArray[i].Y = rand.Next(100, 800);
CreateDots(pointsArray[i], e);
}
Point Nearest = SearchForTheFirstDots(pointsArray);
CreateLines(Base, Nearest, e);
Point Next = SearchForTheNextDotsTopRight(Nearest, pointsArray);
CreateLines(Nearest, Next, e);
}
private Point SearchForTheNextDotsTopRight(Point p, Point[] pointsArray)
{
Point PointNext = p;
for (int j = p.X; j <= RightBorderX; j++)
{
for (int i = 0; i <= sizeOfArray; i++)
{
if ((pointsArray[i].X == j) &&
((pointsArray[i].Y) <= p.Y))
{
PointNext = pointsArray[i];
goto endofLoop:
}
}
}
endofLoop:
return PointNext;
}
private double CalculateDistance(Point p1, Point p2)
{
double distance = Math.Pow(p1.X - p2.X, 2) + Math.Pow(p1.Y - p2.Y, 2);
return distance;
}
private Point SearchForTheFirstDots(Point[] pointsArray)
{
Point Nearest = pointsArray[0];
for (int i = 0; i < sizeOfArray; i++)
{
if (CalculateDistance(Base, pointsArray[i]) <
CalculateDistance(Base, Nearest))
{
Nearest = pointsArray[i];
}
}
//pointList.Add(Nearest);
return Nearest;
}
private void CreateDots(Point p, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen pen = new Pen(Color.Blue, 3);
int width = 3;
int height = 3;
int pointXform = p.X - width / 2;
int pointYform = p.Y - height / 2;
Rectangle r = new Rectangle(pointXform, pointYform, width, height);
g.DrawEllipse(pen, r);
}
private void CreateLines(Point p1, Point p2, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen pen = new Pen(Color.Blue, 3);
g.DrawLine(pen, p1, p2);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Point p1 = new Point(25, 25);
Point p2 = new Point(100, 150);
PointsArray(e);
}
}
}