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
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
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;
using System.Windows;
namespace WindowsFormsApplication2
{
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;
}
public bool cross(double x1, double y1, double x2, double y2)
{
return (x1 * y2 - y1 * x2) < 0;
}
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);
if (cross(output[5, 0] - output[1, 0], output[5, 1] - output[1, 1], output[4, 0] - output[5, 0], output[4, 1] - output[5, 1]))
{
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[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[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[1, 0], (float)output[1, 1]), new PointF((float)output[5, 0], (float)output[5, 1]));
}
if (cross(output[6, 0] - output[2, 0], output[6, 1] - output[2, 1], output[5, 0] - output[6, 0], output[5, 1] - output[6, 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[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[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[6, 0], (float)output[6, 1]));
}
if (cross(output[7, 0] - output[3, 0], output[7, 1] - output[3, 1], output[6, 0] - output[7, 0], output[6, 1] - output[7, 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[3, 0], (float)output[3, 1]), new PointF((float)output[7, 0], (float)output[7, 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[2, 0], (float)output[2, 1]), new PointF((float)output[3, 0], (float)output[3, 1]));
}
if (cross(output[4, 0] - output[0, 0], output[4, 1] - output[0, 1], output[7, 0] - output[4, 0], output[7, 1] - 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[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[3, 0], (float)output[3, 1]), new PointF((float)output[7, 0], (float)output[7, 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]));
}
if (cross(output[4, 0] - output[7, 0], output[4, 1] - output[7, 1], output[5, 0] - output[4, 0], output[5, 1] - output[4, 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[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]));
}
if (cross(output[1, 0] - output[2, 0], output[1, 1] - output[2, 1], output[0, 0] - output[1, 0], output[0, 1] - output[1, 1]))
{
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]));
}
}
private void timer1_Tick(object sender, EventArgs e)
{
this.Invalidate();
this.DoubleBuffered = true;
alpha += 0.01;
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}