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
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 PerspectiveCube
{
public partial class Form1 : Form
{
Pen black = new Pen(Color.Black, 3);
Pen gray = new Pen(Color.Gray);
public Form1()
{
InitializeComponent();
}
public double[,] mult(double[,] a, double[,] b)
{
double[,] c = new double[a.GetLength(0), b.GetLength(1)];
for (int i = 0; i < a.GetLength(0); ++i)
{
for (int j = 0; j < b.GetLength(1); ++j)
{
c[i, j] = 0;
for (int k = 0; k < a.GetLength(1); ++k)
{
c[i, j] = c[i, j] + a[i, k] * b[k, j];
}
}
}
return c;
}
public double[,] check(double[,] a)
{
for (int i = 0; i < a.GetLength(0); i++)
{
if (a[i, a.GetLength(1) - 1] != 1)
{
for (int j = 0; j < a.GetLength(1); j++)
{
a[i, j] = a[i, j] / a[i, a.GetLength(1) - 1];
}
}
}
return a;
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
double[,] cube = { {0, 0, 0, 1},
{100, 0, 0, 1},
{100, 0, 100, 1},
{0, 0, 100, 1},
{0, 100, 0, 1},
{100, 100, 0, 1},
{100, 100, 100, 1},
{0, 100, 100, 1} };
double[,] result = new double[8, 4];
double[,] t = new double[4, 4] { { 0.707, 0, 0, 0.002828 },
{ 0, 1, 0, 0 },
{ 0.707, 0, 0, -0.002828 },
{ 0, 40, 0, 1 } };
double[,] tochki_shoda = new double[3, 4];
double[,] ishodnie_tochki = new double[3, 4] { { 1, 0, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 0, 1, 0 } };
result = mult(cube, t);
result = check(result);
tochki_shoda = mult(ishodnie_tochki, t);
tochki_shoda = check(tochki_shoda);
g.TranslateTransform(this.ClientRectangle.Width / 2, this.ClientRectangle.Height / 2);
g.DrawLine(black, new PointF((float)result[0, 0], (float)result[0, 1]), new PointF((float)result[1, 0], (float)result[1, 1]));//ab
g.DrawLine(black, new PointF((float)result[1, 0], (float)result[1, 1]), new PointF((float)result[2, 0], (float)result[2, 1]));//bc
g.DrawLine(black, new PointF((float)result[2, 0], (float)result[2, 1]), new PointF((float)result[3, 0], (float)result[3, 1]));//cd
g.DrawLine(black, new PointF((float)result[3, 0], (float)result[3, 1]), new PointF((float)result[0, 0], (float)result[0, 1]));//da
g.DrawLine(black, new PointF((float)result[0, 0], (float)result[0, 1]), new PointF((float)result[4, 0], (float)result[4, 1]));//ah
g.DrawLine(black, new PointF((float)result[1, 0], (float)result[1, 1]), new PointF((float)result[5, 0], (float)result[5, 1]));//bg
g.DrawLine(black, new PointF((float)result[2, 0], (float)result[2, 1]), new PointF((float)result[6, 0], (float)result[6, 1]));//cf
g.DrawLine(black, new PointF((float)result[3, 0], (float)result[3, 1]), new PointF((float)result[7, 0], (float)result[7, 1]));//de
g.DrawLine(black, new PointF((float)result[7, 0], (float)result[7, 1]), new PointF((float)result[4, 0], (float)result[4, 1]));//eh
g.DrawLine(black, new PointF((float)result[4, 0], (float)result[4, 1]), new PointF((float)result[5, 0], (float)result[5, 1]));//hg
g.DrawLine(black, new PointF((float)result[5, 0], (float)result[5, 1]), new PointF((float)result[6, 0], (float)result[6, 1]));//gf
g.DrawLine(black, new PointF((float)result[6, 0], (float)result[6, 1]), new PointF((float)result[7, 0], (float)result[7, 1]));//fe
g.DrawLine(gray, new PointF((float)result[0, 0], (float)result[0, 1]), new PointF((float)tochki_shoda[0, 0], (float)tochki_shoda[0, 1]));//a - x
g.DrawLine(gray, new PointF((float)result[2, 0], (float)result[2, 1]), new PointF((float)tochki_shoda[2, 0], (float)tochki_shoda[2, 1]));//c - z
g.DrawLine(gray, new PointF((float)result[3, 0], (float)result[3, 1]), new PointF((float)tochki_shoda[0, 0], (float)tochki_shoda[0, 1]));//d - x
g.DrawLine(gray, new PointF((float)result[3, 0], (float)result[3, 1]), new PointF((float)tochki_shoda[2, 0], (float)tochki_shoda[2, 1]));//d - z
g.DrawLine(gray, new PointF((float)result[4, 0], (float)result[4, 1]), new PointF((float)tochki_shoda[0, 0], (float)tochki_shoda[0, 1]));//h - x
g.DrawLine(gray, new PointF((float)result[6, 0], (float)result[6, 1]), new PointF((float)tochki_shoda[2, 0], (float)tochki_shoda[2, 1]));//f - z
g.DrawLine(gray, new PointF((float)result[7, 0], (float)result[7, 1]), new PointF((float)tochki_shoda[0, 0], (float)tochki_shoda[0, 1]));//e - x
g.DrawLine(gray, new PointF((float)result[7, 0], (float)result[7, 1]), new PointF((float)tochki_shoda[2, 0], (float)tochki_shoda[2, 1]));//e - z
}
}
}