Skip to content

Instantly share code, notes, and snippets.

@yakreved
Created December 23, 2013 17:18
Show Gist options
  • Save yakreved/8100940 to your computer and use it in GitHub Desktop.
Save yakreved/8100940 to your computer and use it in GitHub Desktop.
kg5 sharp
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;
namespace kompgraf5_2
{
public partial class Form1 : Form
{
public int[][] initialCoordinates = new int[][]
{
new int[]{20,40,10,1},
new int[]{40,20,10,1},
new int[]{60,20,10,1},
new int[]{80,40,10,1},
new int[]{60,60,10,1},
new int[]{40,60,10,1},
new int[]{50,40,60,1}, //Макушка
};
public int[][] initialCoordinates2 = new int[][]
{
new int[]{20,20,20,1},
new int[]{60,20,10,1},
new int[]{60,60,20,1},
new int[]{20,60,20,1},
new int[]{60,60,60,1},
new int[]{20,20,60,1},
new int[]{50,40,60,1}, //Макушка
};
int[][] tmpCoord;
public Form1()
{
InitializeComponent();
tmpCoord = initialCoordinates.Clone() as int[][];
textBox1.TextChanged += textsHandlerFilter;
textBox2.TextChanged += textsHandlerFilter;
textBox3.TextChanged += textsHandlerFilter;
textBox4.TextChanged += textsHandlerFilter;
}
private void textsHandlerFilter(object sender, EventArgs e)
{
int o = 0, f = 0; double R = 0,d=0;
if (int.TryParse(textBox1.Text, out o) && int.TryParse(textBox2.Text, out f) && double.TryParse(textBox3.Text, out R) && double.TryParse(textBox4.Text, out d))
moves(o, f, R);
}
private void moves(int o, int f, double R) //вращения
{
tmpCoord = initialCoordinates.Clone() as int[][];
double O = o * Math.PI / 180, F = f * Math.PI / 180;
double[][] M = new double[][]
{
new double[]{-Math.Sin(O), -Math.Cos(F)*Math.Cos(O), -Math.Sin(F)*Math.Cos(O), 0},
new double[]{Math.Cos(O), -Math.Cos(F)*Math.Sin(O), -Math.Sin(F)*Math.Sin(O), 0},
new double[]{0, Math.Sin(F), -Math.Cos(F), R},
new double[]{0, 0, 0, 1},
};
for (int i = 0; i < tmpCoord.Count();i++ )
{
tmpCoord[i] = mult(tmpCoord[i], M);
}
//perspective(R);
setOffset();
this.Invalidate();
}
private void perspective(double R)
{
try
{
double d = 0;
if (!double.TryParse(textBox4.Text, out d))
d = 0;
for (int i = 0; i < 7; i++)
{
if (tmpCoord[i][2] != 0)
{
tmpCoord[i][0] = Convert.ToInt32(tmpCoord[i][0] / (1 - tmpCoord[i][2] / d));
tmpCoord[i][1] = Convert.ToInt32(tmpCoord[i][1] / (1 - tmpCoord[i][2] / d));
}
}
}
catch { }
}
private void perspective2(double R)
{
double d = 0;
if (!double.TryParse(textBox4.Text, out d))
d = 0;
double[][] M = new double[][]
{
new double[]{1, 0, 0, 0},
new double[]{0, 1, 0, 0},
new double[]{0, 0, 1, d},
new double[]{0, 0, 0, 0},
};
for (int i = 0; i < tmpCoord.Count(); i++)
{
tmpCoord[i] = mult(tmpCoord[i], M);
}
}
private int[] mult(int[] vect, double[][] M)//Умножение вектора на матрицу
{
int[] res = new int[]{0,0,0,0};
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
res[i] += Convert.ToInt32(vect[j] * M[i][j]);
return res;
}
protected override void OnPaint(PaintEventArgs e)
{
Pen pen = new Pen(Color.FromArgb(255, 0, 0, 0));
//-----------Кольцо-----------
e.Graphics.DrawLine(pen, tmpCoord[0][0], tmpCoord[0][1], tmpCoord[1][0], tmpCoord[1][1]);
e.Graphics.DrawLine(pen, tmpCoord[1][0], tmpCoord[1][1], tmpCoord[2][0], tmpCoord[2][1]);
e.Graphics.DrawLine(pen, tmpCoord[2][0], tmpCoord[2][1], tmpCoord[3][0], tmpCoord[3][1]);
e.Graphics.DrawLine(pen, tmpCoord[3][0], tmpCoord[3][1], tmpCoord[4][0], tmpCoord[4][1]);
e.Graphics.DrawLine(pen, tmpCoord[4][0], tmpCoord[4][1], tmpCoord[5][0], tmpCoord[5][1]);
e.Graphics.DrawLine(pen, tmpCoord[5][0], tmpCoord[5][1], tmpCoord[0][0], tmpCoord[0][1]);
//---------------------------
e.Graphics.DrawLine(pen, tmpCoord[6][0], tmpCoord[6][1], tmpCoord[0][0], tmpCoord[0][1]);
e.Graphics.DrawLine(pen, tmpCoord[6][0], tmpCoord[6][1], tmpCoord[1][0], tmpCoord[1][1]);
e.Graphics.DrawLine(pen, tmpCoord[6][0], tmpCoord[6][1], tmpCoord[2][0], tmpCoord[2][1]);
e.Graphics.DrawLine(pen, tmpCoord[6][0], tmpCoord[6][1], tmpCoord[3][0], tmpCoord[3][1]);
e.Graphics.DrawLine(pen, tmpCoord[6][0], tmpCoord[6][1], tmpCoord[4][0], tmpCoord[4][1]);
e.Graphics.DrawLine(pen, tmpCoord[6][0], tmpCoord[6][1], tmpCoord[5][0], tmpCoord[5][1]);
}
private void setOffset()
{
for (int i = 0; i < 7; i++)
for (int j = 0; j < 2; j++)
tmpCoord[i][j] += 250;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment