using System using System Collections Generic using System Linq using

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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.Media.Media3D;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace KurenkovaEngine
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
class vec4 {
public double x, y, z, h;
public vec4(double ix = 0, double iy = 0, double iz = 0)
{
x = ix;
y = iy;
z = iz;
h = 1;
}
public double getX()
{
return x / h;
}
public double getY()
{
return y / h;
}
public double getZ()
{
return z / h;
}
}
class mMatrix
{
public double[] data;
public mMatrix(double angle, double x, double y, double z)
{
double len = Math.Sqrt(x * x + y * y + z * z);
x /= len;
y /= len;
z /= len;
double cosAng = Math.Cos(angle);
double sinAng = Math.Sin(angle);
data = new double[16];
data[0] = cosAng + (1 - cosAng) * x * x;
data[1] = (1 - cosAng) * x * y - sinAng * z;
data[2] = (1 - cosAng) * x * z + sinAng * y;
data[3] = 0;
data[4] = (1 - cosAng) * y * x + sinAng * z;
data[5] = cosAng + (1 - cosAng) * y * y;
data[6] = (1 - cosAng) * y * z - sinAng * x;
data[7] = 0;
data[8] = (1 - cosAng) * z * x - sinAng * y;
data[9] = (1 - cosAng) * z * y + sinAng * x;
data[10] = cosAng + (1 - cosAng) * z * z;
data[11] = 0;
data[12] = 0;
data[13] = 0;
data[14] = 0;
data[15] = 1;
}
}
public partial class MainWindow : Window
{
vec4 multiply(mMatrix matr, vec4 vc)
{
vec4 ret = new vec4();
ret.x = matr.data[0] * vc.x + matr.data[1] * vc.y
+ matr.data[2] * vc.z + matr.data[3] * vc.h;
ret.y = matr.data[4] * vc.x + matr.data[5] * vc.y
+ matr.data[6] * vc.z + matr.data[7] * vc.h;
ret.z = matr.data[8] * vc.x + matr.data[9] * vc.y
+ matr.data[10] * vc.z + matr.data[11] * vc.h;
ret.h = matr.data[12] * vc.x + matr.data[13] * vc.y
+ matr.data[14] * vc.z + matr.data[15] * vc.h;
return ret;
}
vec4[] cube = new vec4[8]{new vec4(0, 0, 0), new vec4(100, 0, 0), new vec4(100, 100, 0), new vec4(0, 100, 0),
new vec4(0, 0, 100), new vec4(100, 0, 100), new vec4(100, 100, 100), new vec4(0, 100, 100)};
int[] indeces = new int[24] { 0, 1, 2, 3, 4, 5, 6, 7, 1, 2, 6, 5, 0, 3, 7, 4, 0, 4, 5, 1, 3, 2, 6, 7 };
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 = indeces[offset + j];
int b = indeces[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();
mMatrix rt = new mMatrix(0.8, 1, 1, 1);
for (int i = 0; i < 8; ++i)
{
cube[i] = multiply(rt, cube[i]);
}
drawCube();
}
}
}