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();
}
}
}