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;
using System.Drawing.Drawing2D;
namespace B_Splain {
public partial class Form1 : Form {
float[] B_2Dx = new float[] { 4, 110, 209, 360 };
float[] B_2Dy = new float[] { 230, 20, 128, 17 };
float[,] B_3Dx = new float[,] { { 100, 100, 100, 100 }, { 200, 200, 200, 200 }, { 300, 300, 300, 300 }, { 400, 400, 400, 400 } };
float[,] B_3Dy = new float[,] { { 100, 200, 300, 400 }, { 100, 200, 300, 400 }, { 100, 200, 300, 400 }, { 100, 200, 300, 400 } };
float[,] B_3Dz = new float[,] { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } };
float[,] uCoeff, wCoeff, Qx, Qy, Qz;
public static Pen penRed = new Pen(Color.Red, 1.5f),
penBlue = new Pen(Color.Blue, 1.5f);
public static bool isMoved = false;
public static float step = 0.074f;
public static int size = (int)(1 / step + 1);
public static Matrix3D surface3D = new Matrix3D(new float[size * size, 4]), transformMatrix;
public static PointF[,] surface = new PointF[size, size];
public Form1() {
InitializeComponent();
transformMatrix = new Matrix3D(new float[,] { {1, 0, 0, 0},
{0, (float)Math.Cos(Math.PI/10), (float)Math.Sin(Math.PI/10), 0},
{0, -(float)Math.Sin(Math.PI/10), (float)Math.Cos(Math.PI/10), 0},
{0, 0, 0, 1} });
}
private void Form1_MouseMove(object sender, MouseEventArgs e) {
if (isMoved) {
B_2Dx[2] = e.X;
B_2Dy[2] = e.Y;
B_3Dx[1, 2] = e.X;
B_3Dy[1, 2] = e.Y;
Invalidate();
}
}
private void Form1_MouseDown(object sender, MouseEventArgs e) {
isMoved = true;
}
private void Form1_MouseUp(object sender, MouseEventArgs e) {
isMoved = false;
}
public void DrawCurve() {
/*int countIter = 0;
view.Clear(this.BackColor);
for (int i = 0; i < B.Length - 1; ++i) {
view.DrawLine(pen2, B[i], B[i + 1]);
}
for (double t = 0.0; t <= 1.0; t += 0.01) {
bPoint[countIter] = new PointF((float)(Math.Pow((1 - t), 3) * B[0].X + 3 * t * Math.Pow((1 - t), 2) * B[1].X + 3 * Math.Pow(t, 2) * (1 - t) * B[2].X + Math.Pow(t, 3) * B[3].X),
(float)(Math.Pow((1 - t), 3) * B[0].Y + 3 * t * Math.Pow((1 - t), 2) * B[1].Y + 3 * Math.Pow(t, 2) * (1 - t) * B[2].Y + Math.Pow(t, 3) * B[3].Y));
countIter++;
}
for (int j = 0; j < countIter - 1; ++j) {
view.DrawLine(pen, bPoint[j], bPoint[j + 1]);
}*/
}
public void GetSurface() {
int i = 0, curPoint = 0;
for (float u = 0.0f; u <= 1.000000005f; u += step, ++i) {
int j = 0;
for (float w = 0.0f; w <= 1.000000005f; w += step, ++j) {
uCoeff = new float[1, 4] { { (1 - u) * (1 - u) * (1 - u), 3 * u * (1 - u) * (1 - u), 3 * u * u * (1 - u), u * u * u } };
wCoeff = new float[4, 1] { { (1 - w) * (1 - w) * (1 - w) },
{ 3 * w * (1 - w) * (1 - w) },
{ 3 * w * w * (1 - w) },
{ w * w * w } };
Qx = mult(uCoeff, B_3Dx);
Qx = mult(Qx, wCoeff);
Qy = mult(uCoeff, B_3Dy);
Qy = mult(Qy, wCoeff);
Qz = mult(uCoeff, B_3Dz);
Qz = mult(Qz, wCoeff);
surface[i, j] = new PointF(Qx[0, 0], Qy[0, 0]);
surface3D.transformMatrix[curPoint, 0] = Qx[0, 0];
surface3D.transformMatrix[curPoint, 1] = Qy[0, 0];
surface3D.transformMatrix[curPoint, 2] = Qz[0, 0];
surface3D.transformMatrix[curPoint, 3] = 1;
curPoint++;
}
}
}
public void DrawSurface(PaintEventArgs e) {
Graphics view = e.Graphics;
for (int k = 0; k < surface.GetLength(0); ++k) {
for (int j = 0; j < surface.GetLength(1) - 1; ++j) {
view.DrawLine(penBlue, surface[k, j], surface[k, j + 1]);
}
}
for (int k = 0; k < surface.GetLength(0); ++k) {
for (int j = 0; j < surface.GetLength(1) - 1; ++j) {
view.DrawLine(penBlue, surface[j, k], surface[j + 1, k]);
}
}
}
private void Form1_Paint(object sender, PaintEventArgs e) {
GetSurface();
DrawSurface(e);
}
float[,] mult(float[,] first, float[,] second) {
float[,] retValue = new float[first.GetLength(0), second.GetLength(1)];
for (int i = 0; i < first.GetLength(0); ++i)
for (int j = 0; j < second.GetLength(1); ++j) {
float curValue = 0;
for (int k = 0; k < first.GetLength(1); ++k)
curValue += first[i, k] * second[k, j];
retValue[i, j] = curValue;
}
return retValue;
}
}
}