コード例 #1
0
        private void ButtonStart_Click(object sender, EventArgs e)
        {
            /// Initialization
            firstAngle  = float.Parse(textBoxFirstTrapezoid.Text);
            secondAngle = float.Parse(textBoxSecondTrapezoid.Text);
            trapezoid1  = new Trapezoid(g, 50, 150, firstAngle);
            trapezoid2  = new Trapezoid(g, 500, 150, secondAngle);
            newFigure   = null;
            int progressMax = 0;

            /// Serializing the trapezoids
            IFormatter formatter = new BinaryFormatter();
            Stream     stream1   = new FileStream("Trapezoid1.bin", FileMode.Create, FileAccess.Write, FileShare.None);

            formatter.Serialize(stream1, trapezoid1);
            stream1.Close();
            Stream stream2 = new FileStream("Trapezoid2.bin", FileMode.Create, FileAccess.Write, FileShare.None);

            formatter.Serialize(stream2, trapezoid2);
            stream2.Close();

            Collision           += CollisionAlert;
            progressColl.Minimum = (int)trapezoid1.MaxRight();
            progressMax          = Convert.ToInt32((trapezoid2.MaxLeft() - trapezoid1.MaxRight()) / 2 + trapezoid1.MaxRight());
            progressColl.Maximum = progressMax;
            ///Main actions
            timer.Start();
        }
コード例 #2
0
        private void Timer_Tick(object sender, EventArgs e)
        {
            if (trapezoid1.MaxRight() - trapezoid2.MaxLeft() >= 0)      /// If figures got into collision
            {
                if (Collision != null)
                {
                    Collision();
                }
                if (newFigure == null)              /// First tick when figure got into collision
                {
                    progressColl.Value = progressColl.Maximum;
                    /// Rotation of trapezoid2
                    Matrix matrix = new Matrix();
                    matrix.RotateAt(-(180 - firstAngle - secondAngle), trapezoid2.points[0]);
                    matrix.TransformPoints(trapezoid2.points);
                    matrix.Reset();
                    /// Moving of trapezoid2 to unite them both
                    matrix.Translate(trapezoid1.points[3].X - trapezoid2.points[0].X, trapezoid1.points[0].Y - trapezoid2.points[0].Y);
                    matrix.TransformPoints(trapezoid2.points);
                    matrix.Reset();

                    /// Serializing the NewFigure
                    newFigure = new United(g, trapezoid1.points, trapezoid2.points);
                    IFormatter formatter = new BinaryFormatter();
                    Stream     stream    = new FileStream("NewFigure.bin", FileMode.Create, FileAccess.Write, FileShare.None);
                    formatter.Serialize(stream, newFigure);
                    stream.Close();
                    /// Deserializing the figures
                    /// Trapezoid1
                    Stream    streamTrapezoid1 = new FileStream("Trapezoid1.bin", FileMode.Open, FileAccess.Read, FileShare.None);
                    Trapezoid figure1          = (Trapezoid)formatter.Deserialize(streamTrapezoid1);
                    streamTrapezoid1.Close();

                    matrix.Translate(0, -150);
                    matrix.TransformPoints(figure1.points);
                    matrix.Reset();
                    figure1.g   = graphics;
                    figure1.pen = new Pen(Color.Red, 2);
                    figure1.Draw();
                    /// Trapezoid2
                    Stream    streamTrapezoid2 = new FileStream("Trapezoid2.bin", FileMode.Open, FileAccess.Read, FileShare.None);
                    Trapezoid figure2          = (Trapezoid)formatter.Deserialize(streamTrapezoid2);
                    streamTrapezoid2.Close();

                    matrix.Translate(-450, -95);
                    matrix.TransformPoints(figure2.points);
                    matrix.Reset();
                    figure2.g   = graphics;
                    figure2.pen = new Pen(Color.Red, 2);
                    figure2.Draw();

                    /// FigureNew
                    Stream streamNewFigure = new FileStream("NewFigure.bin", FileMode.Open, FileAccess.Read, FileShare.None);
                    United figureNew       = (United)formatter.Deserialize(streamNewFigure);
                    streamNewFigure.Close();

                    matrix.Translate(-50, 50);
                    matrix.TransformPoints(figureNew.points);
                    matrix.Reset();
                    figureNew.g   = graphics;
                    figureNew.pen = new Pen(Color.Red, 2);
                    figureNew.Draw();
                }
                g.Clear(FormMain.DefaultBackColor);
                /// Rotation of newFigure
                Matrix m = new Matrix();
                m.RotateAt(5, newFigure.points[2]);
                m.TransformPoints(newFigure.points);

                newFigure.Draw();
            }
            else
            {       /// Moving of trapezoids to the center
                g.Clear(FormMain.DefaultBackColor);

                progressColl.Value = (int)trapezoid1.MaxRight();
                trapezoid1.Move(2, 0);
                trapezoid1.Scale(1.01F);

                trapezoid2.Move(-2, 0);
                trapezoid2.Scale(1.01F);

                trapezoid1.Draw();
                trapezoid2.Draw();
            }
        }