using System using System Collections Generic using System Drawing usi

  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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
using System;
using System.Collections.Generic;
using System.Drawing;
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;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public class v4
{
public double x;
public double y;
public double z;
public double h;
public v4(double _x, double _y, double _z, double _h)
{
this.x = _x;
this.y = _y;
this.z = _z;
this.h = _h;
}
/*public v4 operator - (v4 b)
{
return new v4(this.x - b.x, this.y - b.y, this.z - b.z, this.h - b.h);
}*/
}
public bool cross(double ax, double ay, double bx, double by) {
return (ax * by - ay * bx) < 0.0;
}
public class matrix
{
public v4[] pts;
public matrix(v4[] _pts)
{
pts = new v4[_pts.Length];
_pts.CopyTo(this.pts, 0);
}
}
public v4[] cube = new v4[8];
public matrix rotation_matrix;
void init()
{
cube[0] = new v4(0, 0, 0, 1);
cube[1] = new v4(100, 0, 0, 1);
cube[2] = new v4(100, 100, 0, 1);
cube[3] = new v4(0, 100, 0, 1);
cube[4] = new v4(0, 0, 100, 1);
cube[5] = new v4(100, 0, 100, 1);
cube[6] = new v4(100, 100, 100, 1);
cube[7] = new v4(0, 100, 100, 1);
/*cube[0] = new v4(-50, -50, -50, 1);
cube[1] = new v4(50, -50, -50, 1);
cube[2] = new v4(50, 50, -50, 1);
cube[3] = new v4(-50, 50, -50, 1);
cube[4] = new v4(-50, -50, 50, 1);
cube[5] = new v4(50, -50, 50, 1);
cube[6] = new v4(50, 50, 50, 1);
cube[7] = new v4(-50, 50, 50, 1);*/
}
public v4 mult(v4 vect, matrix rotat)
{
v4 ret = new v4(0, 0, 0, 0);
ret.x = vect.x * rotat.pts[0].x + vect.y * rotat.pts[1].x +
vect.z * rotat.pts[2].x + vect.h * rotat.pts[3].x;
ret.y = vect.x * rotat.pts[0].y + vect.y * rotat.pts[1].y +
vect.z * rotat.pts[2].y + vect.h * rotat.pts[3].y;
ret.z = vect.x * rotat.pts[0].z + vect.y * rotat.pts[1].z +
vect.z * rotat.pts[2].z + vect.h * rotat.pts[3].z;
ret.h = vect.x * rotat.pts[0].h + vect.y * rotat.pts[1].h +
vect.z * rotat.pts[2].h + vect.h * rotat.pts[3].h;
ret.x /= ret.h;
ret.y /= ret.h;
ret.z /= ret.h;
ret.h = 1;
return ret;
}
public void fill_rotation_matrix(double teta,
double x,
double y,
double z)
{
double c = Math.Cos(teta);
double s = Math.Sin(teta);
double norm = Math.Sqrt(x * x + y * y + z * z);
x = x / norm;
y = y / norm;
z = z / norm;
v4[] ret = new v4[4];
ret[0] = new v4(c + (1 - c) * x * x,
(1 - c) * x * y - s * z,
(1 - c) * x * z + s * y,
0);
ret[1] = new v4((1 - c) * x * y + s * z,
c + (1 - c) * y * y,
(1 - c) * y * z - s * x,
0);
ret[2] = new v4((1 - c) * z * x - s * y,
(1 - c) * y * z + s * x,
c + (1 - c) * z * z,
0);
ret[3] = new v4(0, 0, 0, 1);
rotation_matrix = new matrix(ret);
}
public matrix rotation(double teta,
double x,
double y,
double z)
{
v4[] ret = new v4[8];
cube.CopyTo(ret, 0);
fill_rotation_matrix(teta, x, y, z);
for (int i = 0; i < 8; ++i)
{
ret[i] = mult(ret[i], rotation_matrix);
}
return new matrix(ret);
}
static int[] ind = new int[24] {0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6, 7, 7, 4, 4, 0, 5, 1, 6, 2, 7, 3};
static int[] id = new int[24] {0, 1, 2, 3, 0, 1, 5, 4, 1, 2, 6, 5, 2, 3, 7, 6, 3, 0, 4, 7, 7, 6, 5, 4};
public void draw_line(v4 a, v4 b)
{
Line ln = new Line();
ln.X1 = a.x;
ln.X2 = b.x;
ln.Y1 = a.y;
ln.Y2 = b.y;
ln.Stroke = Brushes.Black;
this.grid.Children.Add(ln);
}
public void draw_edge(v4 a, v4 b, v4 c, v4 d)
{
if (cross(b.x - a.x, b.y - a.y, c.x - a.x, c.y - a.y) == false)
{
LinearGradientBrush gr = new LinearGradientBrush();
PointCollection point_collection = new PointCollection();
point_collection.Add(new Point(a.x, a.y));
point_collection.Add(new Point(b.x, b.y));
point_collection.Add(new Point(c.x, c.y));
point_collection.Add(new Point(d.x, d.y));
Polygon polygon = new Polygon();
polygon.Points = point_collection;
polygon.Fill = Brushes.Blue;
polygon.Stroke = Brushes.Black;
this.grid.Children.Add(polygon);
}
}
public void remove_line(v4 a, v4 b)
{
Line ln = new Line();
ln.X1 = a.x;
ln.X2 = b.x;
ln.Y1 = a.y;
ln.Y2 = b.y;
ln.Stroke = Brushes.Black;
this.grid.Children.Remove(ln);
}
System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();
public double angle = 2;
public MainWindow()
{
InitializeComponent();
init();
timer.Start();
timer.Interval = TimeSpan.FromSeconds(10);
timer.Tick += new EventHandler(timer_tick);
}
public void timer_tick(object sender, EventArgs e)
{
matrix rotated = rotation(angle, 1, 1, 1);
for (int i = 0; i < 24; i += 4)
{
// draw_line(rotated.pts[ind[i]], rotated.pts[ind[i + 1]]);
draw_edge(rotated.pts[ind[i]], rotated.pts[ind[i + 1]], rotated.pts[ind[i + 2]], rotated.pts[ind[i + 3]]);
}
v4 a = new v4(0, 0, 0, 1);
v4 b = new v4(0, 100, 0, 1);
v4 c = new v4(100, 100, 0, 1);
v4 d = new v4(100, 0, 0, 1);
//draw_edge(d, c, b, a);
angle += 10;
}
}
}