using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace cube
{
class Vector
{
public double x,y,z,h;
//конструктор, по умолчанию значения координат нулевые, а h = 1
public Vector(double x = 0, double y = 0, double z = 0, double h = 1)
{
this.x = x;
this.y = y;
this.z = z;
this.h = h;
}
//функции для возврата к обычным координатам от гомогенных при отрисовке
public double GetX()
{
return this.x / h;
}
public double GetY()
{
return this.y / h;
}
public double GetZ()
{
return this.z / h;
}
}
class RotateMatrix
{
//строка, столбец
public double[,] rotateMatrix = new double[4, 4];
//конструктор, вносим туда координаты и желаемый угол
public RotateMatrix(double x, double y, double z, double angle)
{
//нормируем векторы, для начала находим норму, затем делим координаты поочередно на норму
double Norm = Math.Sqrt(x * x + y * y + z * z);
x = x / Norm;
y = y / Norm;
z = z / Norm;
//Вычисляем заранее синусы и косинусы угла, Иван сказал так круче(!)
//Math.cos и Math.sin работают с радианами, поэтому переводим в них
double cos = Math.Cos(Math.PI*angle/180);
double sin = Math.Sin(Math.PI*angle/180);
//заносим матрицу поворота в rotateMartix
//первая строка
rotateMatrix[0,0] = cos + (1 - cos) * x * x;
rotateMatrix[0,1] = (1 - cos) * x * y - sin * z;
rotateMatrix[0,2] = (1 - cos) * x * z + sin * y;
rotateMatrix[0,3] = 0;
//вторая строка
rotateMatrix[1,0] = (1 - cos) * y * x + sin * z;
rotateMatrix[1,1] = cos + (1 - cos) * y * y;
rotateMatrix[1,2] = (1 - cos) * y * z - sin * x;
rotateMatrix[1,3] = 0;
//третья строка
rotateMatrix[2,0] = (1 - cos) * z * x - sin * y;
rotateMatrix[2,1] = (1 - cos) * z * y + sin * x;
rotateMatrix[2,2] = cos + (1 - cos) * z * z;
rotateMatrix[2,3] = 0;
//четвертая строка
rotateMatrix[3,0] = 0;
rotateMatrix[3,1] = 0;
rotateMatrix[3,2] = 0;
rotateMatrix[3,3] = 1;
}
}
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
//функция, умножающая вектор на матрицу поворота
Vector resultCoordinates(RotateMatrix matrix, Vector inputVector)
{
Vector resultVector = new Vector();
resultVector.x = inputVector.x * matrix.rotateMatrix[0, 0] +
inputVector.y * matrix.rotateMatrix[1, 0] +
inputVector.z * matrix.rotateMatrix[2, 0] +
inputVector.h * matrix.rotateMatrix[3, 0];
resultVector.y = inputVector.x * matrix.rotateMatrix[0, 1] +
inputVector.y * matrix.rotateMatrix[1, 1] +
inputVector.z * matrix.rotateMatrix[2, 1] +
inputVector.h * matrix.rotateMatrix[3, 1];
resultVector.z = inputVector.x * matrix.rotateMatrix[0, 2] +
inputVector.y * matrix.rotateMatrix[1, 2] +
inputVector.z * matrix.rotateMatrix[2, 2] +
inputVector.h * matrix.rotateMatrix[3, 2];
resultVector.h = inputVector.x * matrix.rotateMatrix[0, 3] +
inputVector.y * matrix.rotateMatrix[1, 3] +
inputVector.z * matrix.rotateMatrix[2, 3] +
inputVector.h * matrix.rotateMatrix[3, 3];
return resultVector;
}
//Brushesint[] pointsSet = new int[24] { 0, 1, 2, 3, 0, 3, 4, 7, 3, 2, 5, 4, 0, 7, 6, 1, 1, 2, 5, 6, 7, 6, 5, 4 };
int[] set = new int[] { 0, 1, 2, 3, 0, 7, 4, 3, 0 , 7, 6, 1, 0, 1, 6, 5, 2, 1, 2, 5, 4, 3, 2, 5, 4, 7, 6, 5};
//данные о кубике
Vector[] cube = new Vector[8]{ new Vector(0, 0, 0),
new Vector(100, 0, 0),
new Vector(100, 0, 100),
new Vector(0, 0, 100),
new Vector(0, 100, 100),
new Vector(100, 100, 100),
new Vector(100, 100, 0),
new Vector(0, 100, 0)};
void paint(int[] set)
{
Line[] lines = new Line[40];
int lineCounter = 0;
for (int i = 0; i < set.Length - 1; i++)
{
int i1 = set[i];
int i2 = set[i + 1];
lines[lineCounter] = new Line();
lines[lineCounter].Stroke = Brushes.Black;
lines[lineCounter].X1 = cube[set[i]].GetX();
lines[lineCounter].X2 = cube[set[i + 1]].GetX();
lines[lineCounter].Y1 = cube[set[i]].GetY();
lines[lineCounter].Y2 = cube[set[i + 1]].GetY();
this.grid1.Children.Add(lines[lineCounter]);
lineCounter++;
}
}
DispatcherTimer timer = null;
int angle = 30;
void timerStart()
{
timer = new DispatcherTimer(); // если надо, то в скобках указываем приоритет, например DispatcherPriority.Render
timer.Tick += new EventHandler(timerTick);
timer.Interval = new TimeSpan(0, 0, 0, 0, 500);
timer.Start();
}
void timerTick(object sender, EventArgs e)
{
angle = angle + 30;
}
//void drawCube()
//{
// Line[] cline = new Line[24];
// int ln = 0;
// for (int i = 0; i < 6; ++i)
// {
// int offset = i * 4;
// for (int j = 0; j < 4; ++j)
// {
// cline[ln] = new Line();
// cline[ln].Stroke = Brushes.Black;
// int a = set[offset + j];
// int b = set[offset + (j + 1) % 4];
// cline[ln].X1 = cube[a].GetX();
// cline[ln].Y1 = cube[a].GetY();
// cline[ln].X2 = cube[b].GetX();
// cline[ln].Y2 = cube[b].GetY();
// this.grid1.Children.Add(cline[ln]);
// ln++;
// }
// }
//}
public MainWindow()
{
InitializeComponent();
RotateMatrix test = new RotateMatrix(1, 1, 1, angle);
timerStart();
for (int i = 0; i < 8; i++)
{
cube[i] = resultCoordinates(test, cube[i]);
}
paint(set);
}
}
}