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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public double[,] mult(double[,] a, double[,] b)
{
double[,] c = new double[8, 4];
for (int i = 0; i < 8; ++i)
{
for (int j = 0; j < 4; ++j)
{
for (int k = 0; k < 4; ++k)
{
c[i, j] += a[i, k] * b[k, j];
}
}
}
return c;
}
double[,] cube = { {0.0, 0.0, 0.0, 1.0},
{100.0, 0.0, 0.0, 1.0},
{100.0, 0.0, 100.0, 1.0},
{0.0, 0.0, 100.0, 1.0},
{0.0, 100.0, 0.0, 1.0},
{100.0, 100.0, 0.0, 1.0},
{100.0, 100.0, 100.0, 1.0},
{0.0, 100.0, 100.0, 1.0} };
double[] pt = { 1.0, 1.0, 1.0 };
double n1, n2, n3;
double alpha = 0.0;
private void Form1_Paint(object sender, PaintEventArgs e)
{
double[,] rotate = new double[4, 4];
n1 = pt[0] / Math.Sqrt(pt[0] * pt[0] + pt[1] * pt[1] + pt[2] * pt[2]);
n2 = pt[1] / Math.Sqrt(pt[0] * pt[0] + pt[1] * pt[1] + pt[2] * pt[2]);
n3 = pt[2] / Math.Sqrt(pt[0] * pt[0] + pt[1] * pt[1] + pt[2] * pt[2]);
rotate[0, 0] = n1 * n1 + (1.0 - n1 * n1) * Math.Cos(alpha);
rotate[1, 0] = n1 * n2 * (1.0 - Math.Cos(alpha)) - n3 * Math.Sin(alpha);
rotate[2, 0] = n1 * n3 * (1.0 - Math.Cos(alpha)) + n2 * Math.Sin(alpha);
rotate[3, 0] = 0.0;
rotate[0, 1] = n1 * n2 * (1.0 - Math.Cos(alpha)) + n3 * Math.Sin(alpha);
rotate[1, 1] = n2 * n2 + (1.0 - n2 * n2) * Math.Cos(alpha);
rotate[2, 1] = n2 * n3 * (1.0 - Math.Cos(alpha)) - n1 * Math.Sin(alpha);
rotate[3, 1] = 0.0;
rotate[0, 2] = n1 * n3 * (1.0 - Math.Cos(alpha)) - n2 * Math.Sin(alpha);
rotate[1, 2] = n2 * n3 * (1.0 - Math.Cos(alpha)) + n1 * Math.Sin(alpha);
rotate[2, 2] = n3 * n3 + (1.0 - n3 * n3) * Math.Cos(alpha);
rotate[3, 2] = 0.0;
rotate[0, 3] = 0.0;
rotate[1, 3] = 0.0;
rotate[2, 3] = 0.0;
rotate[3, 3] = 1.0;
double[,] output = mult(cube, rotate);
Graphics g = e.Graphics;
g.TranslateTransform(this.ClientRectangle.Width / 2, this.ClientRectangle.Height / 2);
g.DrawLine(new Pen(Color.Black), new PointF((float)output[0, 0], (float)output[0, 1]), new PointF((float)output[1, 0], (float)output[1, 1]));
g.DrawLine(new Pen(Color.Black), new PointF((float)output[1, 0], (float)output[1, 1]), new PointF((float)output[2, 0], (float)output[2, 1]));
g.DrawLine(new Pen(Color.Black), new PointF((float)output[2, 0], (float)output[2, 1]), new PointF((float)output[3, 0], (float)output[3, 1]));
g.DrawLine(new Pen(Color.Black), new PointF((float)output[3, 0], (float)output[3, 1]), new PointF((float)output[0, 0], (float)output[0, 1]));
g.DrawLine(new Pen(Color.Black), new PointF((float)output[4, 0], (float)output[4, 1]), new PointF((float)output[5, 0], (float)output[5, 1]));
g.DrawLine(new Pen(Color.Black), new PointF((float)output[5, 0], (float)output[5, 1]), new PointF((float)output[6, 0], (float)output[6, 1]));
g.DrawLine(new Pen(Color.Black), new PointF((float)output[6, 0], (float)output[6, 1]), new PointF((float)output[7, 0], (float)output[7, 1]));
g.DrawLine(new Pen(Color.Black), new PointF((float)output[7, 0], (float)output[7, 1]), new PointF((float)output[4, 0], (float)output[4, 1]));
g.DrawLine(new Pen(Color.Black), new PointF((float)output[0, 0], (float)output[0, 1]), new PointF((float)output[4, 0], (float)output[4, 1]));
g.DrawLine(new Pen(Color.Black), new PointF((float)output[1, 0], (float)output[1, 1]), new PointF((float)output[5, 0], (float)output[5, 1]));
g.DrawLine(new Pen(Color.Black), new PointF((float)output[2, 0], (float)output[2, 1]), new PointF((float)output[6, 0], (float)output[6, 1]));
g.DrawLine(new Pen(Color.Black), new PointF((float)output[3, 0], (float)output[3, 1]), new PointF((float)output[7, 0], (float)output[7, 1]));
}
private void timer1_Tick(object sender, EventArgs e)
{
this.Invalidate();
this.DoubleBuffered = true;
alpha += 0.01;
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}