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
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
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;
namespace cube
{
public partial class Form1 : Form
{
double[,] matrix = new double[4, 4];
double[,] pnts = new double[8, 4];
int[] drIndex = new int[24]{ 0, 3, 2, 1, 1, 2, 6, 5, 0, 1, 5, 4, 0, 4, 7, 3, 3, 7, 6, 2, 4, 5, 6, 7};
void mult()
{
double[,] temp = new double[8, 4];
for (int i = 0; i < 8; ++i)
{
for (int l = 0; l < 4; ++l)
{
double ans = 0;
for (int k = 0; k < 4; ++k)
{
ans += pnts[i, k] * matrix[k, l];
}
temp[i, l] = ans;
}
}
pnts = temp;
}
public Form1()
{
InitializeComponent();
double x = 1;
double y = 0;
double z = 0.3;
double angle = 0.3;
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);
matrix[0, 0] = cosAng + (1 - cosAng) * x * x;
matrix[0, 1] = (1 - cosAng) * x * y - sinAng * z;
matrix[0, 2] = (1 - cosAng) * x * z + sinAng * y;
matrix[0, 3] = 0;
matrix[1, 0] = (1 - cosAng) * y * x + sinAng * z;
matrix[1, 1] = cosAng + (1 - cosAng) * y * y;
matrix[1, 2] = (1 - cosAng) * y * z - sinAng * x;
matrix[1, 3] = 0;
matrix[2, 0] = (1 - cosAng) * z * x - sinAng * y;
matrix[2, 1] = (1 - cosAng) * z * y + sinAng * x;
matrix[2, 2] = cosAng + (1 - cosAng) * z * z;
matrix[2, 3] = 0;
matrix[3, 0] = 0;
matrix[3, 1] = 0;
matrix[3, 2] = 0;
matrix[3, 3] = 1;
pnts[0, 0] = 0;
pnts[0, 1] = 0;
pnts[0, 2] = 0;
pnts[0, 3] = 1;
pnts[1, 0] = 100;
pnts[1, 1] = 0;
pnts[1, 2] = 0;
pnts[1, 3] = 1;
pnts[2, 0] = 100;
pnts[2, 1] = 100;
pnts[2, 2] = 0;
pnts[2, 3] = 1;
pnts[3, 0] = 0;
pnts[3, 1] = 100;
pnts[3, 2] = 0;
pnts[3, 3] = 1;
////////////////////
pnts[4, 0] = 0;
pnts[4, 1] = 0;
pnts[4, 2] = 100;
pnts[4, 3] = 1;
pnts[5, 0] = 100;
pnts[5, 1] = 0;
pnts[5, 2] = 100;
pnts[5, 3] = 1;
pnts[6, 0] = 100;
pnts[6, 1] = 100;
pnts[6, 2] = 100;
pnts[6, 3] = 1;
pnts[7, 0] = 0;
pnts[7, 1] = 100;
pnts[7, 2] = 100;
pnts[7, 3] = 1;
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics view = e.Graphics;
view.TranslateTransform(100, 100);
for (int i = 0; i < drIndex.Length; i += 4)
{
double x = pnts[drIndex[i], 0] / pnts[drIndex[i], 3];
double y = pnts[drIndex[i], 1] / pnts[drIndex[i], 3];
PointF p1 = new PointF((float)x, (float) y);
x = pnts[drIndex[i + 1], 0] / pnts[drIndex[i + 1], 3];
y = pnts[drIndex[i + 1], 1] / pnts[drIndex[i + 1], 3];
PointF p2 = new PointF((float)x, (float) y);
x = pnts[drIndex[i + 2], 0] / pnts[drIndex[i + 2], 3];
y = pnts[drIndex[i + 2], 1] / pnts[drIndex[i + 2], 3];
PointF p3 = new PointF((float)x, (float)y);
x = pnts[drIndex[i + 3], 0] / pnts[drIndex[i + 3], 3];
y = pnts[drIndex[i + 3], 1] / pnts[drIndex[i + 3], 3];
PointF p4 = new PointF((float)x, (float)y);
PointF vc1 = new PointF(p2.X - p1.X, p2.Y - p1.Y);
PointF vc2 = new PointF(p3.X - p1.X, p3.Y - p1.Y);
if (vc1.X * vc2.Y - vc1.Y * vc2.X < 0)
continue;
view.DrawLine(new Pen(Color.Black), p1, p2);
view.DrawLine(new Pen(Color.Black), p2, p3);
view.DrawLine(new Pen(Color.Black), p3, p4);
view.DrawLine(new Pen(Color.Black), p4, p1);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
mult();
Invalidate();
}
}
}