/*Изменение позиции вершины*/ private void changeVertexPosition() { //Если ни одна вершина еще не изменяется, то выбираем вершину для изменения //И запоминаем её старые координаты, вектор перемещения if (currChangeIndex == -1) { currChangeIndex = rnd.Next(amount_points); direction = true; points[amount_points] = points[currChangeIndex]; points[amount_points + 1] = new Point_gr(Convert.ToDouble(rnd.Next(-100, 100)), Convert.ToDouble(rnd.Next(-100, 100)), Convert.ToDouble(rnd.Next(-100, 100))); points[amount_points + 2] = new Point_gr(points[amount_points].x + points[amount_points + 1].x, points[amount_points].y + points[amount_points + 1].y, points[amount_points].z + points[amount_points + 1].z); } //изменяем положение точки в направлении - от настоящей координаты if (direction == true) { points[currChangeIndex].x += points[amount_points + 1].x / 10; points[currChangeIndex].y += points[amount_points + 1].y / 10; points[currChangeIndex].z += points[amount_points + 1].z / 10; //Если текущее положение точки достаточно близко к тому, которое должно быть получено расчетами //то меняем направление движения и для точности устанавливаем точку в расчетное положение if (points[amount_points + 2].x + epsilon > points[currChangeIndex].x && points[amount_points + 2].x - epsilon <points[currChangeIndex].x && points[amount_points + 2].y + epsilon> points[currChangeIndex].y && points[amount_points + 2].y - epsilon <points[currChangeIndex].y && points[amount_points + 2].z + epsilon> points[currChangeIndex].z && points[amount_points + 2].z - epsilon < points[currChangeIndex].z) { direction = false; points[currChangeIndex] = points[amount_points + 2]; } } //изменяем положение точки в направлении - к настоящей координате else { points[currChangeIndex].x -= points[amount_points + 1].x / 10; points[currChangeIndex].y -= points[amount_points + 1].y / 10; points[currChangeIndex].z -= points[amount_points + 1].z / 10; //Если текущее положение точки достаточно близко к её изначальному, //то изменения для этой вершины окончены, выставляем сигнал того, что ни одна точка не изменяется if (points[amount_points].x + epsilon > points[currChangeIndex].x && points[amount_points].x - epsilon <points[currChangeIndex].x && points[amount_points].y + epsilon> points[currChangeIndex].y && points[amount_points].y - epsilon <points[currChangeIndex].y && points[amount_points].z + epsilon> points[currChangeIndex].z && points[amount_points].z - epsilon < points[currChangeIndex].z) { points[currChangeIndex] = points[amount_points]; currChangeIndex = -1; } } }
/*Рисование фигур*/ void DrawPoints() { drawBox.Refresh(); //Если точки уже были отрисованы //то по-новому проецируем их if (points != null) { //матрица косоугольного проецирования double[,] projectionMatrix = { { 1, 0, 0, 0 }, { 0, 1, 0, 0 }, { -0.25 * Math.Sqrt(2), -0.25 * Math.Sqrt(2), 0, 0 }, { 0, 0, 0, 1 } }; //Изменённые координаты точек Point_gr[] trans_points = new Point_gr[amount_points]; double[] matrix; for (int i = 0; i < amount_points; i++) { double[] point_coord = { points[i].x, points[i].y, points[i].z, points[i].h }; //перемножение координат точки на матрицу проектирования matrix = MatrixMulty(projectionMatrix, point_coord); trans_points[i] = new Point_gr(matrix[0] / matrix[3], matrix[1] / matrix[3], matrix[2] / matrix[3]); } Bitmap myBitmap = new Bitmap(drawBox.Width, drawBox.Height); g = Graphics.FromImage(myBitmap); //рисование осей координат Point_gr start_point = new Point_gr(0, 0, 0); Point_gr x = new Point_gr(300, 0, 0), x_trans; Point_gr y = new Point_gr(0, 300, 0), y_trans; Point_gr z = new Point_gr(0, 0, 300), z_trans; double[] point_coord_x = { x.x, x.y, x.z, x.h }; matrix = MatrixMulty(projectionMatrix, point_coord_x); x_trans = new Point_gr(matrix[0] / matrix[3], matrix[1] / matrix[3], matrix[2] / matrix[3]); double[] point_coord_y = { y.x, y.y, y.z, y.h }; matrix = MatrixMulty(projectionMatrix, point_coord_y); y_trans = new Point_gr(matrix[0] / matrix[3], matrix[1] / matrix[3], matrix[2] / matrix[3]); double[] point_coord_z = { z.x, z.y, z.z, z.h }; matrix = MatrixMulty(projectionMatrix, point_coord_z); z_trans = new Point_gr(matrix[0] / matrix[3], matrix[1] / matrix[3], matrix[2] / matrix[3]); g.DrawLine(new Pen(Color.Green, 2), new Point(175, 175), new Point(175 + Convert.ToInt32(x_trans.x), 175 + Convert.ToInt32(x_trans.y))); g.DrawLine(new Pen(Color.Green, 2), new Point(175, 175), new Point(175 + Convert.ToInt32(y_trans.x), 175 + Convert.ToInt32(y_trans.y))); g.DrawLine(new Pen(Color.Green, 2), new Point(175, 175), new Point(125 + Convert.ToInt32(z_trans.x), 125 + Convert.ToInt32(z_trans.y))); //рисование линий 3д объекта foreach (Line_gr line in lines) { g.DrawLine(Pens.Red, Convert.ToInt32(trans_points[line.from].x + 175), Convert.ToInt32(trans_points[line.from].y + 175), Convert.ToInt32(trans_points[line.to].x + 175), Convert.ToInt32(trans_points[line.to].y + 175)); } drawBox.Image = myBitmap; } //иначе считываем точки из файлов else { //56 точек и 88 линий соединения points = new Point_gr[amount_points + 3]; lines = new Line_gr[amount_lines]; //Файлы, из которых берутся точки System.IO.StreamReader inp_p = new System.IO.StreamReader("points.txt"); System.IO.StreamReader inp_l = new System.IO.StreamReader("lines.txt"); string tmp; string[] mass_str; //Получение списка точек int i = 0; do { tmp = inp_p.ReadLine(); mass_str = tmp.Split(' '); points[i] = new Point_gr(Convert.ToDouble(mass_str[0]), Convert.ToDouble(mass_str[1]), Convert.ToDouble(mass_str[2])); i++; } while (inp_p.Peek() != -1); //Получение отрезков i = 0; do { tmp = inp_l.ReadLine(); mass_str = tmp.Split(' '); lines[i] = new Line_gr(Convert.ToInt32(mass_str[0]), Convert.ToInt32(mass_str[1])); i++; } while (inp_l.Peek() != -1); //Отрисовывание полученной фигуры DrawPoints(); } }