Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Отчер по ЛР 2 МО-.docx
Скачиваний:
1
Добавлен:
22.08.2023
Размер:
433.22 Кб
Скачать

УФИМСКИЙ ГОСУДАРСТВЕННЫЙ АВИАЦИОННЫЙ ТЕХНИЧЕСКИЙ УНИВЕРСИТЕТ

Кафедра вычислительной математики и кибернетики

Лабораторная работа № 2

«Программирование трехмерной графики»

Выполнил:

студент группы МО-

Проверил:

Котельников В.А

Уфа, 2021

Оглавление

Теоретические основы: 3

Ход работы: 3

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

Листинг программ 16

Цель работы: изучить основы программирования двухмерной графики.

Задание: Перевести примеры с VB на С#. Визуализировать

  1. Сферу, вписанную в куб

  2. Тор

  3. Эллипсоид вращения

  4. Шесть сфер

Теоретические основы:

Точки трехмерного пространства имеют три, а не две координаты, например, A(x,y,z). Математический аппарат двухмерных и трехмерных трансформаций хорошо описан на сайте лаборатории компьютерной графики МГУ и в работе Дэвида Любке, чьими формулами мы пользовались при написании данной главы. Прежде чем рисовать трехмерный объекты, нам понадобятся сведения о том, как их вращать. Без вращения трудно оценить форму объекта. Вариантов вращения в трехмерном пространстве гораздо больше, чем в двухмерном, так, как наряду с перемещением добавляется еще и спин. Что означает слово «спин»? Представьте себе, что вы вращаете карандаш. Спин будет изменяться при вращении карандаша вокруг своей оси. Для описания различных трансформаций объектов в трехмерном (и двухмерном) пространстве широко применяется математический аппарат матриц. Предположим, что у нас есть текущие координаты некой точки A(x,y,z) и мы хотим повернуть ее, осуществить сдвиг, изменить масштаб или переместить в пространстве и найти, в конце концов, новые координаты B(x’,y’,z’).

Ход работы:

Реализация примеров:

Рисунок 1.Вращение объекта относительно трех осей координат

Рисунок 2.Вращение объекта вокруг произвольной точки

Рисунок 3.Поворот вокруг оси, проходящей через начало координат

Рисунок 4. Поворот вокруг оси, проходящей через начало координат на угол

Рисунок 5. Поворот точки вокруг произвольной оси

Рисунок 6.Вращение вокруг произвольной прямой

Рисунок 7. Произвольное вращение

Рисунок 8. Визуализация вершин куба

Рисунок 9.Визуализация граней куба

Рисунок 10. Вращение вокруг вектора

Рисунок 11. Изображение сторон куба

Рисунок 12. Параллелепипед, как совокупность точек

Рисунок 13. Пирамида, как совокупность точек

Рисунок 14. Цилиндр, как совокупность точек

Рисунок 15. Сфера, как совокупность точек

Рисунок 16. Конус, как совокупность точек

Рисунок 17. Комбинация двух конусов

Рисунок 18. Объединение трех усеченных конусов

Рисунок 19. Сфера, вписанная в куб

Рисунок 20.Тор

Рисунок 21. Эллипсоид вращения

Рисунок 22. Шесть сфер

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

Пример 1. Вращение объекта относительно трех осей координат

namespace ICG_LR2

{

public partial class Example1 : Form

{

public Example1()

{

InitializeComponent();

}

double curve1, curve2, curve3;

private void hScrollBarPitch_Scroll(object sender, ScrollEventArgs e)

{

x = factor * (hScrollBarPitch.Value) - curve1;

curve1 = factor * (hScrollBarPitch.Value);

DrawShape(g, 0);

}

private void hScrollBarYaw_Scroll(object sender, ScrollEventArgs e)

{

y = factor * (hScrollBarYaw.Value) - curve2;

curve2 = factor * (hScrollBarYaw.Value);

DrawShape(g, 1);

}

private void hScrollBarRoll_Scroll(object sender, ScrollEventArgs e)

{

z = factor * (hScrollBarRoll.Value) - curve3;

curve3 = factor * (hScrollBarRoll.Value);

DrawShape(g, 2);

}

Graphics g;

private void Example1_Load(object sender, EventArgs e)

{

g = pictureBox1.CreateGraphics();

int maximum = 369, minimum = 0;

//Трекбары получают максимальное и минимальное значение

hScrollBarPitch.Minimum = minimum;

hScrollBarPitch.Maximum = maximum;

hScrollBarRoll.Minimum = minimum;

hScrollBarRoll.Maximum = maximum;

hScrollBarYaw.Minimum = minimum;

hScrollBarYaw.Maximum = maximum;

x0[0] = 200;

y0[0] = 100;

z0[0] = 0;

x0[1] = 300;

y0[1] = 300;

z0[1] = 0;

x0[2] = 400;

y0[2] = 100;

z0[2] = 0;

}

double factor = Math.PI / 180;

double x, y, z;

static int vertex_number = 3;

double[] x0 = new double[vertex_number];

double[] y0 = new double[vertex_number];

double[] z0 = new double[vertex_number];

Point[] point = new Point[vertex_number];

double[] newX = new double[vertex_number];

double[] newY = new double[vertex_number];

double[] newZ = new double[vertex_number];

private void pictureBox1_Click(object sender, EventArgs e)

{

}

public double RotatePitch(double x, double y, double z, double alpha, ref double newX, ref double newY)

{

double newZ;

newX = x;

newY = y * Math.Cos(alpha) - z * Math.Sin(alpha);

newZ = y * Math.Sin(alpha) + z * Math.Cos(alpha);

return newZ;

}

public double RotateYaw(double x, double y, double z, double alpha, ref double newX, ref double newY)

{

double newZ;

newX = x * Math.Cos(alpha) + z * Math.Sin(alpha);

newY = y;

newZ = -x * Math.Sin(alpha) + z * Math.Cos(alpha);

return newZ;

}

public double RotateRoll(double x, double y, double z, double alpha, ref double newX, ref double newY)

{

double newZ;

newX = x * Math.Cos(alpha) - y * Math.Sin(alpha);

newY = x * Math.Sin(alpha) + y * Math.Cos(alpha);

newZ = z;

return newZ;

}

public void DrawShape(Graphics GraphicObject, int Axis)

{

if (Axis == 0)

{

newZ[0] = RotatePitch(x0[0], y0[0], z0[0], x, ref newX[0], ref newY[0]);

newZ[1] = RotatePitch(x0[1], y0[1], z0[1], x, ref newX[1], ref newY[1]);

newZ[2] = RotatePitch(x0[2], y0[2], z0[2], x, ref newX[2], ref newY[2]);

}

else if (Axis == 1)

{

newZ[0] = RotateYaw(x0[0], y0[0], z0[0], y, ref newX[0], ref newY[0]);

newZ[1] = RotateYaw(x0[1], y0[1], z0[1], y, ref newX[1], ref newY[1]);

newZ[2] = RotateYaw(x0[2], y0[2], z0[2], y, ref newX[2], ref newY[2]);

}

else if (Axis == 2)

{

newZ[0] = RotateRoll(x0[0], y0[0], z0[0], z, ref newX[0], ref newY[0]);

newZ[1] = RotateRoll(x0[1], y0[1], z0[1], z, ref newX[1], ref newY[1]);

newZ[2] = RotateRoll(x0[2], y0[2], z0[2], z, ref newX[2], ref newY[2]);

}

x0[0] = newX[0];

y0[0] = newY[0];

z0[0] = newZ[0];

//////////////

x0[1] = newX[1];

y0[1] = newY[1];

z0[1] = newZ[1];

////////////////

x0[2] = newX[2];

y0[2] = newY[2];

z0[2] = newZ[2];

////////////////

point[0] = new Point((int)newX[0], (int)newY[0]);

point[1] = new Point((int)newX[1], (int)newY[1]);

point[2] = new Point((int)newX[2], (int)newY[2]);

PointF[] myCurve = new PointF[] { point[0], point[1], point[2] };

Pen mypen = new Pen(Color.Red, 2);

SolidBrush mybrush = new SolidBrush(Color.Blue);

g.Clear(Color.White);

GraphicObject.DrawPolygon(mypen, myCurve);

GraphicObject.FillPolygon(mybrush, myCurve);

}

}

}

Пример 2. Вращение объекта вокруг произвольной точки

public Example2()

{

InitializeComponent();

}

Graphics G;

public double RotatePitch(double x, double y, double z, double alpha, ref double NewX, ref double NewY)

{

double NewZ;

NewX = x;

NewY = y0 + (y - y0) * Math.Cos(alpha) + (z0 - z) * Math.Sin(alpha);

NewZ = z0 + (y - y0) * Math.Sin(alpha) + (z - z0) * Math.Cos(alpha);

return NewZ;

}

public double RotateYaw(double x, double y, double z, double alpha, ref double NewX, ref double NewY)

{

double NewZ;

NewX = x0 + (x - x0) * Math.Cos(alpha) + (z - z0) * Math.Sin(alpha);

NewY = y;

NewZ = z0 + (x0 - x) * Math.Sin(alpha) + (z - z0) * Math.Cos(alpha);

return NewZ;

}

public double RotateRoll(double x, double y, double z, double alpha, ref double NewX, ref double NewY)

{

double NewZ;

NewX = x0 + (x - x0) * Math.Cos(alpha) + (y0 - y) * Math.Sin(alpha);

NewY = y0 + (x - x0) * Math.Sin(alpha) + (y - y0) * Math.Cos(alpha);

NewZ = z;

return NewZ;

}

public void DrawShape(Graphics GraphicObject, int Axis)

{

GraphicObject = pictureBox1.CreateGraphics();

if (Axis == 0)

{

newZ[0] = RotatePitch(x[0], y[0], z[0], XAxisAngle, ref newX[0], ref newY[0]);

newZ[1] = RotatePitch(x[1], y[1], z[1], XAxisAngle, ref newX[1], ref newY[1]);

newZ[2] = RotatePitch(x[2], y[2], z[2], XAxisAngle, ref newX[2], ref newY[2]);

}

else if (Axis == 1)

{

newZ[0] = RotateYaw(x[0], y[0], z[0], YAxisAngle, ref newX[0], ref newY[0]);

newZ[1] = RotateYaw(x[1], y[1], z[1], YAxisAngle, ref newX[1], ref newY[1]);

newZ[2] = RotateYaw(x[2], y[2], z[2], YAxisAngle, ref newX[2], ref newY[2]);

}

else if (Axis == 2)

{

newZ[0] = RotateRoll(x[0], y[0], z[0], ZAxisAngle, ref newX[0], ref newY[0]);

newZ[1] = RotateRoll(x[1], y[1], z[1], ZAxisAngle, ref newX[1], ref newY[1]);

newZ[2] = RotateRoll(x[2], y[2], z[2], ZAxisAngle, ref newX[2], ref newY[2]);

}

x[0] = newX[0];

y[0] = newY[0];

z[0] = newZ[0];

x[1] = newX[1];

y[1] = newY[1];

z[1] = newZ[1];

x[2] = newX[2];

y[2] = newY[2];

z[2] = newZ[2];

point[0] = new Point((int)newX[0], (int)newY[0]);

point[1] = new Point((int)newX[1], (int)newY[1]);

point[2] = new Point((int)newX[2], (int)newY[2]);

PointF[] MyCurve = { point[0], point[1], point[2] };

Pen Mypen = new Pen(Color.Red, 2);

SolidBrush myBrush = new SolidBrush(Color.Blue);

G.Clear(Color.White);

Pen BlackPen = new Pen(Color.Black, 2);

Rectangle MyBox = new Rectangle(x0, y0, 5, 5);

GraphicObject.DrawEllipse(BlackPen, MyBox);

GraphicObject.DrawPolygon(Mypen, MyCurve);

GraphicObject.FillPolygon(myBrush, MyCurve);

//pictureBox1.Image = bitmap;

}

double Factor = Math.PI / 180;

static int vertex_number = 3;

double[] x = new double[vertex_number];

double[] y = new double[vertex_number];

double[] z = new double[vertex_number];

double[] newX = new double[vertex_number];

double[] newY = new double[vertex_number];

double[] newZ = new double[vertex_number];

Point[] point = new Point[vertex_number];

double XAxisAngle, YAxisAngle, ZAxisAngle;

int x0, y0, z0;

private void hScrollBarPitch_Scroll(object sender, ScrollEventArgs e)

{

XAxisAngle = Factor * (hScrollBarPitch.Value);

DrawShape(G, 0);

}

private void hScrollBarYaw_Scroll(object sender, ScrollEventArgs e)

{

YAxisAngle = Factor * (hScrollBarYaw.Value);

DrawShape(G, 1);

}

private void hScrollBarRoll_Scroll(object sender, ScrollEventArgs e)

{

ZAxisAngle = Factor * (hScrollBarRoll.Value);

DrawShape(G, 2);

}

private void Example2_Load(object sender, EventArgs e)

{

//bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);

//G = Graphics.FromImage(bitmap);

//pictureBox1.Image = bitmap;

G = pictureBox1.CreateGraphics();

hScrollBarPitch.Minimum = 1;

hScrollBarPitch.Maximum = 369;

hScrollBarYaw.Minimum = 1;

hScrollBarYaw.Maximum = 369;

hScrollBarRoll.Minimum = 1;

hScrollBarRoll.Maximum = 369;

hScrollBarPitchOffset.Minimum = 1;

hScrollBarPitchOffset.Maximum = pictureBox1.Width;

hScrollBarYawOffset.Minimum = 1;

hScrollBarYawOffset.Maximum = pictureBox1.Height;

hScrollBarRollOffset.Minimum = 1;

hScrollBarRollOffset.Maximum = pictureBox1.Height;

//coord triangle

x[0] = 200;

y[0] = 100;

x[1] = 300;

y[1] = 300;

x[2] = 400;

y[2] = 100;

}

private void hScrollBarPitchOffset_Scroll(object sender, ScrollEventArgs e)

{

x0 = hScrollBarPitchOffset.Value;

DrawShape(G, 3);

}

private void hScrollBarYawOffset_Scroll(object sender, ScrollEventArgs e)

{

y0 = hScrollBarYawOffset.Value;

DrawShape(G, 4);

}

private void hScrollBarRollOffset_Scroll(object sender, ScrollEventArgs e)

{

z0 = hScrollBarRollOffset.Value;

DrawShape(G, 5);

}

}

}

Пример 3. Поворот вокруг оси, проходящей через начало координат

public Example3()

{

InitializeComponent();

}

Graphics G;

Bitmap bitmap;

public double RotateObject(double Pitch, double Yaw, double Roll, double x, double y, double z, ref double NewX, ref double NewY)

{

double[,] m = new double[3, 3];

m[0, 0] = Cos(Factor * Yaw) * Cos(Factor * Roll);

m[0, 1] = -Cos(Factor * Yaw) * Sin(Factor * Roll);

m[0, 2] = -Sin(Factor * Yaw);

m[1, 0] = Sin(Factor * Pitch) * Sin(Factor * Yaw) * Cos(Factor * Roll) + Sin(Factor * Roll) * Cos(Factor * Pitch);

m[1, 1] = -Sin(Factor * Pitch) * Sin(Factor * Yaw) * Sin(Factor * Roll) + Cos(Factor * Roll) * Cos(Factor * Pitch);

m[1, 2] = Cos(Factor * Yaw);

m[2, 0] = -Cos(Factor * Pitch) * Sin(Factor * Yaw) * Cos(Factor * Roll) + Sin(Factor * Pitch) * Sin(Factor * Roll);

m[2, 1] = Cos(Factor * Pitch) * Sin(Factor * Yaw) * Sin(Factor * Roll) + Sin(Factor * Pitch) * Cos(Factor * Roll);

m[2, 2] = Cos(Factor * Yaw) * Cos(Factor * Pitch);

double NewZ;

NewX = m[0, 0] * x + m[1, 0] * y + m[2, 0] * z;

NewY = m[0, 1] * x + m[1, 1] * y + m[2, 1] * z;

NewZ = m[0, 2] * x + m[1, 2] * y + m[2, 2] * z;

return NewZ;

}

double Factor = Math.PI / 180;

static int vertex_number = 3;

double[] x = new double[vertex_number];

double[] y = new double[vertex_number];

double[] z = new double[vertex_number];

double[] newX = new double[vertex_number];

double[] newY = new double[vertex_number];

double[] newZ = new double[vertex_number];

Point[] point = new Point[vertex_number];

double Pitch, Yaw, Roll;

private void hScrollBarPitch_Scroll(object sender, ScrollEventArgs e)

{

Pitch = hScrollBarPitch.Value;

DrawShape(G);

}

private void hScrollBarYaw_Scroll(object sender, ScrollEventArgs e)

{

Yaw = hScrollBarYaw.Value;

DrawShape(G);

}

private void hScrollBarRoll_Scroll(object sender, ScrollEventArgs e)

{

Roll = hScrollBarRoll.Value;

DrawShape(G);

}

private void Example3_Load(object sender, EventArgs e)

{

bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);

G = Graphics.FromImage(bitmap);

pictureBox1.Image = bitmap;

hScrollBarPitch.Minimum = 1;

hScrollBarPitch.Maximum = 369;

hScrollBarYaw.Minimum = 1;

hScrollBarYaw.Maximum = 369;

hScrollBarRoll.Minimum = 1;

hScrollBarRoll.Maximum = 369;

x[0] = 200;

y[0] = 100;

x[1] = 300;

y[1] = 300;

x[2] = 400;

y[2] = 100;

}

public void DrawShape(Graphics GraphicObject)

{

newZ[0] = RotateObject(Pitch, Yaw, Roll, x[0], y[0], z[0], ref newX[0], ref newY[0]);

newZ[1] = RotateObject(Pitch, Yaw, Roll, x[1], y[1], z[1], ref newX[1], ref newY[1]);

newZ[2] = RotateObject(Pitch, Yaw, Roll, x[2], y[2], z[2], ref newX[2], ref newY[2]);

point[0] = new Point((int)newX[0], (int)newY[0]);

point[1] = new Point((int)newX[1], (int)newY[1]);

point[2] = new Point((int)newX[2], (int)newY[2]);

Point[] curvePoints = { point[0], point[1], point[2] };

Pen MyPen = new Pen(Color.Red, 2);

SolidBrush myBrush = new SolidBrush(Color.Blue);

GraphicObject.Clear(Color.White);

GraphicObject.DrawPolygon(MyPen, curvePoints);

GraphicObject.FillPolygon(myBrush, curvePoints);

pictureBox1.Image = bitmap;

}

Пример 4. Поворот вокруг оси, проходящей через начало координат на угол

public Example4()

{

InitializeComponent();

}

Graphics G;

Bitmap bitmap;

public double RotateObject(double Pitch, double Yaw, double Roll, double a, double x, double y, double z, ref double newx, ref double newy)

{

double Temp;

Temp = 1.0 - Math.Cos(a);

double NewZ;

newx = x * (Pitch * Temp * Pitch + Math.Cos(a)) + y * (Yaw * Temp * Pitch - Math.Sin(a) * Roll) + z * (Roll * Temp * Pitch + Math.Sin(a) * Yaw);

newy = x * (Pitch * Temp * Yaw + Math.Sin(a) * Roll) + y * (Yaw * Temp * Yaw + Math.Cos(a)) + z * (Roll * Temp * Yaw - Math.Sin(a) * Pitch);

NewZ = x * (Pitch * Temp * Roll - Math.Sin(a) * Yaw) + y * (Yaw * Temp * Roll + Math.Sin(a) * Pitch) + z * (Roll * Temp * Roll + Math.Cos(a));

return NewZ;

}

public void DrawShape(Graphics GraphicObject)

{

newZ[0] = RotateObject(Pitch, Yaw, Roll, Theta, x[0], y[0], z[0], ref newX[0], ref newY[0]);

newZ[1] = RotateObject(Pitch, Yaw, Roll, Theta, x[1], y[1], z[1], ref newX[1], ref newY[1]);

newZ[2] = RotateObject(Pitch, Yaw, Roll, Theta, x[2], y[2], z[2], ref newX[2], ref newY[2]);

Point[0] = new Point((int)newX[0], (int)newY[0]);

Point[1] = new Point((int)newX[1], (int)newY[1]);

Point[2] = new Point((int)newX[2], (int)newY[2]);

Point MyPoint1 = new Point(0, 0);

double L = 5 * this.Width;

int MyX, MyY;

MyX = (int)(L * Cos(Pitch));

MyY = (int)(L * Cos(Yaw));

Point MyPoint2 = new Point(MyX, MyY);

Pen blackPen = new Pen(Color.Black, 2);

Point[] curvePoints = new[] { Point[0], Point[1], Point[2] };

Pen MyPen = new Pen(Color.Red, 2);

SolidBrush myBrush = new SolidBrush(Color.Blue);

G.Clear(Color.White);

GraphicObject.DrawPolygon(MyPen, curvePoints);

GraphicObject.FillPolygon(myBrush, curvePoints);

GraphicObject.DrawLine(blackPen, MyPoint1, MyPoint2);

pictureBox1.Image = bitmap;

}

private void hScrollBarPitch_Scroll(object sender, ScrollEventArgs e)

{

Pitch = Factor * (hScrollBarPitch.Value);

DrawShape(G);

}

private void hScrollBarYaw_Scroll(object sender, ScrollEventArgs e)

{

Yaw = Factor * (hScrollBarYaw.Value);

DrawShape(G);

}

private void hScrollBarRoll_Scroll(object sender, ScrollEventArgs e)

{

Roll = Factor * (hScrollBarRoll.Value);

DrawShape(G);

}

private void hScrollBarTheta_Scroll(object sender, ScrollEventArgs e)

{

Theta = Factor * (hScrollBarTheta.Value);

DrawShape(G);

}

double Factor = Math.PI / 180;

double Pitch, Yaw, Roll, Theta;

static int Vertex_Number = 3;

double[] x = new double[Vertex_Number + 1];

double[] y = new double[Vertex_Number + 1];

double[] z = new double[Vertex_Number + 1];

double[] newX = new double[Vertex_Number + 1];

double[] newY = new double[Vertex_Number + 1];

double[] newZ = new double[Vertex_Number + 1];

Point[] Point = new Point[Vertex_Number + 1];

private void Example4_Load(object sender, EventArgs e)

{

bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);

G = Graphics.FromImage(bitmap);

pictureBox1.Image = bitmap;

hScrollBarPitch.Minimum = 1;

hScrollBarPitch.Maximum = 369;

hScrollBarYaw.Minimum = 1;

hScrollBarYaw.Maximum = 369;

hScrollBarRoll.Minimum = 1;

hScrollBarRoll.Maximum = 369;

hScrollBarTheta.Minimum = 1;

hScrollBarTheta.Maximum = 369;

// Задаем координаты вершин треугольника

x[0] = 200;

y[0] = 100;

x[1] = 300;

y[1] = 300;

x[2] = 400;

y[2] = 100;

}

Пример 5. Поворот точки вокруг произвольной оси

public Example5()

{

InitializeComponent();

}

Graphics G;

Bitmap bitmap;

static int Vertex_Number = 3;

double[] x = new double[Vertex_Number + 1];

double[] y = new double[Vertex_Number + 1];

double[] z = new double[Vertex_Number + 1];

double[] newX = new double[Vertex_Number + 1];

double[] newY = new double[Vertex_Number + 1];

double[] newZ = new double[Vertex_Number + 1];

Point[] Point = new Point[Vertex_Number + 1];

double Factor = Math.PI / 180;

double Theta, Pitch, Yaw, Roll, XOffset, YOffset, ZOffset;

private void hScrollBarPitch_Scroll(object sender, ScrollEventArgs e)

{

Pitch = hScrollBarPitch.Value;

DrawShape(G);

}

private void hScrollBarYaw_Scroll(object sender, ScrollEventArgs e)

{

Yaw = hScrollBarYaw.Value;

DrawShape(G);

}

private void hScrollBarRoll_Scroll(object sender, ScrollEventArgs e)

{

Roll = hScrollBarRoll.Value;

DrawShape(G);

}

private void hScrollBarTheta_Scroll(object sender, ScrollEventArgs e)

{

Theta = hScrollBarTheta.Value;

DrawShape(G);

}

private void hScrollBarXOffset_Scroll(object sender, ScrollEventArgs e)

{

XOffset = hScrollBarXOffset.Value;

DrawShape(G);

}

private void hScrollBarYOffset_Scroll(object sender, ScrollEventArgs e)

{

YOffset = hScrollBarYOffset.Value;

DrawShape(G);

}

private void hScrollBarZOffset_Scroll(object sender, ScrollEventArgs e)

{

ZOffset = hScrollBarZOffset.Value;

DrawShape(G);

}

public double RotateObject(double Theta, double Pitch, double Yaw, double Roll, double x, double y, double z, double x0, double y0, double z0, ref double NewX, ref double NewY)

{

double temp;

temp = 1.0 - Cos(Theta);

double NewZ;

NewX = x0 + (x - x0) * (Pitch * temp * Pitch + Cos(Theta)) + (y - y0) * (Yaw * temp * Pitch - Sin(Theta) * Roll) + (z - z0) * (Roll * temp * Pitch + Sin(Theta) * Yaw);

NewY = y0 + (x - x0) * (Pitch * temp * Yaw + Sin(Theta) * Roll) + (y - y0) * (Yaw * temp * Yaw + Cos(Theta)) + (z - z0) * (Roll * temp * Yaw - Sin(Theta) * Pitch);

NewZ = z0 + (x - x0) * (Pitch * temp * Roll - Sin(Theta) * Yaw) + (y - y0) * (Yaw * temp * Roll + Sin(Theta) * Pitch) + (z - z0) * (Roll * temp * Roll + Cos(Theta));

return NewZ;

}

public void DrawShape(Graphics GraphicObject)

{

newZ[0] = RotateObject(Theta, Pitch, Yaw, Roll, x[0], y[0], z[0], XOffset, YOffset, ZOffset, ref newX[0], ref newY[0]);

newZ[1] = RotateObject(Theta, Pitch, Yaw, Roll, x[1], y[1], z[1], XOffset, YOffset, ZOffset, ref newX[1], ref newY[1]);

newZ[2] = RotateObject(Theta, Pitch, Yaw, Roll, x[2], y[2], z[2], XOffset, YOffset, ZOffset, ref newX[2], ref newY[2]);

Point[0] = new Point((int)newX[0], (int)newY[0]);

Point[1] = new Point((int)newX[1], (int)newY[1]);

Point[2] = new Point((int)newX[2], (int)newY[2]);

Rectangle MyRectangle = new Rectangle((int)XOffset, (int)YOffset, 5, 5);

Pen blackPen = new Pen(Color.Black, 2);

Point[] curvePoints = new[] { Point[0], Point[1], Point[2] };

Pen MyPen = new Pen(Color.Red, 2);

SolidBrush myBrush = new SolidBrush(Color.Blue);

GraphicObject.Clear(Color.White);

GraphicObject.DrawEllipse(blackPen, MyRectangle);

double L;

L = 5 * this.Width;

Point MyPoint1 = new Point(0, 0);

Point MyPoint2 = new Point((int)(L * Cos(Pitch)), (int)(L * Cos(Yaw)));

GraphicObject.DrawLine(MyPen, MyPoint1, MyPoint2);

GraphicObject.DrawPolygon(MyPen, curvePoints);

GraphicObject.FillPolygon(myBrush, curvePoints);

pictureBox1.Image = bitmap;

}

private void Example5_Load(object sender, EventArgs e)

{

bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);

G = Graphics.FromImage(bitmap);

pictureBox1.Image = bitmap;

int Minimum, Maximum;

Minimum = 1;

Maximum = 369;

hScrollBarTheta.Minimum = Minimum;

hScrollBarTheta.Maximum = Maximum;

hScrollBarPitch.Minimum = Minimum;

hScrollBarPitch.Maximum = pictureBox1.Width;

hScrollBarYaw.Minimum = Minimum;

hScrollBarYaw.Maximum = pictureBox1.Width;

hScrollBarRoll.Minimum = Minimum;

hScrollBarRoll.Maximum = pictureBox1.Width;

hScrollBarXOffset.Minimum = Minimum;

hScrollBarXOffset.Maximum = pictureBox1.Width;

hScrollBarYOffset.Minimum = Minimum;

hScrollBarYOffset.Maximum = pictureBox1.Width;

hScrollBarZOffset.Minimum = Minimum;

hScrollBarZOffset.Maximum = pictureBox1.Width;

// Задаем координаты вершин треугольника

x[0] = 200;

y[0] = 100;

x[1] = 300;

y[1] = 300;

x[2] = 400;

y[2] = 100;

}

Пример 6. Вращение вокруг произвольной прямой

public Example7()

{

InitializeComponent();

}

Graphics G;

Bitmap bitmap;

static int Vertex_Number = 3;

double[] x = new double[Vertex_Number + 1], y = new double[Vertex_Number + 1], z = new double[Vertex_Number + 1], newX = new double[Vertex_Number + 1], newY = new double[Vertex_Number + 1], newZ = new double[Vertex_Number + 1];

Point[] Point = new Point[Vertex_Number + 1];

double Theta, u, v, w, A, B, C;

public double RotateObject(double Theta, double A, double B, double C, double u, double v, double w, double x, double y, double z, ref double NewX, ref double NewY)

{

double NewZ;

double L = Pow(u, 2) + Pow(v, 2) + Pow(w, 2);

double SqrL = Sqrt(L);

double Lu = Pow(v, 2) + Pow(w, 2);

double Lv = Pow(u, 2) + Pow(w, 2);

double Lw = Pow(u, 2) + Pow(v, 2);

NewX = (A * Lu + u * (-B * v - C * w + u * x + v * y + w * z)

+ (-A * Lu + u * (B * v + C * w - v * y - w * z) + Lu * x) * Cos(Theta) + SqrL * (-C * v + B * w - w * y + v * z) * Sin(Theta)) / L;

NewY = (B * Lv + v * (-A * u - C * w + u * x + v * y + w * z)

+ (-B * Lv + v * (A * u + C * w - u * x - w * z) + Lv * y) * Cos(Theta) + SqrL * (-C * u - A * w + w * x - u * z) * Sin(Theta)) / L;

NewZ = (C * Lw + w * (-A * u - B * v + u * x + v * y + w * z)

+ (-C * Lw + w * (A * u + B * v - u * x - v * y) + Lw * z) * Cos(Theta) + SqrL * (-B * u + A * v - v * x + u * y) * Sin(Theta)) / L;

return NewZ;

}

public void DrawShape(Graphics GraphicObject)

{

newZ[0] = RotateObject(Theta, A, B, C, u, v, w, x[0], y[0], z[0], ref newX[0], ref newY[0]);

newZ[1] = RotateObject(Theta, A, B, C, u, v, w, x[1], y[1], z[1], ref newX[1], ref newY[1]);

newZ[2] = RotateObject(Theta, A, B, C, u, v, w, x[2], y[2], z[2], ref newX[2], ref newY[2]);

Point[0] = new Point((int)newX[0], (int)newY[0]);

Point[1] = new Point((int)newX[1], (int)newY[1]);

Point[2] = new Point((int)newX[2], (int)newY[2]);

Point[] curvePoints = new[] { Point[0], Point[1], Point[2] };

Pen MyPen = new Pen(Color.Red, 2);

SolidBrush myBrush = new SolidBrush(Color.Blue);

GraphicObject.Clear(Color.White);

GraphicObject.DrawPolygon(MyPen, curvePoints);

GraphicObject.FillPolygon(myBrush, curvePoints);

Point MyPoint1 = new Point(System.Convert.ToInt32(A), System.Convert.ToInt32(B));

Point MyPoint2 = new Point(System.Convert.ToInt32(u), System.Convert.ToInt32(v));

Pen blackPen = new Pen(Color.Black, 2);

GraphicObject.DrawLine(blackPen, MyPoint1, MyPoint2);

pictureBox1.Image = bitmap;

}

private void hScrollBarPitch_Scroll(object sender, ScrollEventArgs e)

{

u = hScrollBarPitch.Value;

DrawShape(G);

}

private void hScrollBarYaw_Scroll(object sender, ScrollEventArgs e)

{

v = hScrollBarYaw.Value;

DrawShape(G);

}

private void hScrollBarRoll_Scroll(object sender, ScrollEventArgs e)

{

w = hScrollBarRoll.Value;

DrawShape(G);

}

private void hScrollBarA_Scroll(object sender, ScrollEventArgs e)

{

A = hScrollBarA.Value;

DrawShape(G);

}

private void hScrollBarB_Scroll(object sender, ScrollEventArgs e)

{

B = hScrollBarB.Value;

DrawShape(G);

}

private void hScrollBarC_Scroll(object sender, ScrollEventArgs e)

{

C = hScrollBarC.Value;

DrawShape(G);

}

private void hScrollBarTheta_Scroll(object sender, ScrollEventArgs e)

{

Theta = hScrollBarTheta.Value;

DrawShape(G);

}

private void Example7_Load(object sender, EventArgs e)

{

bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);

G = Graphics.FromImage(bitmap);

pictureBox1.Image = bitmap;

int Minimum = 1, Maximum = 369;

hScrollBarTheta.Minimum = Minimum;

hScrollBarTheta.Maximum = Maximum;

hScrollBarPitch.Minimum = Minimum;

hScrollBarPitch.Maximum = pictureBox1.Width;

hScrollBarYaw.Minimum = Minimum;

hScrollBarYaw.Maximum = pictureBox1.Width;

hScrollBarRoll.Minimum = Minimum;

hScrollBarRoll.Maximum = pictureBox1.Width;

hScrollBarA.Minimum = Minimum;

hScrollBarA.Maximum = pictureBox1.Width;

hScrollBarB.Minimum = Minimum;

hScrollBarB.Maximum = pictureBox1.Width;

hScrollBarC.Minimum = Minimum;

hScrollBarC.Maximum = pictureBox1.Width;

// Задаем координаты вершин треугольника

x[0] = 200;

y[0] = 100;

x[1] = 300;

y[1] = 300;

x[2] = 400;

y[2] = 100;

}

}

Пример 7. Произвольное вращение

public Example8()

{

InitializeComponent();

}

Graphics G;

Bitmap bitmap;

static int Vertex_Number = 3;

double[] x = new double[Vertex_Number + 1], y = new double[Vertex_Number + 1], z = new double[Vertex_Number + 1], newX = new double[Vertex_Number + 1], newY = new double[Vertex_Number + 1], newZ = new double[Vertex_Number + 1];

Point[] Point = new Point[Vertex_Number + 1];

static double Factor = Math.PI / 180;

double Theta, u, v, w, A, B, C;

public double RotateObject(double Theta, double A, double B, double C, double u, double v, double w, double x, double y, double z, ref double NewX, ref double NewY)

{

double NewZ;

double L = Pow(u, 2) + Pow(v, 2) + Pow(w, 2);

double SqrL = Sqrt(L);

double Lu = Pow(v, 2) + Pow(w, 2);

double Lv = Pow(u, 2) + Pow(w, 2);

double Lw = Pow(u, 2) + Pow(v, 2);

NewX = (A * Lu + u * (-B * v - C * w + u * x + v * y

+ w * z) + ((x - A) * Lu + u * (B * v + C * w - v * y

- w * z) + Lu * x) * Cos(Theta) + SqrL * (-C * v + B * w - w * y + v * z) * Sin(Theta)) / L;

NewY = (B * Lv + v * (-A * u - C * w + u * x + v * y + w * z) + ((y - B) * Lv + v * (A * u + C * w - u * x - w * z) + Lv * y) * Cos(Theta) + SqrL * (-C * u - A * w + w * x - u * z) * Sin(Theta)) / L;

NewZ = (C * Lw + w * (-A * u - B * v + u * x + v * y + w * z) + ((z - C) * Lw + w * (A * u + B * v - u * x - v * y)

+ Lw * z) * Cos(Theta) + SqrL * (-B * u + A * v - v * x + u * y) * Sin(Theta)) / L;

return NewZ;

}

public void DrawShape(Graphics GraphicObject)

{

newZ[0] = RotateObject(Theta, A, B, C, u, v, w, x[0], y[0], z[0], ref newX[0], ref newY[0]);

newZ[1] = RotateObject(Theta, A, B, C, u, v, w, x[1], y[1], z[1], ref newX[1], ref newY[1]);

newZ[2] = RotateObject(Theta, A, B, C, u, v, w, x[2], y[2], z[2], ref newX[2], ref newY[2]);

Point[0] = new Point((int)newX[0], (int)newY[0]);

Point[1] = new Point((int)newX[1], (int)newY[1]);

Point[2] = new Point((int)newX[2], (int)newY[2]);

Point[] curvePoints = new[] { Point[0], Point[1], Point[2] };

Pen MyPen = new Pen(Color.Red, 2);

SolidBrush myBrush = new SolidBrush(Color.Blue);

GraphicObject.Clear(Color.White);

GraphicObject.DrawPolygon(MyPen, curvePoints);

GraphicObject.FillPolygon(myBrush, curvePoints);

Point MyPoint1 = new Point(System.Convert.ToInt32(A), System.Convert.ToInt32(B));

Point MyPoint2 = new Point(System.Convert.ToInt32(u), System.Convert.ToInt32(v));

Pen blackPen = new Pen(Color.Black, 2);

GraphicObject.DrawLine(blackPen, MyPoint1, MyPoint2);

pictureBox1.Image = bitmap;

}

private void hScrollBarPitch_Scroll(object sender, ScrollEventArgs e)

{

u = Factor * hScrollBarPitch.Value;

DrawShape(G);

}

private void hScrollBarYaw_Scroll(object sender, ScrollEventArgs e)

{

v = Factor * hScrollBarYaw.Value;

DrawShape(G);

}

private void hScrollBarRoll_Scroll(object sender, ScrollEventArgs e)

{

w = Factor * hScrollBarRoll.Value;

DrawShape(G);

}

private void hScrollBarA_Scroll(object sender, ScrollEventArgs e)

{

A = Factor * hScrollBarA.Value;

DrawShape(G);

}

private void hScrollBarB_Scroll(object sender, ScrollEventArgs e)

{

B = Factor * hScrollBarB.Value;

DrawShape(G);

}

private void hScrollBarC_Scroll(object sender, ScrollEventArgs e)

{

C = Factor * hScrollBarC.Value;

DrawShape(G);

}

private void hScrollBarTheta_Scroll(object sender, ScrollEventArgs e)

{

Theta = Factor * hScrollBarTheta.Value;

DrawShape(G);

}

private void Example8_Load(object sender, EventArgs e)

{

bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);

G = Graphics.FromImage(bitmap);

pictureBox1.Image = bitmap;

int Minimum = 1, Maximum = 369;

hScrollBarTheta.Minimum = Minimum;

hScrollBarTheta.Maximum = Maximum;

hScrollBarPitch.Minimum = Minimum;

hScrollBarPitch.Maximum = pictureBox1.Width;

hScrollBarYaw.Minimum = Minimum;

hScrollBarYaw.Maximum = pictureBox1.Width;

hScrollBarRoll.Minimum = Minimum;

hScrollBarRoll.Maximum = pictureBox1.Width;

hScrollBarA.Minimum = Minimum;

hScrollBarA.Maximum = pictureBox1.Width;

hScrollBarB.Minimum = Minimum;

hScrollBarB.Maximum = pictureBox1.Width;

hScrollBarC.Minimum = Minimum;

hScrollBarC.Maximum = pictureBox1.Width;

// Задаем координаты вершин треугольника

x[0] = 200;

y[0] = 100;

x[1] = 300;

y[1] = 300;

x[2] = 400;

y[2] = 100;

}

Пример 8. Визуализация вершин куба

public Example9()

{

InitializeComponent();

}

Graphics G;

Bitmap bitmap;

double Factor = Math.PI / 180;

double Pitch;

double Yaw;

double Roll;

public class Vertex : ArrayList

{

public double x, y, z;

public Vertex(double x, double y, double z) : base()

{

this.x = x;

this.y = y;

this.z = z;

}

}

public class Side

{

public Vertex a, b, c, d;

public Side(Vertex a, Vertex b, Vertex c, Vertex d) : base()

{

this.a = a;

this.b = b;

this.c = c;

this.d = d;

}

}

Vertex[] VertexList = new Vertex[8];

ArrayList SideList = new ArrayList();

public void FillCube()

{

VertexList[0] = (new Vertex(0, 0, 0));

VertexList[1] = (new Vertex(100, 0, 0));

VertexList[2] = (new Vertex(100, 0, 100));

VertexList[3] = (new Vertex(0, 0, 100));

VertexList[4] = (new Vertex(0, 100, 0));

VertexList[5] = (new Vertex(100, 100, 0));

VertexList[6] = (new Vertex(100, 100, 100));

VertexList[7] = (new Vertex(0, 100, 100));

SideList.Add(new Side((Vertex)VertexList[0], (Vertex)VertexList[1], (Vertex)VertexList[2], (Vertex)VertexList[3]));

SideList.Add(new Side((Vertex)VertexList[4], (Vertex)VertexList[5], (Vertex)VertexList[6], (Vertex)VertexList[7]));

SideList.Add(new Side((Vertex)VertexList[6], (Vertex)VertexList[5], (Vertex)VertexList[1], (Vertex)VertexList[2]));

SideList.Add(new Side((Vertex)VertexList[7], (Vertex)VertexList[4], (Vertex)VertexList[0], (Vertex)VertexList[3]));

SideList.Add(new Side((Vertex)VertexList[0], (Vertex)VertexList[4], (Vertex)VertexList[5], (Vertex)VertexList[1]));

SideList.Add(new Side((Vertex)VertexList[7], (Vertex)VertexList[6], (Vertex)VertexList[2], (Vertex)VertexList[3]));

}

public double RotateObject(double Pitch, double Yaw, double Roll, double x, double y, double z, ref double NewX, ref double NewY)

{

double[,] m = new double[4, 4];

m[1, 1] = Math.Cos(Yaw) * Math.Cos(Roll);

m[1, 2] = -Math.Cos(Yaw) * Math.Sin(Roll);

m[1, 3] = -Math.Sin(Yaw);

m[2, 1] = Math.Sin(Pitch) * Math.Sin(Yaw) * Math.Cos(Roll) + Math.Sin(Roll) * Math.Cos(Pitch);

m[2, 2] = -Math.Sin(Pitch) * Math.Sin(Yaw) * Math.Sin(Roll) + Math.Cos(Roll) * Math.Cos(Pitch);

m[2, 3] = Math.Cos(Yaw);

m[3, 1] = -Math.Cos(Pitch) * Math.Sin(Yaw) * Math.Cos(Roll) + Math.Sin(Pitch) * Math.Sin(Roll);

m[3, 2] = Math.Cos(Pitch) * Math.Sin(Yaw) * Math.Sin(Roll) + Math.Sin(Pitch) * Math.Cos(Roll);

m[3, 3] = Math.Cos(Yaw) * Math.Cos(Pitch);

double NewZ;

NewX = m[1, 1] * x + m[2, 1] * y + m[3, 1] * z;

NewY = m[1, 2] * x + m[2, 2] * y + m[3, 2] * z;

NewZ = m[1, 3] * x + m[2, 3] * y + m[3, 3] * z;

return NewZ;

}

public void DrawShape()

{

Vertex ve = new Vertex(0, 0, 0);

double h = ve.x;

int ArraySize;

ArraySize = 8;

double[] NewX = new double[ArraySize + 1], NewY = new double[ArraySize + 1], NewZ = new double[ArraySize + 1];

int i;

Pen MyPen = new Pen(Color.Red, 2);

SolidBrush MyBrush = new SolidBrush(Color.Blue);

G.Clear(Color.White);

Matrix myMatrix = new Matrix();

myMatrix.Translate(pictureBox1.Width / 2, pictureBox1.Height / 2, MatrixOrder.Append);

G.Transform = myMatrix;

for (i = 0; i <= ArraySize - 1; i++)

{

NewZ[i] = RotateObject(Pitch, Yaw, Roll, VertexList[i].x, VertexList[i].y, VertexList[i].z, ref NewX[i], ref NewY[i]);

Rectangle MyBox = new Rectangle((int)NewX[i], (int)NewY[i], 10, 10);

G.DrawEllipse(MyPen, MyBox);

G.FillEllipse(MyBrush, MyBox);

}

pictureBox1.Image = bitmap;

}

private void hScrollBarPitch_Scroll(object sender, ScrollEventArgs e)

{

Pitch = Factor * hScrollBarPitch.Value;

DrawShape();

}

private void hScrollBarYaw_Scroll(object sender, ScrollEventArgs e)

{

Yaw = Factor * hScrollBarYaw.Value;

DrawShape();

}

private void hScrollBarRoll_Scroll(object sender, ScrollEventArgs e)

{

Roll = Factor * hScrollBarRoll.Value;

DrawShape();

}

private void Example9_Load(object sender, EventArgs e)

{

bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);

G = Graphics.FromImage(bitmap);

pictureBox1.Image = bitmap;

FillCube();

int Minimum = 0, Maximum = 369;

hScrollBarPitch.Minimum = Minimum;

hScrollBarPitch.Maximum = Maximum;

hScrollBarYaw.Minimum = Minimum;

hScrollBarYaw.Maximum = Maximum;

hScrollBarRoll.Minimum = Minimum;

hScrollBarRoll.Maximum = Maximum;

}

}

Пример 9. Визуализация граней куба

public Example10()

{

InitializeComponent();

}

Graphics G;

double Factor = Math.PI / 180;

double Pitch, Yaw, Roll;

public class Vertex

{

public int x, y, z;

public Vertex(int x, int y, int z) : base()

{

this.x = x;

this.y = y;

this.z = z;

}

}

public class Surface

{

public Vertex x1, x2, x3, x4;

public Surface(Vertex x1, Vertex x2, Vertex x3, Vertex x4) : base()

{

this.x1 = x1;

this.x2 = x2;

this.x3 = x3;

this.x4 = x4;

}

}

ArrayList Surface_List = new ArrayList();

public void Fill_Surface()

{

ArrayList SurfList = new ArrayList();

Surface_List.Add(new Surface(new Vertex(0, 0, 0), new Vertex(100, 0, 0), new Vertex(100, 0, 100), new Vertex(0, 0, 100)));

Surface_List.Add(new Surface(new Vertex(0, 0, 100), new Vertex(0, 100, 100), new Vertex(100, 100, 100), new Vertex(100, 0, 100)));

Surface_List.Add(new Surface(new Vertex(0, 0, 100), new Vertex(0, 100, 100), new Vertex(0, 100, 0), new Vertex(0, 0, 0)));

Surface_List.Add(new Surface(new Vertex(0, 0, 0), new Vertex(0, 100, 0), new Vertex(100, 100, 0), new Vertex(100, 0, 0)));

Surface_List.Add(new Surface(new Vertex(100, 0, 0), new Vertex(100, 0, 100), new Vertex(100, 100, 100), new Vertex(100, 100, 0)));

Surface_List.Add(new Surface(new Vertex(0, 100, 100), new Vertex(0, 100, 0), new Vertex(100, 100, 0), new Vertex(100, 100, 100)));

}

public Vertex RotateVertex(double Pitch, double Yaw, double Roll, Vertex MyVertex)

{

int x, y, z;

int NewX, NewY, NewZ;

x = MyVertex.x;

y = MyVertex.y;

z = MyVertex.z;

double[,] m = new double[4, 4];

m[1, 1] = Cos(Yaw) * Cos(Roll);

m[1, 2] = -Cos(Yaw) * Sin(Roll);

m[1, 3] = -Sin(Yaw);

m[2, 1] = Sin(Pitch) * Sin(Yaw) * Cos(Roll) + Sin(Roll)

* Cos(Pitch);

m[2, 2] = -Sin(Pitch) * Sin(Yaw) * Sin(Roll) + Cos(Roll)

* Cos(Pitch);

m[2, 3] = Cos(Yaw);

m[3, 1] = -Cos(Pitch) * Sin(Yaw) * Cos(Roll) + Sin(Pitch)

* Sin(Roll);

m[3, 2] = Cos(Pitch) * Sin(Yaw) * Sin(Roll) + Sin(Pitch)

* Cos(Roll);

m[3, 3] = Cos(Yaw) * Cos(Pitch);

NewX = System.Convert.ToInt32(m[1, 1] * x + m[2, 1] * y + m[3, 1] * z);

NewY = System.Convert.ToInt32(m[1, 2] * x + m[2, 2] * y + m[3, 2] * z);

NewZ = System.Convert.ToInt32(m[1, 3] * x + m[2, 3] * y + m[3, 3] * z);

Vertex NewVertex = new Vertex(NewX, NewY, NewZ);

return NewVertex;

}

public Surface RotateSurface(double Pitch, double Yaw, double Roll, Surface MySurface)

{

Vertex x1, x2, x3, x4;

x1 = MySurface.x1;

x2 = MySurface.x2;

x3 = MySurface.x3;

x4 = MySurface.x4;

Vertex NewX1, NewX2, NewX3, NewX4;

NewX1 = RotateVertex(Pitch, Yaw, Roll, x1);

NewX2 = RotateVertex(Pitch, Yaw, Roll, x2);

NewX3 = RotateVertex(Pitch, Yaw, Roll, x3);

NewX4 = RotateVertex(Pitch, Yaw, Roll, x4);

Surface NewSurface = new Surface(NewX1, NewX2, NewX3, NewX4);

return NewSurface;

}

public void DrawShape(Graphics GraphicObject)

{

int Surface_Count;

Surface_Count = Surface_List.Count;

int i;

SolidBrush[] MyBrush = new[] { new SolidBrush(Color.Blue), new SolidBrush(Color.Red), new SolidBrush(Color.Black), new SolidBrush(Color.Purple), new SolidBrush(Color.Orchid), new SolidBrush(Color.Green) };

Pen[] MyPen = new[] { new Pen(Color.Blue, 1), new Pen(Color.Red, 1), new Pen(Color.Black, 1), new Pen(Color.Purple, 1), new Pen(Color.Orchid, 1), new Pen(Color.Green, 1) };

GraphicObject.Clear(Color.White);

for (i = 0; i <= Surface_Count - 1; i++)

{

Surface x, newx;

x = (Surface)Surface_List[i];

newx = RotateSurface(Pitch, Yaw, Roll, x);

PointF[] curvePoints = new[] { new PointF(newx.x1.x, newx.x1.y), new PointF(newx.x2.x, newx.x2.y), new PointF(newx.x3.x, newx.x3.y), new PointF(newx.x4.x, newx.x4.y) };

FillMode newFillMode = FillMode.Winding;

G.DrawPolygon(MyPen[i], curvePoints);

}

int XStart, YStart;

XStart = pictureBox1.Width / 2;

YStart = pictureBox1.Height / 2;

Matrix MyMatrix = new Matrix();

MyMatrix.Translate(XStart, YStart);

GraphicObject.Transform = MyMatrix;

}

private void hScrollBarPitch_Scroll(object sender, ScrollEventArgs e)

{

//Pitch = hScrollBarPitch.Value;

Pitch = Factor * hScrollBarPitch.Value;

DrawShape(G);

}

private void hScrollBarYaw_Scroll(object sender, ScrollEventArgs e)

{

//Yaw = hScrollBarYaw.Value;

Yaw = Factor * hScrollBarYaw.Value;

DrawShape(G);

}

private void hScrollBarRoll_Scroll(object sender, ScrollEventArgs e)

{

//Roll = hScrollBarRoll.Value;

Roll = Factor * hScrollBarRoll.Value;

DrawShape(G);

}

private void Example10_Load(object sender, EventArgs e)

{

G = pictureBox1.CreateGraphics();

int Minimum = 0, Maximum = 369;

hScrollBarPitch.Minimum = Minimum;

hScrollBarPitch.Maximum = Maximum;

hScrollBarYaw.Minimum = Minimum;

hScrollBarYaw.Maximum = Maximum;

hScrollBarRoll.Minimum = Minimum;

hScrollBarRoll.Maximum = Maximum;

Fill_Surface();

}

}

Пример 10. Вращение вокруг вектора

public Example6()

{

InitializeComponent();

}

Graphics G;

public double RotateObject(double Theta, double u, double v, double w, double x, double y, double z, ref double NewX, ref double NewY)

{

double NewZ;

double a, b, c, d, e;

a = u * x + v * y + w * z;

b = Pow(u, 2) + Pow(v, 2) + Pow(w, 2);

c = Sqrt(b);

d = Cos(Theta);

e = Sin(Theta);

NewX = (u * a + (x * (Pow(v, 2) + Pow(w, 2) - u * (v * y + w * z)) * d + c * (-w * y + v * z) * e)) / b;

NewY = (v * a + (y * (Pow(u, 2) + Pow(w, 2) - v * (u * x + w * z)) * d + c * (w * x - u * z) * e)) / b;

NewZ = (w * a + (z * (Pow(u, 2) + Pow(v, 2) - w * (u * x + v * y)) * d + c * (-v * x + u * y) * e)) / b;

return NewZ;

}

static int Vertex_Number = 3;

double[] x = new double[Vertex_Number + 1];

double[] y = new double[Vertex_Number + 1];

double[] z = new double[Vertex_Number + 1];

double[] newX = new double[Vertex_Number + 1];

double[] newY = new double[Vertex_Number + 1];

double[] newZ = new double[Vertex_Number + 1];

Point[] Point = new Point[Vertex_Number + 1];

double Factor = Math.PI / 180;

public void DrawShape(Graphics GraphicObject)

{

newZ[0] = RotateObject(Theta, u, v, w, x[0], y[0], z[0], ref newX[0], ref newY[0]);

newZ[1] = RotateObject(Theta, u, v, w, x[1], y[1], z[1], ref newX[1], ref newY[1]);

newZ[2] = RotateObject(Theta, u, v, w, x[2], y[2], z[2], ref newX[2], ref newY[2]);

Point[0] = new Point((int)newX[0], (int)newY[0]);

Point[1] = new Point((int)newX[1], (int)newY[1]);

Point[2] = new Point((int)newX[2], (int)newY[2]);

Point[] curvePoints = new[] { Point[0], Point[1], Point[2] };

Pen MyPen = new Pen(Color.Red, 2);

SolidBrush myBrush = new SolidBrush(Color.Blue);

GraphicObject.Clear(Color.White);

GraphicObject.DrawPolygon(MyPen, curvePoints);

GraphicObject.FillPolygon(myBrush, curvePoints);

Point MyPoint1 = new Point(0, 0);

Point MyPoint2 = new Point(System.Convert.ToInt32(u), System.Convert.ToInt32(v));

GraphicObject.DrawLine(MyPen, MyPoint1, MyPoint2);

}

double Theta, u, v, w;

private void hScrollBarPitch_Scroll(object sender, ScrollEventArgs e)

{

u = Factor * hScrollBarPitch.Value;

DrawShape(G);

}

private void hScrollBarYaw_Scroll(object sender, ScrollEventArgs e)

{

v = Factor * hScrollBarYaw.Value;

DrawShape(G);

}

private void hScrollBarRoll_Scroll(object sender, ScrollEventArgs e)

{

w = Factor * hScrollBarRoll.Value;

DrawShape(G);

}

private void hScrollBarTheta_Scroll(object sender, ScrollEventArgs e)

{

Theta = Factor * hScrollBarTheta.Value;

DrawShape(G);

}

private void Example6_Load(object sender, EventArgs e)

{

G = pictureBox1.CreateGraphics();

int Minimum = 1, Maximum = 369;

hScrollBarTheta.Minimum = Minimum;

hScrollBarTheta.Maximum = Maximum;

hScrollBarPitch.Minimum = Minimum;

hScrollBarPitch.Maximum = pictureBox1.Width;

hScrollBarYaw.Minimum = Minimum;

hScrollBarYaw.Maximum = pictureBox1.Width;

hScrollBarRoll.Minimum = Minimum;

hScrollBarRoll.Maximum = pictureBox1.Width;

// Задаем координаты вершин треугольника

x[0] = 200;

y[0] = 100;

x[1] = 300;

y[1] = 300;

x[2] = 400;

y[2] = 100;

}

Пример 11. Изображение сторон куба

public Example11()

{

InitializeComponent();

}

Graphics G;

double Factor = Math.PI / 180;

double Pitch;

double Yaw;

double Roll;

public class VertexComparer : IComparer

{

public int Compare(object o1, object o2)

{

NewSurface z1, z2;

try

{

z1 = (NewSurface)o1;

z2 = (NewSurface)o2;

}

catch (Exception compareException)

{

throw (compareException);

// return;

}

if (z1.z < z2.z)

{

return 1;

}

else if (z1.z > z2.z)

{

return -1;

}

else

return 0;

}

}

public class Vertex : ArrayList

{

public int x;

public int y;

public int z;

public int x1, x2, x3, x4;

public Vertex(int x, int y, int z) : base()

{

this.x = x;

this.y = y;

this.z = z;

}

}

public class Surface : ArrayList

{

public Vertex x1;

public Vertex x2;

public Vertex x3;

public Vertex x4;

public Surface()

{

}

public Surface(Vertex x1, Vertex x2, Vertex x3, Vertex x4) : base()

{

this.x1 = x1;

this.x2 = x2;

this.x3 = x3;

this.x4 = x4;

}

}

public class NewSurface : ArrayList

{

public Surface x;

public int z;

public NewSurface()

{

}

public NewSurface(Surface x, int z) : base()

{

this.x = x;

this.z = z;

}

}

private ArrayList Surface_List = new ArrayList();

// private ArrayList Surface_List = new Surface();

public void Fill_Surface()

{

ArrayList SurfList = new ArrayList();

Surface_List.Add(new Surface(new Vertex(0, 0, 0), new Vertex(100, 0, 0), new Vertex(100, 0, 100), new Vertex(0, 0, 100)));

Surface_List.Add(new Surface(new Vertex(0, 0, 100), new Vertex(0, 100, 100), new Vertex(100, 100, 100), new Vertex(100, 0, 100)));

Surface_List.Add(new Surface(new Vertex(0, 0, 100), new Vertex(0, 100, 100), new Vertex(0, 100, 0), new Vertex(0, 0, 0)));

Surface_List.Add(new Surface(new Vertex(0, 0, 0), new Vertex(0, 100, 0), new Vertex(100, 100, 0), new Vertex(100, 0, 0)));

Surface_List.Add(new Surface(new Vertex(100, 0, 0), new Vertex(100, 0, 100), new Vertex(100, 100, 100), new Vertex(100, 100, 0)));

Surface_List.Add(new Surface(new Vertex(0, 100, 100), new Vertex(0, 100, 0), new Vertex(100, 100, 0), new Vertex(100, 100, 100)));

}

public Vertex RotateVertex(double Pitch, double Yaw, double Roll, Vertex MyVertex)

{

int x, y, z;

int NewX, NewY, NewZ;

x = MyVertex.x;

y = MyVertex.y;

z = MyVertex.z;

double[,] m = new double[4, 4];

m[1, 1] = Cos(Yaw) * Cos(Roll);

m[1, 2] = -Cos(Yaw) * Sin(Roll);

m[1, 3] = -Sin(Yaw);

m[2, 1] = Sin(Pitch) * Sin(Yaw) * Cos(Roll) + Sin(Roll) * Cos(Pitch);

m[2, 2] = -Sin(Pitch) * Sin(Yaw) * Sin(Roll) + Cos(Roll) * Cos(Pitch);

m[2, 3] = Cos(Yaw);

m[3, 1] = -Cos(Pitch) * Sin(Yaw) * Cos(Roll) + Sin(Pitch) * Sin(Roll);

m[3, 2] = Cos(Pitch) * Sin(Yaw) * Sin(Roll) + Sin(Pitch) * Cos(Roll);

m[3, 3] = Cos(Yaw) * Cos(Pitch);

NewX = System.Convert.ToInt32(m[1, 1] * x + m[2, 1] * y + m[3, 1] * z);

NewY = System.Convert.ToInt32(m[1, 2] * x + m[2, 2] * y + m[3, 2] * z);

NewZ = System.Convert.ToInt32(m[1, 3] * x + m[2, 3] * y + m[3, 3] * z);

Vertex NewVertex = new Vertex(NewX, NewY, NewZ);

return NewVertex;

}

public Surface RotateSurface(double Pitch, double Yaw, double Roll, Surface MySurface)

{

Vertex x1, x2, x3, x4;

x1 = MySurface.x1;

x2 = MySurface.x2;

x3 = MySurface.x3;

x4 = MySurface.x4;

Vertex NewX1, NewX2, NewX3, NewX4;

NewX1 = RotateVertex(Pitch, Yaw, Roll, x1);

NewX2 = RotateVertex(Pitch, Yaw, Roll, x2);

NewX3 = RotateVertex(Pitch, Yaw, Roll, x3);

NewX4 = RotateVertex(Pitch, Yaw, Roll, x4);

Surface NewSurface = new Surface(NewX1, NewX2, NewX3, NewX4);

return NewSurface;

}

public void DrawShape(Graphics GraphicObject)

{

int Surface_Count;

Surface_Count = Surface_List.Count;

ArrayList New_Surface_List = new ArrayList();

//ArrayList New_Surface_List = new NewSurface();

int i;

SolidBrush[] MyBrush = new[]

{

new SolidBrush(Color.Blue),

new SolidBrush(Color.Red),

new SolidBrush(Color.Black),

new SolidBrush(Color.Purple),

new SolidBrush(Color.Orchid),

new SolidBrush(Color.Green)

};

GraphicObject.Clear(Color.White);

Surface[] x = new Surface[Surface_Count + 1], newx = new Surface[Surface_Count + 1];

for (i = 0; i <= Surface_Count - 1; i++)

{

x[i] = (Surface)Surface_List[i];

int a, b, c;

a = (((Surface)Surface_List[i]).x1.x + ((Surface)Surface_List[i]).x2.x + ((Surface)Surface_List[i]).x3.x + ((Surface)Surface_List[i]).x4.x) / 4;

b = (((Surface)Surface_List[i]).x1.y + ((Surface)Surface_List[i]).x2.y + ((Surface)Surface_List[i]).x3.y + ((Surface)Surface_List[i]).x4.y) / 4;

c = (((Surface)Surface_List[i]).x1.z + ((Surface)Surface_List[i]).x2.z + ((Surface)Surface_List[i]).x3.z + ((Surface)Surface_List[i]).x4.z) / 4;

Vertex V = new Vertex(a, b, c);

Vertex NewV;

NewV = RotateVertex(Pitch, Yaw, Roll, V);

int z;

z = NewV.z;

newx[i] = RotateSurface(Pitch, Yaw, Roll, x[i]);

New_Surface_List.Add(new NewSurface(newx[i], z));

}

New_Surface_List.Sort(new VertexComparer());

for (i = 0; i <= Surface_Count - 1; i++)

{

PointF[] curvePoints = new[]

{

new PointF(((NewSurface)New_Surface_List[i]).x.x1.x, ((NewSurface)New_Surface_List[i]).x.x1.y),

new PointF(((NewSurface)New_Surface_List[i]).x.x2.x, ((NewSurface)New_Surface_List[i]).x.x2.y),

new PointF(((NewSurface)New_Surface_List[i]).x.x3.x, ((NewSurface)New_Surface_List[i]).x.x3.y),

new PointF(((NewSurface)New_Surface_List[i]).x.x4.x, ((NewSurface)New_Surface_List[i]).x.x4.y)

};

FillMode newFillMode = FillMode.Winding;

G.FillPolygon(MyBrush[i], curvePoints, newFillMode);

}

int XStart, YStart;

XStart = pictureBox1.Width / 2;

YStart = pictureBox1.Height / 2;

Matrix MyMatrix = new Matrix();

MyMatrix.Translate(XStart, YStart);

GraphicObject.Transform = MyMatrix;

}

private void Example11_Load(object sender, EventArgs e)

{

G = pictureBox1.CreateGraphics();

int Minimum, Maximum;

Minimum = 0;

Maximum = 369;

hScrollBarPitch.Minimum = Minimum;

hScrollBarPitch.Maximum = Maximum;

hScrollBarYaw.Minimum = Minimum;

hScrollBarYaw.Maximum = Maximum;

hScrollBarRoll.Minimum = Minimum;

hScrollBarRoll.Maximum = Maximum;

Fill_Surface();

}

private void hScrollBarPitch_Scroll(object sender, ScrollEventArgs e)

{

//Pitch = hScrollBarPitch.Value;

Pitch = Factor * hScrollBarPitch.Value;

DrawShape(G);

}

private void hScrollBarYaw_Scroll(object sender, ScrollEventArgs e)

{

//Yaw = hScrollBarYaw.Value;

Yaw = Factor * hScrollBarYaw.Value;

DrawShape(G);

}

private void hScrollBarRoll_Scroll(object sender, ScrollEventArgs e)

{

//Roll = hScrollBarRoll.Value;

Roll = Factor * hScrollBarRoll.Value;

DrawShape(G);

}

}

Пример 12. Параллелепипед, как совокупность точек

public Example12()

{

InitializeComponent();

}

Graphics G;

Bitmap bitmap;

double Factor = Math.PI / 180;

public double Pitch;

public double Yaw;

public double Roll;

public double RotateObject(double Pitch, double Yaw, double Roll, double x, double y, double z, ref double NewX, ref double NewY)

{

double[,] m = new double[4, 4];

m[1, 1] = Cos(Yaw) * Cos(Roll);

m[1, 2] = -Cos(Yaw) * Sin(Roll);

m[1, 3] = -Sin(Yaw);

m[2, 1] = Sin(Pitch) * Sin(Yaw) * Cos(Roll) + Sin(Roll)

* Cos(Pitch);

m[2, 2] = -Sin(Pitch) * Sin(Yaw) * Sin(Roll) + Cos(Roll)

* Cos(Pitch);

m[2, 3] = Cos(Yaw);

m[3, 1] = -Cos(Pitch) * Sin(Yaw) * Cos(Roll) + Sin(Pitch)

* Sin(Roll);

m[3, 2] = Cos(Pitch) * Sin(Yaw) * Sin(Roll) + Sin(Pitch)

* Cos(Roll);

m[3, 3] = Cos(Yaw) * Cos(Pitch);

double NewZ;

NewX = m[1, 1] * x + m[2, 1] * y + m[3, 1] * z;

NewY = m[1, 2] * x + m[2, 2] * y + m[3, 2] * z;

NewZ = m[1, 3] * x + m[2, 3] * y + m[3, 3] * z;

return NewZ;

}

public void DrawShape(Graphics GraphicObject)

{

int x0, y0, z0; // Координаты начала отсчета

x0 = 20;

y0 = 10;

z0 = 5;

int a, b, c; // Длины сторон параллепипеда

a = 100;

b = 100;

c = 100;

int i, j, k, l;

int m; // Шаг итерации

m = 3;

Pen MyPen = new Pen(Color.Red);

Pen MyPen1 = new Pen(Color.Blue, 1);

Pen MyPen2 = new Pen(Color.Red, 1);

Pen MyPen3 = new Pen(Color.Green, 1);

Pen MyPen4 = new Pen(Color.Black, 1);

Pen MyPen5 = new Pen(Color.Yellow, 1);

Pen MyPen6 = new Pen(Color.Orchid, 1);

GraphicObject.Clear(Color.White);

for (i = x0; i <= x0 + a; i += m)

{

for (j = y0; j <= y0 + b; j += m)

{

double newx1 = 0, newy1 = 0, newz1;

newz1 = RotateObject(Pitch, Yaw, Roll, i, j, z0, ref newx1, ref newy1);

Rectangle MyBox = new Rectangle((int)newx1, (int)newy1, m, m);

GraphicObject.DrawEllipse(MyPen5, MyBox);

}

}

for (i = z0 + m; i <= z0 + c - m; i += m)

{

for (j = y0; j <= y0 + b; j += m)

{

double newx1 = 0, newy1 = 0, newz1;

double newx2 = 0, newy2 = 0, newz2;

newz1 = RotateObject(Pitch, Yaw, Roll, x0, j, i, ref newx1, ref newy1);

newz2 = RotateObject(Pitch, Yaw, Roll, x0 + a, j, i, ref newx2, ref newy2);

Rectangle MyBox1 = new Rectangle(System.Convert.ToInt32(newx1), System.Convert.ToInt32(newy1), m, m);

Rectangle MyBox2 = new Rectangle(System.Convert.ToInt32(newx2), System.Convert.ToInt32(newy2), m, m);

GraphicObject.DrawEllipse(MyPen1, MyBox1);

GraphicObject.DrawEllipse(MyPen2, MyBox2);

}

for (k = x0; k <= x0 + a; k += m)

{

double newx1 = 0, newy1 = 0, newz1;

double newx2 = 0, newy2 = 0, newz2;

newz1 = RotateObject(Pitch, Yaw, Roll, k, y0, i, ref newx1, ref newy1);

newz2 = RotateObject(Pitch, Yaw, Roll, k, y0 + b, i, ref newx2, ref newy2);

Rectangle MyBox3 = new Rectangle(System.Convert.ToInt32(newx1), System.Convert.ToInt32(newy1), m, m);

Rectangle MyBox4 = new Rectangle(System.Convert.ToInt32(newx2), System.Convert.ToInt32(newy2), m, m);

GraphicObject.DrawEllipse(MyPen3, MyBox3);

GraphicObject.DrawEllipse(MyPen4, MyBox4);

}

}

for (i = x0; i <= x0 + a; i += m)

{

for (j = y0; j <= y0 + b; j += m)

{

double newx1 = 0, newy1 = 0, newz1;

newz1 = RotateObject(Pitch, Yaw, Roll, i, j, z0 + c, ref newx1, ref newy1);

Rectangle MyBox = new Rectangle((int)newx1, (int)newy1, m, m);

GraphicObject.DrawEllipse(MyPen6, MyBox);

}

}

Matrix myMatrix = new Matrix();

myMatrix.Translate(pictureBox1.Width / 2, pictureBox1.Height / 2, MatrixOrder.Append);

G.Transform = myMatrix;

pictureBox1.Image = bitmap;

}

private void hScrollBarPitch_Scroll(object sender, ScrollEventArgs e)

{

Pitch = Factor * hScrollBarPitch.Value;

DrawShape(G);

}

private void hScrollBarYaw_Scroll(object sender, ScrollEventArgs e)

{

Yaw = Factor * hScrollBarYaw.Value;

DrawShape(G);

}

private void hScrollBarRoll_Scroll(object sender, ScrollEventArgs e)

{

Roll = Factor * hScrollBarRoll.Value;

DrawShape(G);

}

private void Example12_Load(object sender, EventArgs e)

{

bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);

G = Graphics.FromImage(bitmap);

pictureBox1.Image = bitmap;

int minimum = 0, maximum = 369;

hScrollBarPitch.Minimum = minimum;

hScrollBarPitch.Maximum = maximum;

hScrollBarYaw.Minimum = minimum;

hScrollBarYaw.Maximum = maximum;

hScrollBarRoll.Minimum = minimum;

hScrollBarRoll.Maximum = maximum;

}

}

Пример 13. Пирамида, как совокупность точек

public Example13()

{

InitializeComponent();

}

Graphics G;

Bitmap bitmap;

private double Factor = Math.PI / 180;

private double Pitch;

private double Yaw;

private double Roll;

public double RotateObject(double Pitch, double Yaw, double Roll, double x, double y, double z, ref double NewX, ref double NewY)

{

double[,] m = new double[4, 4];

m[1, 1] = Cos(Yaw) * Cos(Roll);

m[1, 2] = -Cos(Yaw) * Sin(Roll);

m[1, 3] = -Sin(Yaw);

m[2, 1] = Sin(Pitch) * Sin(Yaw) * Cos(Roll) + Sin(Roll)

* Cos(Pitch);

m[2, 2] = -Sin(Pitch) * Sin(Yaw) * Sin(Roll) + Cos(Roll)

* Cos(Pitch);

m[2, 3] = Cos(Yaw);

m[3, 1] = -Cos(Pitch) * Sin(Yaw) * Cos(Roll) + Sin(Pitch)

* Sin(Roll);

m[3, 2] = Cos(Pitch) * Sin(Yaw) * Sin(Roll) + Sin(Pitch)

* Cos(Roll);

m[3, 3] = Cos(Yaw) * Cos(Pitch);

double NewZ;

NewX = m[1, 1] * x + m[2, 1] * y + m[3, 1] * z;

NewY = m[1, 2] * x + m[2, 2] * y + m[3, 2] * z;

NewZ = m[1, 3] * x + m[2, 3] * y + m[3, 3] * z;

return NewZ;

}

public void DrawShape(Graphics GraphicObject)

{

int x0, y0, z0, h, d, m;

x0 = 0;

y0 = 0;

z0 = 0;

h = 150;

d = 70;

m = 3;

GraphicObject.Clear(Color.White);

double i, j, k;

Pen MyPen1 = new Pen(Color.Blue, 1);

Pen MyPen2 = new Pen(Color.Red, 1);

Pen MyPen3 = new Pen(Color.Green, 1);

Pen MyPen4 = new Pen(Color.Black, 1);

double XMin, Xmax;

for (i = z0; i <= z0 + h; i += m)

{

XMin = -(z0 + i) * d / (2 * h);

Xmax = (z0 + i) * d / (2 * h);

for (j = XMin; j <= Xmax; j += m)

{

double newx1 = 0, newy1 = 0, newz1;

double newx2 = 0, newy2 = 0, newz2;

newz1 = RotateObject(Pitch, Yaw, Roll, XMin, j, i, ref newx1, ref newy1);

newz2 = RotateObject(Pitch, Yaw, Roll, Xmax, j, i, ref newx2, ref newy2);

Rectangle MyBox1 = new Rectangle(System.Convert.ToInt32(newx1), System.Convert.ToInt32(newy1), 1, 1);

Rectangle MyBox2 = new Rectangle(System.Convert.ToInt32(newx2), System.Convert.ToInt32(newy2), 1, 1);

GraphicObject.DrawEllipse(MyPen1, MyBox1);

GraphicObject.DrawEllipse(MyPen2, MyBox2);

}

for (k = XMin; k <= Xmax; k += m)

{

double newx1 = 0, newy1 = 0, newz1;

double newx2 = 0, newy2 = 0, newz2;

newz1 = RotateObject(Pitch, Yaw, Roll, k, Xmax, i, ref newx1, ref newy1);

newz2 = RotateObject(Pitch, Yaw, Roll, k, XMin, i, ref newx2, ref newy2);

Rectangle MyBox3 = new Rectangle(System.Convert.ToInt32(newx1), System.Convert.ToInt32(newy1), 1, 1);

Rectangle MyBox4 = new Rectangle(System.Convert.ToInt32(newx2), System.Convert.ToInt32(newy2), 1, 1);

GraphicObject.DrawEllipse(MyPen3, MyBox3);

GraphicObject.DrawEllipse(MyPen4, MyBox4);

}

}

XMin = -(z0 + h) * d / (double)(2 * h);

Xmax = (z0 + h) * d / (double)(2 * h);

Pen MyPen5 = new Pen(Color.Brown, 1);

for (j = XMin; j <= Xmax; j += m)

{

double newx1 = 0, newy1 = 0, newz1;

double newx2 = 0, newy2 = 0, newz2;

newz1 = RotateObject(Pitch, Yaw, Roll, j, XMin, z0 + h, ref newx1, ref newy1);

newz2 = RotateObject(Pitch, Yaw, Roll, j, Xmax, z0 + h, ref newx2, ref newy2);

GraphicObject.DrawLine(MyPen5, (float)newx1, (float)newy1, (float)newx2, (float)newy2);

}

Matrix myMatrix = new Matrix();

myMatrix.Translate(pictureBox1.Width / 2, pictureBox1.Height / 2, MatrixOrder.Append);

G.Transform = myMatrix;

pictureBox1.Image = bitmap;

}

private void hScrollBarPitch_Scroll(object sender, ScrollEventArgs e)

{

Pitch = Factor * hScrollBarPitch.Value;

DrawShape(G);

}

private void hScrollBarYaw_Scroll(object sender, ScrollEventArgs e)

{

Yaw = Factor * hScrollBarYaw.Value;

DrawShape(G);

}

private void hScrollBarRoll_Scroll(object sender, ScrollEventArgs e)

{

Roll = Factor * hScrollBarRoll.Value;

DrawShape(G);

}

private void Example13_Load(object sender, EventArgs e)

{

bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);

G = Graphics.FromImage(bitmap);

pictureBox1.Image = bitmap;

int minimum = 0, maximum = 369;

hScrollBarPitch.Minimum = minimum;

hScrollBarPitch.Maximum = maximum;

hScrollBarYaw.Minimum = minimum;

hScrollBarYaw.Maximum = maximum;

hScrollBarRoll.Minimum = minimum;

hScrollBarRoll.Maximum = maximum;

}

}

Пример 14. Цилиндр, как совокупность точек

public Example14()

{

InitializeComponent();

}

Graphics G;

Bitmap bitmap;

private double Factor = Math.PI / 180;

private double Pitch;

private double Yaw;

private double Roll;

public double RotateObject(double Pitch, double Yaw, double Roll, double x, double y, double z, ref double NewX, ref double NewY)

{

double[,] m = new double[4, 4];

m[1, 1] = Cos(Yaw) * Cos(Roll);

m[1, 2] = -Cos(Yaw) * Sin(Roll);

m[1, 3] = -Sin(Yaw);

m[2, 1] = Sin(Pitch) * Sin(Yaw) * Cos(Roll) + Sin(Roll)

* Cos(Pitch);

m[2, 2] = -Sin(Pitch) * Sin(Yaw) * Sin(Roll) + Cos(Roll)

* Cos(Pitch);

m[2, 3] = Cos(Yaw);

m[3, 1] = -Cos(Pitch) * Sin(Yaw) * Cos(Roll) + Sin(Pitch)

* Sin(Roll);

m[3, 2] = Cos(Pitch) * Sin(Yaw) * Sin(Roll) + Sin(Pitch)

* Cos(Roll);

m[3, 3] = Cos(Yaw) * Cos(Pitch);

double NewZ;

NewX = m[1, 1] * x + m[2, 1] * y + m[3, 1] * z;

NewY = m[1, 2] * x + m[2, 2] * y + m[3, 2] * z;

NewZ = m[1, 3] * x + m[2, 3] * y + m[3, 3] * z;

return NewZ;

}

public void DrawShape(Graphics GraphicObject)

{

int x0, y0, z0, R, L, m;

x0 = 5;

y0 = 5;

z0 = 5;

R = 50;

L = 100;

m = 3;

GraphicObject.Clear(Color.White);

int i, j;

for (i = x0 - R; i <= x0 + R; i += m)

{

int y1, y2;

y1 = (int)(y0 - Math.Sqrt(Math.Pow(R, 2) - Math.Pow((i - x0), 2)));

y2 = (int)(y0 + Math.Sqrt(Math.Pow(R, 2) - Math.Pow((i - x0), 2)));

double x11 = 0, y11 = 0, z11;

double x21 = 0, y21 = 0, z21;

z11 = RotateObject(Pitch, Yaw, Roll, i, y1, z0, ref x11, ref y11);

z21 = RotateObject(Pitch, Yaw, Roll, i, y2, z0, ref x21, ref y21);

Rectangle MyBox1 = new Rectangle((int)x11, (int)y11, m, m);

Rectangle MyBox2 = new Rectangle((int)x21, (int)y21, m, m);

Pen MyPen1 = new Pen(Color.Green, 1);

Pen MyPen2 = new Pen(Color.Orchid, 1);

GraphicObject.DrawLine(MyPen1, (float)x11, (float)y11, (float)x21, (float)y21);

GraphicObject.DrawEllipse(MyPen1, MyBox1);

GraphicObject.DrawEllipse(MyPen2, MyBox2);

}

for (i = z0 - m; i <= z0 + L - m; i += m)

{

for (j = x0 - R; j <= x0 + R; j += m)

{

int y1, y2;

y1 = (int)(y0 - Math.Sqrt(Math.Pow(R, 2) - Math.Pow((j - x0), 2)));

y2 = (int)(y0 + Math.Sqrt(Math.Pow(R, 2) - Math.Pow((j - x0), 2)));

double x11 = 0, y11 = 0, z11;

double x21 = 0, y21 = 0, z21;

z11 = RotateObject(Pitch, Yaw, Roll, j, y1, i, ref x11, ref y11);

z21 = RotateObject(Pitch, Yaw, Roll, j, y2, i, ref x21, ref y21);

Rectangle MyBox1 = new Rectangle((int)x11, (int)y11, m, m);

Rectangle MyBox2 = new Rectangle((int)x21, (int)y21, m, m);

Pen MyPen1 = new Pen(Color.Blue, 1);

Pen MyPen2 = new Pen(Color.Red, 1);

GraphicObject.DrawEllipse(MyPen1, MyBox1);

GraphicObject.DrawEllipse(MyPen2, MyBox2);

}

}

for (i = x0 - R; i <= x0 + R; i += m)

{

int y1, y2;

y1 = (int)(y0 - Math.Sqrt(Math.Pow(R, 2) - Math.Pow((i - x0), 2)));

y2 = (int)(y0 + Math.Sqrt(Math.Pow(R, 2) - Math.Pow((i - x0), 2)));

double x11 = 0, y11 = 0, z11;

double x21 = 0, y21 = 0, z21;

z11 = RotateObject(Pitch, Yaw, Roll, i, y1, z0 + L, ref x11, ref y11);

z21 = RotateObject(Pitch, Yaw, Roll, i, y2, z0 + L, ref x21, ref y21);

Rectangle MyBox1 = new Rectangle((int)x11, (int)y11, m, m);

Rectangle MyBox2 = new Rectangle((int)x21, (int)y21, m, m);

Pen MyPen1 = new Pen(Color.Green, 1);

Pen MyPen2 = new Pen(Color.Orchid, 1);

GraphicObject.DrawLine(MyPen2, (float)x11, (float)y11, (float)x21, (float)y21);

GraphicObject.DrawEllipse(MyPen1, MyBox1);

GraphicObject.DrawEllipse(MyPen2, MyBox2);

}

Matrix myMatrix = new Matrix();

myMatrix.Translate(pictureBox1.Width / 2, pictureBox1.Height / 2, MatrixOrder.Append);

G.Transform = myMatrix;

pictureBox1.Image = bitmap;

}

private void hScrollBarPitch_Scroll(object sender, ScrollEventArgs e)

{

Pitch = hScrollBarPitch.Value;

DrawShape(G);

}

private void hScrollBarYaw_Scroll(object sender, ScrollEventArgs e)

{

Yaw = hScrollBarYaw.Value;

DrawShape(G);

}

private void hScrollBarRoll_Scroll(object sender, ScrollEventArgs e)

{

Roll = hScrollBarRoll.Value;

DrawShape(G);

}

private void Example14_Load(object sender, EventArgs e)

{

bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);

G = Graphics.FromImage(bitmap);

pictureBox1.Image = bitmap;

int minimum = 0, maximum = 369;

hScrollBarPitch.Minimum = minimum;

hScrollBarPitch.Maximum = maximum;

hScrollBarYaw.Minimum = minimum;

hScrollBarYaw.Maximum = maximum;

hScrollBarRoll.Minimum = minimum;

hScrollBarRoll.Maximum = maximum;

}

}

Пример 15. Сфера, как совокупность точек

public partial class Example15 : Form

{

public Example15()

{

InitializeComponent();

}

Graphics G;

Bitmap bitmap;

private double Factor = Math.PI / 180;

private double Pitch;

private double Yaw;

private double Roll;

public double RotateObject(double Pitch, double Yaw, double Roll, double x, double y, double z, ref double NewX, ref double NewY)

{

double[,] m = new double[4, 4];

m[1, 1] = Cos(Yaw) * Cos(Roll);

m[1, 2] = -Cos(Yaw) * Sin(Roll);

m[1, 3] = -Sin(Yaw);

m[2, 1] = Sin(Pitch) * Sin(Yaw) * Cos(Roll) + Sin(Roll)

* Cos(Pitch);

m[2, 2] = -Sin(Pitch) * Sin(Yaw) * Sin(Roll) + Cos(Roll)

* Cos(Pitch);

m[2, 3] = Cos(Yaw);

m[3, 1] = -Cos(Pitch) * Sin(Yaw) * Cos(Roll) + Sin(Pitch)

* Sin(Roll);

m[3, 2] = Cos(Pitch) * Sin(Yaw) * Sin(Roll) + Sin(Pitch)

* Cos(Roll);

m[3, 3] = Cos(Yaw) * Cos(Pitch);

double NewZ;

NewX = m[1, 1] * x + m[2, 1] * y + m[3, 1] * z;

NewY = m[1, 2] * x + m[2, 2] * y + m[3, 2] * z;

NewZ = m[1, 3] * x + m[2, 3] * y + m[3, 3] * z;

return NewZ;

}

public void DrawShape(Graphics GraphicObject)

{

int x0, y0, z0, R, m;

x0 = 5;

y0 = 5;

z0 = 5;

R = 50;

m = 3;

GraphicObject.Clear(Color.White);

int ZMin, Zmax;

ZMin = z0 - R;

Zmax = z0 + R;

int i, j;

Pen MyPen1 = new Pen(Color.Blue, 1);

Pen MyPen2 = new Pen(Color.Red, 1);

for (i = ZMin; i <= Zmax; i += m)

{

int XMin, XMax;

XMin = (int)(x0 - Math.Sqrt(Math.Pow(R, 2) - Math.Pow((i - z0), 2)));

XMax = (int)(x0 + Math.Sqrt(Math.Pow(R, 2) - Math.Pow((i - z0), 2)));

int SmallR;

SmallR = (int)(Math.Sqrt(Math.Pow(R, 2) - Math.Pow((i - z0), 2)));

for (j = XMin; j <= XMax; j += m)

{

double y1, y2;

y1 = y0 + Math.Sqrt(Math.Pow(SmallR, 2) - Math.Pow((j - x0), 2));

y2 = y0 - Math.Sqrt(Math.Pow(SmallR, 2) - Math.Pow((j - x0), 2));

double NewX1 = 0, NewY1 = 0, NewZ1;

double NewX2 = 0, NewY2 = 0, NewZ2;

NewZ1 = RotateObject(Pitch, Yaw, Roll, j, y1, i, ref NewX1, ref NewY1);

NewZ2 = RotateObject(Pitch, Yaw, Roll, j, y2, i, ref NewX2, ref NewY2);

Rectangle MyBox1 = new Rectangle((int)NewX1, (int)NewY1, m, m);

Rectangle MyBox2 = new Rectangle((int)NewX2, (int)NewY2, m, m);

GraphicObject.DrawEllipse(MyPen1, MyBox1);

GraphicObject.DrawEllipse(MyPen2, MyBox2);

}

}

Matrix myMatrix = new Matrix();

myMatrix.Translate(pictureBox1.Width / 2, pictureBox1.Height / 2, MatrixOrder.Append);

G.Transform = myMatrix;

pictureBox1.Image = bitmap;

}

private void hScrollBarPitch_Scroll(object sender, ScrollEventArgs e)

{

Pitch = Factor * hScrollBarPitch.Value;

DrawShape(G);

}

private void hScrollBarYaw_Scroll(object sender, ScrollEventArgs e)

{

Yaw = Factor * hScrollBarYaw.Value;

DrawShape(G);

}

private void hScrollBarRoll_Scroll(object sender, ScrollEventArgs e)

{

Roll = Factor * hScrollBarRoll.Value;

DrawShape(G);

}

private void Example15_Load(object sender, EventArgs e)

{

bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);

G = Graphics.FromImage(bitmap);

pictureBox1.Image = bitmap;

int minimum = 0, maximum = 369;

hScrollBarPitch.Minimum = minimum;

hScrollBarPitch.Maximum = maximum;

hScrollBarYaw.Minimum = minimum;

hScrollBarYaw.Maximum = maximum;

hScrollBarRoll.Minimum = minimum;

hScrollBarRoll.Maximum = maximum;

}

}

Пример 16. Конус, как совокупность точек

public Example16()

{

InitializeComponent();

}

Graphics G;

Bitmap bitmap;

double Factor = Math.PI / 180;

double Pitch;

double Yaw;

double Roll;

public double RotateObject(double Pitch, double Yaw, double Roll, double x, double y, double z, ref double NewX, ref double NewY)

{

double[,] m = new double[4, 4];

m[1, 1] = Cos(Yaw) * Cos(Roll);

m[1, 2] = -Cos(Yaw) * Sin(Roll);

m[1, 3] = -Sin(Yaw);

m[2, 1] = Sin(Pitch) * Sin(Yaw) * Cos(Roll) + Sin(Roll)

* Cos(Pitch);

m[2, 2] = -Sin(Pitch) * Sin(Yaw) * Sin(Roll) + Cos(Roll)

* Cos(Pitch);

m[2, 3] = Cos(Yaw);

m[3, 1] = -Cos(Pitch) * Sin(Yaw) * Cos(Roll) + Sin(Pitch)

* Sin(Roll);

m[3, 2] = Cos(Pitch) * Sin(Yaw) * Sin(Roll) + Sin(Pitch)

* Cos(Roll);

m[3, 3] = Cos(Yaw) * Cos(Pitch);

double NewZ;

NewX = m[1, 1] * x + m[2, 1] * y + m[3, 1] * z;

NewY = m[1, 2] * x + m[2, 2] * y + m[3, 2] * z;

NewZ = m[1, 3] * x + m[2, 3] * y + m[3, 3] * z;

return NewZ;

}

public void DrawShape(Graphics GraphicObject)

{

double x0, y0, z0, h, R, m;

x0 = 5;

y0 = 5;

z0 = 5;

h = 100;

R = 60;

m = 3;

GraphicObject.Clear(Color.White);

int i, j;

double ZMin, ZMax;

ZMin = z0;

ZMax = z0 + h;

double XMin, XMax;

double SmallR;

SmallR = (int)((R / (double)h) * (h + z0));

XMin = x0 - SmallR;

XMax = x0 + SmallR;

for (j = (int)XMin; j <= XMax; j += (int)m)

{

double YMin, YMax, x, z;

x = j;

z = z0;

YMin = (int)(y0 - Math.Sqrt(Math.Pow(SmallR, 2) - Math.Pow((x - x0), 2)));

YMax = (int)(y0 + Math.Sqrt(Math.Pow(SmallR, 2) - Math.Pow((x - x0), 2)));

double NewX1 = 0, NewY1 = 0, NewZ1, NewX2 = 0, NewY2 = 0, NewZ2;

NewZ1 = RotateObject(Pitch, Yaw, Roll, x, YMin, z, ref NewX1, ref NewY1);

NewZ2 = RotateObject(Pitch, Yaw, Roll, x, YMax, z, ref NewX2, ref NewY2);

Pen MyPen1 = new Pen(Color.Yellow, 1);

Pen MyPen2 = new Pen(Color.Blue, 1);

Rectangle MyBox1 = new Rectangle(System.Convert.ToInt32(NewX1), System.Convert.ToInt32(NewY1), 1, 1);

Rectangle MyBox2 = new Rectangle(System.Convert.ToInt32(NewX2), System.Convert.ToInt32(NewY2), 1, 1);

GraphicObject.DrawLine(MyPen1, (float)NewX1, (float)NewY1, (float)NewX2, (float)NewY2);

}

for (i = (int)ZMin; i <= ZMax; i += (int)m)

{

SmallR = (int)((R / (double)h) * (h + z0 - i));

XMin = x0 - SmallR;

XMax = x0 + SmallR;

for (j = (int)XMin; j <= XMax; j += (int)m)

{

int YMin, YMax, x, z;

x = j;

z = i;

YMin = (int)(y0 - Math.Sqrt(Math.Pow(SmallR, 2) - Math.Pow((x - x0), 2)));

YMax = (int)(y0 + Math.Sqrt(Math.Pow(SmallR, 2) - Math.Pow((x - x0), 2)));

double NewX1 = 0, NewY1 = 0, NewZ1, NewX2 = 0, NewY2 = 0, NewZ2;

NewZ1 = RotateObject(Pitch, Yaw, Roll, x, YMin, z, ref NewX1, ref NewY1);

NewZ2 = RotateObject(Pitch, Yaw, Roll, x, YMax, z, ref NewX2, ref NewY2);

Pen MyPen1 = new Pen(Color.Red, 1);

Pen MyPen2 = new Pen(Color.Blue, 1);

Rectangle MyBox1 = new Rectangle(System.Convert.ToInt32(NewX1), System.Convert.ToInt32(NewY1), 1, 1);

Rectangle MyBox2 = new Rectangle(System.Convert.ToInt32(NewX2), System.Convert.ToInt32(NewY2), 1, 1);

GraphicObject.DrawEllipse(MyPen1, MyBox1);

GraphicObject.DrawEllipse(MyPen2, MyBox2);

}

}

Matrix myMatrix = new Matrix();

myMatrix.Translate(pictureBox1.Width / 2, pictureBox1.Height / 2, MatrixOrder.Append);

G.Transform = myMatrix;

pictureBox1.Image = bitmap;

}

private void hScrollBarPitch_Scroll(object sender, ScrollEventArgs e)

{

Pitch = hScrollBarPitch.Value;

DrawShape(G);

}

private void hScrollBarYaw_Scroll(object sender, ScrollEventArgs e)

{

Yaw = hScrollBarYaw.Value;

DrawShape(G);

}

private void hScrollBarRoll_Scroll(object sender, ScrollEventArgs e)

{

Roll = hScrollBarRoll.Value;

DrawShape(G);

}

private void Example16_Load(object sender, EventArgs e)

{

bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);

G = Graphics.FromImage(bitmap);

pictureBox1.Image = bitmap;

int minimum = 0, maximum = 369;

hScrollBarPitch.Minimum = minimum;

hScrollBarPitch.Maximum = maximum;

hScrollBarYaw.Minimum = minimum;

hScrollBarYaw.Maximum = maximum;

hScrollBarRoll.Minimum = minimum;

hScrollBarRoll.Maximum = maximum;

}

}

Пример 17. Комбинация двух конусов

public Example17()

{

InitializeComponent();

}

Graphics G;

Bitmap bitmap;

double Factor = Math.PI / 180;

double Pitch;

double Yaw;

double Roll;

public double RotateObject(double Pitch, double Yaw, double Roll, double x, double y, double z, ref double NewX, ref double NewY)

{

double[,] m = new double[4, 4];

m[1, 1] = Cos(Yaw) * Cos(Roll);

m[1, 2] = -Cos(Yaw) * Sin(Roll);

m[1, 3] = -Sin(Yaw);

m[2, 1] = Sin(Pitch) * Sin(Yaw) * Cos(Roll) + Sin(Roll)

* Cos(Pitch);

m[2, 2] = -Sin(Pitch) * Sin(Yaw) * Sin(Roll) + Cos(Roll)

* Cos(Pitch);

m[2, 3] = Cos(Yaw);

m[3, 1] = -Cos(Pitch) * Sin(Yaw) * Cos(Roll) + Sin(Pitch)

* Sin(Roll);

m[3, 2] = Cos(Pitch) * Sin(Yaw) * Sin(Roll) + Sin(Pitch)

* Cos(Roll);

m[3, 3] = Cos(Yaw) * Cos(Pitch);

double NewZ;

NewX = m[1, 1] * x + m[2, 1] * y + m[3, 1] * z;

NewY = m[1, 2] * x + m[2, 2] * y + m[3, 2] * z;

NewZ = m[1, 3] * x + m[2, 3] * y + m[3, 3] * z;

return NewZ;

}

public class Vertex_Coordinate

{

public int x;

public int y;

public int z;

public int count;

public Vertex_Coordinate(int x, int y, int z) : base()

{

this.x = x;

this.y = y;

this.z = z;

}

}

ArrayList MyList = new ArrayList();

ArrayList NewList = new ArrayList();

int a, b, c, H, d, x0, y0, z0, R, m, L;

public void Fill_Cone()

{

int i, j;

int ZMin, ZMax;

ZMin = z0;

ZMax = z0 + H;

for (i = ZMin; i <= ZMax; i += m)

{

int XMin, XMax;

int SmallR;

SmallR = (int)((R / (double)H) * (H + z0 - i));

XMin = x0 - SmallR;

XMax = x0 + SmallR;

for (j = XMin; j <= XMax; j += m)

{

int YMin, YMax, x, z;

x = j;

z = i;

YMin = (int)(y0 - Math.Sqrt(Math.Pow(SmallR, 2) - Math.Pow((x - x0), 2)));

YMax = (int)(y0 + Math.Sqrt(Math.Pow(SmallR, 2) - Math.Pow((x - x0), 2)));

MyList.Add(new Vertex_Coordinate(j, YMin, i));

MyList.Add(new Vertex_Coordinate(j, YMax, i));

}

}

}

public int RotateAboutAlphaBeta(double alpha, double beta, int x0, int y0, int z0, ref int x, ref int y)

{

int z;

double factor;

factor = Math.PI / 180;

x = (int)(Cos(alpha) * x0 + Sin(alpha) * Sin(-beta) * y0 + Sin(alpha) * Cos(-beta) * z0);

y = (int)(Cos(-beta) * y0 - Sin(-beta) * z0);

z = (int)(-Sin(alpha) * x0 + Cos(alpha) * Sin(-beta) * y0 + Cos(alpha) * Cos(-beta) * z0);

return z;

}

public void DrawShape(Graphics GraphicObject)

{

double x1, y1, z1, x2, y2, z2, newx0 = 0, newy0 = 0, newz0, newx1 = 0, newy1 = 0, newz1, newx2 = 0, newy2 = 0, newz2;

x1 = x0;

y1 = y0;

z1 = z0 + H;

double factor;

factor = Math.PI / 180;

double alpha, beta;

alpha = Math.PI / 4;

beta = Math.PI / 4;

x2 = (int)(H * Sin(alpha) * Cos(beta));

y2 = (int)(H * Sin(beta));

z2 = (int)(H * Cos(alpha) * Cos(beta));

newz0 = RotateObject(Pitch, Yaw, Roll, x0, y0, z0, ref newx0, ref newy0);

newz1 = RotateObject(Pitch, Yaw, Roll, x1, y1, z1, ref newx1, ref newy1);

newz2 = RotateObject(Pitch, Yaw, Roll, x2, y2, z2, ref newx2, ref newy2);

Pen MyPen1 = new Pen(Color.Blue, 1);

Pen MyPen2 = new Pen(Color.Red, 1);

Point Point0 = new Point((int)newx0, (int)newy0);

Point Point1 = new Point((int)newx1, (int)newy1);

Point Point2 = new Point((int)newx2, (int)newy2);

G.Clear(Color.White);

G.DrawLine(MyPen1, Point0, Point1);

G.DrawLine(MyPen2, Point0, Point2);

int ListCount;

ListCount = MyList.Count - 1;

int i;

Pen MyPen = new Pen(Color.Green, 1);

Pen MyPen3 = new Pen(Color.Brown, 1);

for (i = 0; i <= ListCount; i++)

{

int x, y, z;

double newx = 0, newy = 0, newz;

x = ((Vertex_Coordinate)MyList[i]).x;

y = ((Vertex_Coordinate)MyList[i]).y;

z = ((Vertex_Coordinate)MyList[i]).z;

int myx = 0, myy = 0, myz;

double newmyx = 0, newmyy = 0, newmyz;

myz = RotateAboutAlphaBeta(alpha, beta, x, y, z, ref myx, ref myy);

newmyz = RotateObject(Pitch, Yaw, Roll, myx, myy, myz, ref newmyx, ref newmyy);

newz = RotateObject(Pitch, Yaw, Roll, x, y, z, ref newx, ref newy);

Rectangle MyBox1 = new Rectangle((int)newx, (int)newy, 1, 1);

Rectangle MyBox2 = new Rectangle((int)newmyx, (int)newmyy, 1, 1);

G.DrawEllipse(MyPen, MyBox1);

G.DrawEllipse(MyPen3, MyBox2);

}

int xstart, ystart;

xstart = pictureBox1.Width / (int)2;

ystart = pictureBox1.Height / (int)2;

Matrix MyMatrix = new Matrix();

MyMatrix.Translate(xstart, ystart);

G.Transform = MyMatrix;

pictureBox1.Image = bitmap;

}

private void hScrollBarPitch_Scroll(object sender, ScrollEventArgs e)

{

Pitch = hScrollBarPitch.Value;

DrawShape(G);

}

private void hScrollBarYaw_Scroll(object sender, ScrollEventArgs e)

{

Yaw = hScrollBarYaw.Value;

DrawShape(G);

}

private void hScrollBarRoll_Scroll(object sender, ScrollEventArgs e)

{

Roll = hScrollBarRoll.Value;

DrawShape(G);

}

private void Example17_Load(object sender, EventArgs e)

{

bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);

G = Graphics.FromImage(bitmap);

pictureBox1.Image = bitmap;

int Minimum, Maximum;

Minimum = 0;

Maximum = 369;

hScrollBarPitch.Minimum = Minimum;

hScrollBarPitch.Maximum = Maximum;

hScrollBarYaw.Minimum = Minimum;

hScrollBarYaw.Maximum = Maximum;

hScrollBarRoll.Minimum = Minimum;

hScrollBarRoll.Maximum = Maximum;

x0 = 0;

y0 = 0;

z0 = 0;

R = 100;

m = 3;

L = 200;

a = 50;

b = 100;

c = 150;

H = 300;

d = 150;

Fill_Cone();

}

Пример 18. Объединение трех усеченных конусов

public Example18()

{

InitializeComponent();

}

Graphics G;

Bitmap bitmap;

double Factor = Math.PI / 180;

double Pitch;

double Yaw;

double Roll;

public double RotateObject(double Pitch, double Yaw, double Roll, double x, double y, double z, ref double NewX, ref double NewY)

{

double[,] m = new double[4, 4];

m[1, 1] = Cos(Yaw) * Cos(Roll);

m[1, 2] = -Cos(Yaw) * Sin(Roll);

m[1, 3] = -Sin(Yaw);

m[2, 1] = Sin(Pitch) * Sin(Yaw) * Cos(Roll) + Sin(Roll)

* Cos(Pitch);

m[2, 2] = -Sin(Pitch) * Sin(Yaw) * Sin(Roll) + Cos(Roll)

* Cos(Pitch);

m[2, 3] = Cos(Yaw);

m[3, 1] = -Cos(Pitch) * Sin(Yaw) * Cos(Roll) + Sin(Pitch)

* Sin(Roll);

m[3, 2] = Cos(Pitch) * Sin(Yaw) * Sin(Roll) + Sin(Pitch)

* Cos(Roll);

m[3, 3] = Cos(Yaw) * Cos(Pitch);

double NewZ;

NewX = m[1, 1] * x + m[2, 1] * y + m[3, 1] * z;

NewY = m[1, 2] * x + m[2, 2] * y + m[3, 2] * z;

NewZ = m[1, 3] * x + m[2, 3] * y + m[3, 3] * z;

return NewZ;

}

public class Vertex_Coordinate

{

public int x;

public int y;

public int z;

public Vertex_Coordinate(int x, int y, int z) : base()

{

this.x = x;

this.y = y;

this.z = z;

}

}

private ArrayList GList = new ArrayList();

public int RotateAboutAlphaBeta(double alpha, double beta, int x0, int y0, int z0, ref int x, ref int y)

{

int z;

double factor;

factor = Math.PI / 180;

x = (int)(Cos(alpha) * x0 + Sin(alpha) * Sin(-beta) * y0 + Sin(alpha) * Cos(-beta) * z0);

y = (int)(Cos(-beta) * y0 - Sin(-beta) * z0);

z = (int)(-Sin(alpha) * x0 + Cos(alpha) * Sin(-beta) * y0 + Cos(alpha) * Cos(-beta) * z0);

return z;

}

public ArrayList GetClippedCone(int Length, int RBig, int RSmall, int m)

{

ArrayList MyList = new ArrayList();

double i, j;

int ZMin, ZMax;

ZMin = 0;

ZMax = Length;

for (i = ZMin; i <= ZMax; i += m)

{

double XMin, XMax;

double SmallR;

SmallR = (RBig - i * (RBig - RSmall) / Length);

XMin = -SmallR;

XMax = SmallR;

for (j = XMin; j <= XMax; j += m)

{

double YMin, YMax, x, z;

x = j;

z = i;

YMin = (-Math.Sqrt(Math.Pow(SmallR, 2) - Math.Pow((x), 2)));

YMax = (Math.Sqrt(Math.Pow(SmallR, 2) - Math.Pow((x), 2)));

MyList.Add(new Vertex_Coordinate((int)j, (int)YMin, (int)i));

MyList.Add(new Vertex_Coordinate((int)j, (int)YMax, (int)i));

}

}

return MyList;

}

public void FillClippedCone()

{

int Segments_Count;

Segments_Count = 3;

int[] x = new int[Segments_Count + 1], y = new int[Segments_Count + 1], z = new int[Segments_Count + 1];

double[] alpha = new double[Segments_Count + 1], beta = new double[Segments_Count + 1], Length = new double[Segments_Count + 1];

int[] RBig = new int[Segments_Count + 1], RSmall = new int[Segments_Count + 1];

int m;

m = 3;

RBig[0] = 60;

RSmall[0] = 50;

RBig[1] = 50;

RSmall[1] = 40;

RBig[2] = 40;

RSmall[2] = 30;

double factor;

factor = Math.PI / 180;

alpha[0] = factor * 15;

beta[0] = factor * 25;

alpha[1] = factor * 30;

beta[1] = factor * 45;

alpha[2] = factor * 15;

beta[2] = factor * 30;

Length[0] = 200;

Length[1] = 150;

Length[2] = 100;

x[0] = 0;

y[0] = 0;

z[0] = 0;

int i, j;

for (i = 1; i <= Segments_Count; i++)

{

/*x[i] = x[i - 1] + System.Convert.ToInt32(Length[i - 1] * Cos(beta[i - 1]) * Sin(alpha[i - 1]));

y[i] = y[i - 1] + System.Convert.ToInt32(Length[i - 1] * Sin(beta[i - 1]));

z[i] = z[i - 1] + System.Convert.ToInt32(Length[i - 1] * Cos(beta[i - 1]) * Cos(alpha[i - 1]));*/

x[i] = x[i - 1] + (int)(Length[i - 1] * Math.Cos(beta[i - 1]) * Math.Sin(alpha[i - 1]));

y[i] = y[i - 1] + (int)(Length[i - 1] * Math.Sin(beta[i - 1]));

z[i] = z[i - 1] + (int)(Length[i - 1] * Math.Cos(beta[i - 1]) * Math.Cos(alpha[i - 1]));

ArrayList MyList = new ArrayList();

MyList = GetClippedCone((int)Length[i - 1], RBig[i - 1], RSmall[i - 1], m);

int MyListCount;

MyListCount = MyList.Count - 1;

for (j = 0; j <= MyListCount; j++)

{

int x1, y1, z1, newx = 0, newy = 0, newz;

x1 = ((Vertex_Coordinate)MyList[j]).x;

y1 = ((Vertex_Coordinate)MyList[j]).y;

z1 = ((Vertex_Coordinate)MyList[j]).z;

newz = RotateAboutAlphaBeta(alpha[i - 1], beta[i - 1], x1, y1, z1, ref newx, ref newy);

int nx, ny, nz;

nx = newx + x[i - 1];

ny = newy + y[i - 1];

nz = newz + z[i - 1];

GList.Add(new Vertex_Coordinate(nx, ny, nz));

}

}

}

public void DrawShape(Graphics GraphicObject)

{

G.Clear(Color.White);

int GListCount;

GListCount = GList.Count - 1;

int i;

Pen MyPen = new Pen(Color.Red, 1);

for (i = 0; i <= GListCount; i++)

{

int x, y, z;

double newx = 0, newy = 0, newz;

x = ((Vertex_Coordinate)GList[i]).x;

y = ((Vertex_Coordinate)GList[i]).y;

z = ((Vertex_Coordinate)GList[i]).z;

newz = RotateObject(Pitch, Yaw, Roll, x, y, z, ref newx, ref newy);

Rectangle MyBox = new Rectangle((int)newx, (int)newy, 1, 1);

G.DrawEllipse(MyPen, MyBox);

}

int xstart, ystart;

xstart = pictureBox1.Width / 2;

ystart = pictureBox1.Height / 2;

Matrix MyMatrix = new Matrix();

MyMatrix.Translate(xstart, ystart);

G.Transform = MyMatrix;

pictureBox1.Image = bitmap;

}

private void hScrollBarPitch_Scroll(object sender, ScrollEventArgs e)

{

Pitch = Factor * hScrollBarPitch.Value;

DrawShape(G);

}

private void hScrollBarYaw_Scroll(object sender, ScrollEventArgs e)

{

Yaw = Factor * hScrollBarYaw.Value;

DrawShape(G);

}

private void hScrollBarRoll_Scroll(object sender, ScrollEventArgs e)

{

Roll = Factor * hScrollBarRoll.Value;

DrawShape(G);

}

private void Example18_Load(object sender, EventArgs e)

{

bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);

G = Graphics.FromImage(bitmap);

pictureBox1.Image = bitmap;

int minimum = 0, maximum = 369;

hScrollBarPitch.Minimum = minimum;

hScrollBarPitch.Maximum = maximum;

hScrollBarYaw.Minimum = minimum;

hScrollBarYaw.Maximum = maximum;

hScrollBarRoll.Minimum = minimum;

hScrollBarRoll.Maximum = maximum;

FillClippedCone();

}

}

Доп. Задание 1. Сфера, вписанная в куб

public Task1()

{

InitializeComponent();

}

Graphics G;

Bitmap bitmap;

double Factor = Math.PI / 180;

double Pitch;

double Yaw;

double Roll;

public double RotateObject(double Pitch, double Yaw, double Roll, double x, double y, double z, ref double NewX, ref double NewY)

{

double[,] m = new double[4, 4];

m[1, 1] = Math.Cos(Yaw) * Math.Cos(Roll);

m[1, 2] = -Math.Cos(Yaw) * Math.Sin(Roll);

m[1, 3] = -Math.Sin(Yaw);

m[2, 1] = Math.Sin(Pitch) * Math.Sin(Yaw) * Math.Cos(Roll) + Math.Sin(Roll) * Math.Cos(Pitch);

m[2, 2] = -Math.Sin(Pitch) * Math.Sin(Yaw) * Math.Sin(Roll) + Math.Cos(Roll) * Math.Cos(Pitch);

m[2, 3] = Math.Cos(Yaw);

m[3, 1] = -Math.Cos(Pitch) * Math.Sin(Yaw) * Math.Cos(Roll) + Math.Sin(Pitch) * Math.Sin(Roll);

m[3, 2] = Math.Cos(Pitch) * Math.Sin(Yaw) * Math.Sin(Roll) + Math.Sin(Pitch) * Math.Cos(Roll);

m[3, 3] = Math.Cos(Yaw) * Math.Cos(Pitch);

double NewZ;

NewX = m[1, 1] * x + m[2, 1] * y + m[3, 1] * z;

NewY = m[1, 2] * x + m[2, 2] * y + m[3, 2] * z;

NewZ = m[1, 3] * x + m[2, 3] * y + m[3, 3] * z;

return NewZ;

}

public void DrawShape()

{

G.Clear(Color.White);

double x0 = 0, y0 = 0, z0 = 0, R = 60;

double ZMin = z0 - R, Zmax = z0 + R;

Pen pen1 = new Pen(Color.Blue, 1);

Pen pen2 = new Pen(Color.Red, 1);

//Изображение сферы

for (double i = ZMin; i < Zmax; ++i)

{

double XMin = x0 - Math.Sqrt(Math.Pow(R, 2) - Math.Pow((i - z0), 2));

double XMax = x0 + Math.Sqrt(Math.Pow(R, 2) - Math.Pow((i - z0), 2));

double SmallR = Math.Sqrt(Math.Pow(R, 2) - Math.Pow((i - z0), 2));

for (double j = XMin; j < XMax; ++j)

{

double y1 = y0 + Math.Sqrt(Math.Pow(SmallR, 2) - Math.Pow((j - x0), 2));

double y2 = y0 - Math.Sqrt(Math.Pow(SmallR, 2) - Math.Pow((j - x0), 2));

double newX1 = 0, newY1 = 0;

double newX2 = 0, newY2 = 0;

RotateObject(Pitch, Yaw, Roll, j, y1, i, ref newX1, ref newY1);

RotateObject(Pitch, Yaw, Roll, j, y2, i, ref newX2, ref newY2);

Rectangle box1 = new Rectangle((int)newX1, (int)newY1, 1, 1);

G.DrawEllipse(pen1, box1);

Rectangle box2 = new Rectangle((int)newX2, (int)newY2, 1, 1);

G.DrawEllipse(pen2, box2);

}

}

//Подбор цвето для куба

double side = 2 * R;

Pen[] pens = { new Pen(Color.Blue, 1), new Pen(Color.Red, 1), new Pen(Color.Green, 1),

new Pen(Color.Black, 1), new Pen(Color.Yellow, 1), new Pen(Color.Orchid, 1)};

//Верхняя грань куба

x0 -= side / 2; y0 -= side / 2; z0 -= side / 2;

for (double i = x0; i < x0 + side; i += 3)

{

for (double j = y0; j < y0 + side; j += 3)

{

double newx1 = 0, newy1 = 0;

RotateObject(Pitch, Yaw, Roll, i, j, z0, ref newx1, ref newy1);

Rectangle box = new Rectangle((int)newx1, (int)newy1, 1, 1);

G.DrawEllipse(pens[4], box);

}

}

//Боковые грани куба

for (double i = z0 + 1; i < z0 + side - 1; i += 3)

{

//Две противоложные грани

for (double j = y0; j < y0 + side; j += 3)

{

double newx1 = 0, newy1 = 0;

double newx2 = 0, newy2 = 0;

RotateObject(Pitch, Yaw, Roll, x0, j, i, ref newx1, ref newy1);

RotateObject(Pitch, Yaw, Roll, x0 + side, j, i, ref newx2, ref newy2);

Rectangle box1 = new Rectangle((int)newx1, (int)newy1, 1, 1);

G.DrawEllipse(pens[0], box1);

Rectangle box2 = new Rectangle((int)newx2, (int)newy2, 1, 1);

G.DrawEllipse(pens[1], box2);

}

//Две противоложные грани

for (double k = x0; k < x0 + side; k += 3)

{

double newx1 = 0, newy1 = 0;

double newx2 = 0, newy2 = 0;

RotateObject(Pitch, Yaw, Roll, k, y0, i, ref newx1, ref newy1);

RotateObject(Pitch, Yaw, Roll, k, y0 + side, i, ref newx2, ref newy2);

Rectangle box1 = new Rectangle((int)newx1, (int)newy1, 1, 1);

G.DrawEllipse(pens[2], box1);

Rectangle box2 = new Rectangle((int)newx2, (int)newy2, 1, 1);

G.DrawEllipse(pens[3], box2);

}

}

//Нижняя грань куба

for (double i = x0; i < x0 + side; i += 3)

{

for (double j = y0; j < y0 + side; j += 3)

{

double newx1 = 0, newy1 = 0;

RotateObject(Pitch, Yaw, Roll, i, j, z0 + side, ref newx1, ref newy1);

Rectangle box = new Rectangle((int)newx1, (int)newy1, 1, 1);

G.DrawEllipse(pens[5], box);

}

}

Matrix matrix = new Matrix();

matrix.Translate(pictureBox1.Width / 2, pictureBox1.Height / 2, MatrixOrder.Append);

G.Transform = matrix;

pictureBox1.Image = bitmap;

}

private void hScrollBarPitch_Scroll(object sender, ScrollEventArgs e)

{

Pitch = Factor * hScrollBarPitch.Value;

DrawShape();

}

private void hScrollBarYaw_Scroll(object sender, ScrollEventArgs e)

{

Yaw = Factor * hScrollBarYaw.Value;

DrawShape();

}

private void hScrollBarRoll_Scroll(object sender, ScrollEventArgs e)

{

Roll = Factor * hScrollBarRoll.Value;

DrawShape();

}

private void Task1_Load(object sender, EventArgs e)

{

bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);

G = Graphics.FromImage(bitmap);

pictureBox1.Image = bitmap;

}

Доп. Задание 2. Тор

public Task2()

{

InitializeComponent();

}

Graphics G;

Bitmap bitmap;

double Factor = Math.PI / 180;

double Pitch;

double Yaw;

double Roll;

public double RotateObject(double Pitch, double Yaw, double Roll, double x, double y, double z, ref double NewX, ref double NewY)

{

double[,] m = new double[4, 4];

m[1, 1] = Math.Cos(Yaw) * Math.Cos(Roll);

m[1, 2] = -Math.Cos(Yaw) * Math.Sin(Roll);

m[1, 3] = -Math.Sin(Yaw);

m[2, 1] = Math.Sin(Pitch) * Math.Sin(Yaw) * Math.Cos(Roll) + Math.Sin(Roll) * Math.Cos(Pitch);

m[2, 2] = -Math.Sin(Pitch) * Math.Sin(Yaw) * Math.Sin(Roll) + Math.Cos(Roll) * Math.Cos(Pitch);

m[2, 3] = Math.Cos(Yaw);

m[3, 1] = -Math.Cos(Pitch) * Math.Sin(Yaw) * Math.Cos(Roll) + Math.Sin(Pitch) * Math.Sin(Roll);

m[3, 2] = Math.Cos(Pitch) * Math.Sin(Yaw) * Math.Sin(Roll) + Math.Sin(Pitch) * Math.Cos(Roll);

m[3, 3] = Math.Cos(Yaw) * Math.Cos(Pitch);

double NewZ;

NewX = m[1, 1] * x + m[2, 1] * y + m[3, 1] * z;

NewY = m[1, 2] * x + m[2, 2] * y + m[3, 2] * z;

NewZ = m[1, 3] * x + m[2, 3] * y + m[3, 3] * z;

return NewZ;

}

public void DrawShape()

{

G.Clear(Color.White);

int R = 120, r = 60;

double X, Y, Newx = 100, Newy = 100, Z;

//Изображение тора

for (int i = 0; i < 360; i += 3)

{

for (int j = -180; j < 180; j += 3)

{

X = (R + r * Math.Cos(j)) * Math.Cos(i);

Y = r * Math.Sin(j);

Z = (R + r * Math.Cos(j)) * Math.Sin(i);

Z = RotateObject(Pitch, Yaw, Roll, X, Y, Z, ref Newx, ref Newy);

G.DrawEllipse(Pens.Green, (float)Newx + 400, (float)Newy + 300, 1, 1);

}

}

pictureBox1.Image = bitmap;

}

private void hScrollBarPitch_Scroll(object sender, ScrollEventArgs e)

{

Pitch = Factor * hScrollBarPitch.Value;

DrawShape();

}

private void hScrollBarYaw_Scroll(object sender, ScrollEventArgs e)

{

Yaw = Factor * hScrollBarYaw.Value;

DrawShape();

}

private void hScrollBarRoll_Scroll(object sender, ScrollEventArgs e)

{

Roll = Factor * hScrollBarRoll.Value;

DrawShape();

}

private void Task2_Load(object sender, EventArgs e)

{

bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);

G = Graphics.FromImage(bitmap);

pictureBox1.Image = bitmap;

}

Доп. Задание 3. Эллипсоид вращения

public Task3()

{

InitializeComponent();

}

Graphics G;

Bitmap bitmap;

double Factor = Math.PI / 180;

double Pitch;

double Yaw;

double Roll;

public double RotateObject(double Pitch, double Yaw, double Roll, double x, double y, double z, ref double NewX, ref double NewY)

{

double[,] m = new double[4, 4];

m[1, 1] = Math.Cos(Yaw) * Math.Cos(Roll);

m[1, 2] = -Math.Cos(Yaw) * Math.Sin(Roll);

m[1, 3] = -Math.Sin(Yaw);

m[2, 1] = Math.Sin(Pitch) * Math.Sin(Yaw) * Math.Cos(Roll) + Math.Sin(Roll) * Math.Cos(Pitch);

m[2, 2] = -Math.Sin(Pitch) * Math.Sin(Yaw) * Math.Sin(Roll) + Math.Cos(Roll) * Math.Cos(Pitch);

m[2, 3] = Math.Cos(Yaw);

m[3, 1] = -Math.Cos(Pitch) * Math.Sin(Yaw) * Math.Cos(Roll) + Math.Sin(Pitch) * Math.Sin(Roll);

m[3, 2] = Math.Cos(Pitch) * Math.Sin(Yaw) * Math.Sin(Roll) + Math.Sin(Pitch) * Math.Cos(Roll);

m[3, 3] = Math.Cos(Yaw) * Math.Cos(Pitch);

double NewZ;

NewX = m[1, 1] * x + m[2, 1] * y + m[3, 1] * z;

NewY = m[1, 2] * x + m[2, 2] * y + m[3, 2] * z;

NewZ = m[1, 3] * x + m[2, 3] * y + m[3, 3] * z;

return NewZ;

}

public void DrawShape()

{

G.Clear(Color.White);

double x0, y0, z0, a, b, c;

x0 = 0; y0 = 0; z0 = 0;

//Коэффиценты

a = 100; b = 120; c = 200;

double ZMin = z0 -c, Zmax = z0 + c;

Pen pen1 = new Pen(Color.Red, 1);

Pen pen2 = new Pen(Color.Green, 1);

//Изображение эллипсоида

for (double i = ZMin; i < Zmax; i += 3)

{

double XMin = x0 - Math.Sqrt(1 - Math.Pow((i - z0) / c, 2)) * a;

double XMax = x0 + Math.Sqrt(1 - Math.Pow((i - z0) / c, 2)) * a;

double SmallR = Math.Sqrt(1 - Math.Pow((i - z0) / c, 2));

for (double j = XMin; j < XMax; ++j)

{

double y1 = y0 + Math.Sqrt(Math.Pow(SmallR, 2) - Math.Pow((j - x0) / a, 2)) * b;

double y2 = y0 - Math.Sqrt(Math.Pow(SmallR, 2) - Math.Pow((j - x0) / a, 2)) * b;

double newX1 = 0, newY1 = 0;

double newX2 = 0, newY2 = 0;

RotateObject(Pitch, Yaw, Roll, j, y1, i, ref newX1, ref newY1);

RotateObject(Pitch, Yaw, Roll, j, y2, i, ref newX2, ref newY2);

RectangleF box1 = new RectangleF((float)newX1, (float)newY1, 1, 1);

G.DrawEllipse(pen1, box1);

RectangleF box2 = new RectangleF((float)newX2, (float)newY2, 1, 1);

G.DrawEllipse(pen2, box2);

}

}

Matrix matrix = new Matrix();

matrix.Translate(pictureBox1.Width / 2, pictureBox1.Height / 2, MatrixOrder.Append);

G.Transform = matrix;

pictureBox1.Image = bitmap;

}

private void hScrollBarPitch_Scroll(object sender, ScrollEventArgs e)

{

Pitch = Factor * hScrollBarPitch.Value;

DrawShape();

}

private void hScrollBarYaw_Scroll(object sender, ScrollEventArgs e)

{

Yaw = Factor * hScrollBarYaw.Value;

DrawShape();

}

private void hScrollBarRoll_Scroll(object sender, ScrollEventArgs e)

{

Roll = Factor * hScrollBarRoll.Value;

DrawShape();

}

private void Task3_Load(object sender, EventArgs e)

{

bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);

G = Graphics.FromImage(bitmap);

pictureBox1.Image = bitmap;

}

Доп. Задание 4. Шесть сфер

public Task4()

{

InitializeComponent();

}

Graphics G;

Bitmap bitmap;

double Factor = Math.PI / 180;

ArrayList list = new ArrayList();

double Pitch;

double Yaw;

double Roll;

public double RotateObject(double Pitch, double Yaw, double Roll, double x, double y, double z, ref double NewX, ref double NewY)

{

double[,] m = new double[4, 4];

m[1, 1] = Math.Cos(Yaw) * Math.Cos(Roll);

m[1, 2] = -Math.Cos(Yaw) * Math.Sin(Roll);

m[1, 3] = -Math.Sin(Yaw);

m[2, 1] = Math.Sin(Pitch) * Math.Sin(Yaw) * Math.Cos(Roll) + Math.Sin(Roll) * Math.Cos(Pitch);

m[2, 2] = -Math.Sin(Pitch) * Math.Sin(Yaw) * Math.Sin(Roll) + Math.Cos(Roll) * Math.Cos(Pitch);

m[2, 3] = Math.Cos(Yaw);

m[3, 1] = -Math.Cos(Pitch) * Math.Sin(Yaw) * Math.Cos(Roll) + Math.Sin(Pitch) * Math.Sin(Roll);

m[3, 2] = Math.Cos(Pitch) * Math.Sin(Yaw) * Math.Sin(Roll) + Math.Sin(Pitch) * Math.Cos(Roll);

m[3, 3] = Math.Cos(Yaw) * Math.Cos(Pitch);

double NewZ;

NewX = m[1, 1] * x + m[2, 1] * y + m[3, 1] * z;

NewY = m[1, 2] * x + m[2, 2] * y + m[3, 2] * z;

NewZ = m[1, 3] * x + m[2, 3] * y + m[3, 3] * z;

return NewZ;

}

public void DrawShape()

{

G.Clear(Color.White);

//Извлечение данных и прорисовка

for (int i = 0; i < list.Count; i++)

{

Pen pen = new Pen(Color.Brown);

List<Vertex> tempList = (List<Vertex>)list[i];

for (int j = 0; j < tempList.Count; j += 3)

{

double newx = 0, newy = 0;

RotateObject(Pitch, Yaw, Roll, tempList[j].X, tempList[j].Y, tempList[j].Z, ref newx, ref newy);

RectangleF box = new RectangleF((float)newx, (float)newy, 1, 1);

G.DrawEllipse(pen, box);

}

}

Matrix matrix = new Matrix();

matrix.Translate(pictureBox1.Width / 2, pictureBox1.Height / 2, MatrixOrder.Append);

G.Transform = matrix;

pictureBox1.Image = bitmap;

}

//Получение данных для сфер

List<Vertex> GetSphere(double R, Vertex v)

{

List<Vertex> tempList = new List<Vertex>();

double x0 = v.X, y0 = v.Y, z0 = v.Z;

double ZMin = z0 - R;

double Zmax = z0 + R;

for (double i = ZMin; i < Zmax; ++i)

{

double XMin = x0 - Math.Sqrt(Math.Pow(R, 2) - Math.Pow((i - z0), 2));

double XMax = x0 + Math.Sqrt(Math.Pow(R, 2) - Math.Pow((i - z0), 2));

double SmallR = Math.Sqrt(Math.Pow(R, 2) - Math.Pow((i - z0), 2));

for (double j = XMin; j < XMax; ++j)

{

double y1 = y0 + Math.Sqrt(Math.Pow(SmallR, 2) - Math.Pow((j - x0), 2));

double y2 = y0 - Math.Sqrt(Math.Pow(SmallR, 2) - Math.Pow((j - x0), 2));

tempList.Add(new Vertex(j, y1, i));

tempList.Add(new Vertex(j, y2, i));

}

}

return tempList;

}

private void hScrollBarPitch_Scroll(object sender, ScrollEventArgs e)

{

Pitch = Factor * hScrollBarPitch.Value;

DrawShape();

}

private void hScrollBarYaw_Scroll(object sender, ScrollEventArgs e)

{

Yaw = Factor * hScrollBarYaw.Value;

DrawShape();

}

private void hScrollBarRoll_Scroll(object sender, ScrollEventArgs e)

{

Roll = Factor * hScrollBarRoll.Value;

DrawShape();

}

private void Task4_Load(object sender, EventArgs e)

{

bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);

G = Graphics.FromImage(bitmap);

pictureBox1.Image = bitmap;

int count = 6;

Vertex[] vert = new Vertex[count];

vert[0] = new Vertex(0, 0, 0);

double[] rad = { 80, 70, 50, 40, 30, 20 };

//Создание центров будущих сфер

for (int i = 1; i < count; ++i)

{

double nx = vert[i - 1].X + rad[i - 1] + rad[i];

double ny = vert[i - 1].Y;

double nz = vert[i - 1].Z;

vert[i] = new Vertex(nx, ny, nz);

}

//Подготовка для отрисовки

for (int i = 0; i < count; ++i)

{

list.Add(GetSphere(rad[i], vert[i]));

}

}

Соседние файлы в предмете Инженерная и компьютерная графика