时间控件:
private float rotationAngle;
public Form1()
{
InitializeComponent();
rotationAngle = 0.0f;
}
private void timer1_Tick(object sender, EventArgs e)
{
rotationAngle += 2.0f; // 每次定时器触发时增加旋转角度
if (rotationAngle >= 360.0f)
{
rotationAngle -= 360.0f;
}
this.Invalidate(); // 通知窗体需要重绘
}
绘制三维立体图案:
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen pen = new Pen(Color.Fuchsia, 5);
// 定义立方体的8个顶点
Point3D[] cubePoints = {
new Point3D(-100, -100, -100),
new Point3D(100, -100, -100),
new Point3D(100, 100, -100),
new Point3D(-100, 100, -100),
new Point3D(-100, -100, 100),
new Point3D(100, -100, 100),
new Point3D(100, 100, 100),
new Point3D(-100, 100, 100)
};
// 创建一个旋转矩阵
Matrix3x3 rotationMatrix = Matrix3x3.RotationY(rotationAngle) * Matrix3x3.RotationX(rotationAngle);
// 应用旋转变换到每个顶点
Point[] screenPoints = new Point[8];
for (int i = 0; i < 8; i++)
{
Point3D rotatedPoint = rotationMatrix[cubePoints[i]];
screenPoints[i] = new Point((int)rotatedPoint.X + 200, (int)rotatedPoint.Y + 200);
}
// 绘制立方体的边
g.DrawLine(pen, screenPoints[0], screenPoints[1]);
g.DrawLine(pen, screenPoints[1], screenPoints[2]);
g.DrawLine(pen, screenPoints[2], screenPoints[3]);
g.DrawLine(pen, screenPoints[3], screenPoints[0]);
g.DrawLine(pen, screenPoints[4], screenPoints[5]);
g.DrawLine(pen, screenPoints[5], screenPoints[6]);
g.DrawLine(pen, screenPoints[6], screenPoints[7]);
g.DrawLine(pen, screenPoints[7], screenPoints[4]);
g.DrawLine(pen, screenPoints[0], screenPoints[4]);
g.DrawLine(pen, screenPoints[1], screenPoints[5]);
g.DrawLine(pen, screenPoints[2], screenPoints[6]);
g.DrawLine(pen, screenPoints[3], screenPoints[7]);
}
三维图案类:
// 三维点类
public class Point3D
{
public float X, Y, Z;
public Point3D(float x, float y, float z)
{
X = x;
Y = y;
Z = z;
}
}
// 三维矩阵类
public class Matrix3x3
{
public float[,] M = new float[3, 3];
public static Matrix3x3 RotationX(float angle)
{
float cos = (float)Math.Cos(angle);
float sin = (float)Math.Sin(angle);
return new Matrix3x3
{
M = new float[,]
{
{ 1, 0, 0 },
{ 0, cos, -sin },
{ 0, sin, cos }
}
};
}
public static Matrix3x3 RotationY(float angle)
{
float cos = (float)Math.Cos(angle);
float sin = (float)Math.Sin(angle);
return new Matrix3x3
{
M = new float[,]
{
{ cos, 0, sin },
{ 0, 1, 0 },
{ -sin, 0, cos }
}
};
}
public static Matrix3x3 operator *(Matrix3x3 a, Matrix3x3 b)
{
Matrix3x3 result = new Matrix3x3();
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
result.M[i, j] = 0;
for (int k = 0; k < 3; k++)
{
result.M[i, j] += a.M[i, k] * b.M[k, j];
}
}
}
return result;
}
public Point3D this[Point3D p]
{
get
{
float x = M[0, 0] * p.X + M[0, 1] * p.Y + M[0, 2] * p.Z;
float y = M[1, 0] * p.X + M[1, 1] * p.Y + M[1, 2] * p.Z;
float z = M[2, 0] * p.X + M[2, 1] * p.Y + M[2, 2] * p.Z;
return new Point3D(x, y, z);
}
}
#文章首发挑战赛##头条创作挑战赛#