using System using System Collections Generic using System ComponentMo

  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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
float[,] cube = new float[8, 4]
{
{0, 0, 0, 1},
{100, 0, 0, 1},
{100, 100, 0, 1},
{0, 100, 0, 1},
{0, 0, 100, 1},
{100, 0, 100, 1},
{100, 100, 100, 1},
{0, 100, 100, 1}
};
float angle = 0;
private void Form1_Load(object sender, EventArgs e)
{
cube = rotate(cube, 10, new float[] { 5, 5, 5 });
draw(cube);
}
private float[,] rotate(float[,] original, double angle, float[] vector)
{
Debug.Assert(vector.Length >= 3);
var length = Math.Sqrt(vector[0] * vector[0] + vector[1] * vector[1] + vector[2] * vector[2]);
var n1 = vector[0] / length;
var n2 = vector[1] / length;
var n3 = vector[2] / length;
var cos = Math.Cos(angle);
var sin = Math.Sin(angle);
var trans = new float[4, 4]
{
{(float)(n1 * n1 + (1 - n1 * n1) * cos), (float)(n1 * n2 * (1 - cos) + n2 * sin), (float)(n1 * n3 * (1 - cos) - n2 * sin), 0},
{(float)(n1 * n2 * (1 - cos) - n3 * sin), (float)(n2 * n2 + (1 - n2 * n2) * cos), (float)(n1 * n2 * (1 - cos) + n2 * sin), 0},
{(float)(n1 * n3 * (1 - cos) + n2 * sin), (float)(n2 * n3 * (1 - cos) - n1*sin), (float)(n3 * n3 + (1 - n3 * n3) * cos), 0},
{0, 0, 0, 1}
};
return mult(original, trans);
}
private void draw(float[,] m)
{
var bmp = new Bitmap(400, 400);
var g = Graphics.FromImage(bmp);
g.TranslateTransform(200, 200);
g.Clear(Color.White);
var p0 = new PointF(m[0, 0], m[0, 1]);
var p1 = new PointF(m[1, 0], m[1, 1]);
var p2 = new PointF(m[2, 0], m[2, 1]);
var p3 = new PointF(m[3, 0], m[3, 1]);
var p4 = new PointF(m[4, 0], m[4, 1]);
var p5 = new PointF(m[5, 0], m[5, 1]);
var p6 = new PointF(m[6, 0], m[6, 1]);
var p7 = new PointF(m[7, 0], m[7, 1]);
var pen = new Pen(Brushes.DarkOrange);
g.DrawPolygon(pen, new PointF[] {p0, p1, p2, p3});
g.DrawPolygon(pen, new PointF[] { p7, p6, p2, p3 });
g.DrawPolygon(pen, new PointF[] { p2, p6, p5, p1 });
g.DrawPolygon(pen, new PointF[] { p1, p0, p4, p5 });
g.DrawPolygon(pen, new PointF[] { p3, p0, p4, p7 });
g.DrawPolygon(pen, new PointF[] { p7, p4, p5, p6 });
pictureBox1.Image = bmp;
}
private float[,] mult(float[,] a, float[,] b)
{
Debug.Assert(a.GetLength(1) == b.GetLength(0));
var result = new float[a.GetLength(0), b.GetLength(1)];
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < b.GetLength(1); j++)
{
for (int k = 0; k < a.GetLength(1); k++)
{
result[i, j] += a[i, k] * b[k, j];
}
}
}
return result;
}
private void timer1_Tick(object sender, EventArgs e)
{
angle += 10;
angle /= 360;
cube = rotate(cube, angle, new float[] { 5, 5, 5 });
draw(cube);
}
}
}