Exemple #1
0
        private PVector matrixToVec(double[][] m)
        {
            PVector v = new PVector();

            v.x = m[0][0];
            v.y = m[1][0];

            if (m.Length > 2)
            {
                v.z = m[2][0];
            }

            return(v);
        }
Exemple #2
0
        private PVector Mat_To_vec(double[][] m)
        {
            PVector v = new PVector
            {
                X = m[0][0],
                Y = m[1][0]
            };

            if (m.Length > 2)
            {
                v.Z = m[2][0];
            }

            return(v);
        }
Exemple #3
0
        private void Function_Generate(object sender, EventArgs e)
        {
            coords.Clear();

            var N     = num_func_count.Value;
            var sigma = 1.1 - (double)this.num_func_deviation.Value;

            var x_arr = new List <double>();
            var y_arr = new List <double>();
            var z_arr = new List <double>();

            if (N == 0)
            {
                return;
            }

            for (int i = 0; i < N; i++)
            {
                x_arr.Add((0.7 * Math.Cos(6 * Math.PI * (i / (float)N))));
                y_arr.Add((0.5 * Math.Sin(4 * Math.PI * (i / (float)N))));
                z_arr.Add(-1 + 2 * (i / (float)N));

                var x_avg = x_arr.Sum() / x_arr.Count;
                var y_avg = y_arr.Sum() / y_arr.Count;
                var z_avg = z_arr.Sum() / z_arr.Count;

                var deviation_x = 1 / (Math.Sqrt(2 * Math.PI) * sigma) *
                                  Math.Exp(-Math.Pow((x_arr[i] - x_avg), 2) / (2 * Math.Pow(sigma, 2)));
                var deviation_y = 1 / (Math.Sqrt(2 * Math.PI) * sigma) *
                                  Math.Exp(-Math.Pow((y_arr[i] - y_avg), 2) / (2 * Math.Pow(sigma, 2)));
                var deviation_z = 1 / (Math.Sqrt(2 * Math.PI) * sigma) *
                                  Math.Exp(-Math.Pow((z_arr[i] - z_avg), 2) / (2 * Math.Pow(sigma, 2)));

                var v = new PVector(Math.Round(x_arr[i] + deviation_x, 2),
                                    Math.Round(y_arr[i] + deviation_y, 2),
                                    Math.Round(z_arr[i] + deviation_z, 2));

                v.X = v.X > 1 ? 1 : v.X < -1 ? -1 : v.X;
                v.Y = v.Y > 1 ? 1 : v.Y < -1 ? -1 : v.Y;
                v.Z = v.Z > 1 ? 1 : v.Z < -1 ? -1 : v.Z;

                coords.Add(v);
            }

            Draw_Points();
        }
Exemple #4
0
        public CodingTrain()
        {
            points[0] = new PVector(-0.5, -0.5, -0.5);
            points[1] = new PVector(0.5, -0.5, -0.5);
            points[2] = new PVector(0.5, 0.5, -0.5);
            points[3] = new PVector(-0.5, 0.5, -0.5);
            points[4] = new PVector(-0.5, -0.5, 0.5);
            points[5] = new PVector(0.5, -0.5, 0.5);
            points[6] = new PVector(0.5, 0.5, 0.5);
            points[7] = new PVector(-0.5, 0.5, 0.5);
            size      = 10;
            angle     = 0.01;
            t         = new System.Threading.Timer(new TimerCallback(Draw),
                                                   0, 0, 1);

            InitializeComponent();
        }
Exemple #5
0
        public DVT_LR2_Form()
        {
            InitializeComponent();

            coords     = new List <PVector>();
            lineX      = new PVector[4];
            lineY      = new PVector[4];
            lineZ      = new PVector[4];
            angleDelta = 0.01;
            r          = new Random();
            pointSize  = 7;
            distance   = 150;
            delta.X    = 300;
            delta.Y    = 300;
            angleX     = 0;


            defaultMultipliers = a = new double[][] {
                new double[] { 1, 0, -1 },
                new double[] { 0, 1, 0 },
                new double[] { 1, 0, 1 }
            };

            foreach (var row in defaultMultipliers)
            {
                DataGridViewRow r = new DataGridViewRow();
                r.CreateCells(this.rotationY_grid);

                for (int i = 0; i < 3; i++)
                {
                    r.Cells[i].Value = row[i];
                }

                r.Height = 40;
                this.rotationY_grid.Rows.Add(r);
            }
        }
Exemple #6
0
 private PVector MatMul(double[][] a, PVector b)
 {
     double[][] m = Vec_To_Mat(b);
     return(Mat_To_vec(MatMul(a, m)));
 }
Exemple #7
0
        private void Draw_Points()
        {
            bmp = new Bitmap(this.frame.Width, this.frame.Height);

            lineY[0] = new PVector(-1, 1);
            lineY[1] = new PVector(-1, 0);
            lineY[2] = new PVector(-0.96, 0.1);
            lineY[3] = new PVector(-1.04, 0.1);
            var y_string = new PVector(-1.2, 0.1);

            lineX[0] = new PVector(-1, 1);
            lineX[1] = new PVector(0, 1);
            lineX[2] = new PVector(-0.1, 0.96);
            lineX[3] = new PVector(-0.1, 1.04);
            var x_string = new PVector(-0.2, 1.04);

            lineZ[0] = new PVector(-1, 1, 0);
            lineZ[1] = new PVector(-1, 1, 1);
            lineZ[2] = new PVector(-1.04, 1, 0.96);
            lineZ[3] = new PVector(-0.96, 1, 1.04);
            var z_string = new PVector(-1.04, 1, 1.04);

            rotationY = new double[][] {
                new double[] { Math.Cos(angleX) * a[0][0], 0 * a[0][1], Math.Sin(angleX) * a[0][2] },
                new double[] { 0 * a[1][0], 1 * a[1][1], 0 * a[1][2] },
                new double[] { Math.Sin(angleX) * a[2][0], 0 * a[2][1], Math.Cos(angleX) * a[2][2] }
            };

            using (Graphics g = Graphics.FromImage(bmp))
            {
                foreach (var v in coords)
                {
                    PVector rotated = MatMul(rotationY, v);

                    var x_string_rot = MatMul(rotationY, x_string);
                    var y_string_rot = MatMul(rotationY, y_string);
                    var z_string_rot = MatMul(rotationY, z_string);

                    var rotatedX = new PVector[lineX.Length];
                    var rotatedY = new PVector[lineY.Length];
                    var rotatedZ = new PVector[lineZ.Length];

                    for (int i = 0; i < lineX.Length; i++)
                    {
                        rotatedX[i] = MatMul(rotationY, lineX[i]);
                        rotatedY[i] = MatMul(rotationY, lineY[i]);
                        rotatedZ[i] = MatMul(rotationY, lineZ[i]);
                    }

                    double[][] projection =
                    {
                        new double[] { 1, 0, 0 },
                        new double[] { 0, 1, 0 }
                    };

                    PVector projected2d = MatMul(projection, rotated);
                    projected2d.Mult(distance);
                    projected2d.Move(delta);
                    int alpha = (int)((rotated.Z + 0.86) * 255 / 0.85 / 2);
                    alpha = alpha > 255 ? 255 : alpha < 50 ? 50 : alpha;

                    g.FillEllipse(new SolidBrush(Color.FromArgb((int)alpha, 255, 0, 255)), (float)projected2d.X, (float)projected2d.Y, pointSize, pointSize);

                    var x_string_proj = MatMul(projection, x_string_rot);
                    x_string_proj.Mult(distance);
                    x_string_proj.Move(delta);

                    var y_string_proj = MatMul(projection, y_string_rot);
                    y_string_proj.Mult(distance);
                    y_string_proj.Move(delta);

                    var z_string_proj = MatMul(projection, z_string_rot);
                    z_string_proj.Mult(distance);
                    z_string_proj.Move(delta);

                    g.DrawString("X", new Font("Gilroy Black", 14), Brushes.Red, (float)x_string_proj.X, (float)x_string_proj.Y);
                    g.DrawString("Y", new Font("Gilroy Black", 14), Brushes.Yellow, (float)y_string_proj.X, (float)y_string_proj.Y);
                    g.DrawString("Z", new Font("Gilroy Black", 14), Brushes.Blue, (float)z_string_proj.X, (float)z_string_proj.Y);

                    var projectedX = new PVector[lineX.Length];
                    var projectedY = new PVector[lineY.Length];
                    var projectedZ = new PVector[lineZ.Length];
                    for (int i = 0; i < lineX.Length; i++)
                    {
                        projectedX[i] = MatMul(projection, rotatedX[i]);
                        projectedX[i].Mult(distance);
                        projectedX[i].Move(delta);

                        projectedY[i] = MatMul(projection, rotatedY[i]);
                        projectedY[i].Mult(distance);
                        projectedY[i].Move(delta);

                        projectedZ[i] = MatMul(projection, rotatedZ[i]);
                        projectedZ[i].Mult(distance);
                        projectedZ[i].Move(delta);

                        if (i != 0)
                        {
                            g.DrawLine(new Pen(Color.Red, 3), (float)projectedX[i / 2].X, (float)projectedX[i / 2].Y, (float)projectedX[i % 4].X, (float)projectedX[i % 4].Y);
                            g.DrawLine(new Pen(Color.Yellow, 3), (float)projectedY[i / 2].X, (float)projectedY[i / 2].Y, (float)projectedY[i % 4].X, (float)projectedY[i % 4].Y);
                            g.DrawLine(new Pen(Color.Blue, 3), (float)projectedZ[i / 2].X, (float)projectedZ[i / 2].Y, (float)projectedZ[i % 4].X, (float)projectedZ[i % 4].Y);
                        }
                    }
                }
            }

            this.frame.Image = bmp;
        }
Exemple #8
0
        private void Draw(object obj)
        {
            Bitmap bmp = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);

            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.FillRectangle(Brushes.Black, 0, 0, bmp.Width, bmp.Height);

                double[][] rotationZ =
                {
                    new double[] { Math.Cos(angle), -Math.Sin(angle), 0 },
                    new double[] { Math.Sin(angle), Math.Cos(angle),  0 },
                    new double[] {               0,                0, 1 }
                };

                double[][] rotationX =
                {
                    new double[] { 1,                       0,                        0 },
                    new double[] { 0, (double)Math.Cos(angle), (double)-Math.Sin(angle) },
                    new double[] { 0, (double)Math.Sin(angle), (double)Math.Cos(angle)  },
                };

                double[][] rotationY =
                {
                    new double[] { (double)Math.Cos(angle), 0, (double)-Math.Sin(angle) },
                    new double[] {                       0, 1,                        0 },
                    new double[] { (double)Math.Sin(angle), 0, (double)Math.Cos(angle)  }
                };

                foreach (var v in points)
                {
                    PVector rotated = matmul(rotationY, v);
                    rotated = matmul(rotationX, rotated);
                    rotated = matmul(rotationZ, rotated);

                    double distance = 2;
                    double z        = 1 / (distance - rotated.z);

                    double[][] projection =
                    {
                        new double[] { z, 0, 0 },
                        new double[] { 0, z, 0 }
                    };

                    PVector projected2d = matmul(projection, rotated);

                    projected2d.mult(300);

                    projected2d.x += this.pictureBox1.Width / 2;
                    projected2d.y += this.pictureBox1.Height / 2;

                    int alpha = (int)((rotated.z + 0.86) * 255 / 0.85 / 2);
                    alpha = alpha > 255 ? 255 : alpha < 50 ? 50 : alpha;

                    Console.WriteLine(alpha);

                    g.FillEllipse(new SolidBrush(Color.FromArgb(alpha, 255, 255, 255)), (float)projected2d.x, (float)projected2d.y, size, size);
                }
            }

            angle += 0.01;

            this.pictureBox1.Image = bmp;
        }
Exemple #9
0
 private PVector matmul(double[][] a, PVector b)
 {
     double[][] m = vecToMat(b);
     return(matrixToVec(matmul(a, m)));
 }