Y
Y
Yhak2017-11-09 23:11:26
css
Yhak, 2017-11-09 23:11:26

Drawing an object in C#?

Good day . I ran into a problem, but I can't draw the central drawn area in any way.
Please help with a solution
5a04c55d418e7448378609.png
.

the code
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private const double Sin60 = 0.86602540378443864676372317075294;

        private static readonly Color ImageBackColor = Color.White;

        private readonly Pen mainPen = new Pen(Color.Red, 3);

        private readonly Pen backPen = new Pen(ImageBackColor, 3);

        private int[] bigWindow;

        private int[] littleWindow;

        private const int FirstSize = 80;

        private const int SecondSize = 80;

        private const int MidleSize = 80;

        private double[,] offsets = new double[6, 2];

        private  PointF[] currentPoints = new PointF[6];

        private PointF[] startPoints = new PointF[6];

        private PointF[] endPoints = new PointF[6];

        private int stepNumber = 0;

        private const int StepsCount = 50;

        private Bitmap image;

        private Graphics graphics;

        private void goButton_Click(object sender, EventArgs e)
        {
            bigWindow = new int[] { pictureBox1.Width / 4, 3 * pictureBox1.Width / 4, pictureBox1.Height / 4, 3 * pictureBox1.Height / 4 };
            littleWindow = new int[] { pictureBox1.Width / 3, 2 * pictureBox1.Width / 3, pictureBox1.Height / 4, pictureBox1.Height / 2 };
            Draw();
        }

        private void Draw()
        {
            image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            graphics = Graphics.FromImage(image);
            graphics.Clear(ImageBackColor);
            DrawWindow();
            stepNumber = 0;
            DoAnimation(graphics);
            pictureBox1.Image = image;
        }

        private void DrawWindow()
        {
            DrawWindow(bigWindow, graphics);
            DrawWindow(littleWindow, graphics);
            graphics.DrawLine(backPen, littleWindow[0], littleWindow[2], littleWindow[1], littleWindow[2]);
        }

        private void DrawWindow(int[] window, Graphics gr)
        {
            gr.DrawLine(new Pen(Color.Black),window[0],window[2],window[0] ,window[3]);
            gr.DrawLine(new Pen(Color.Black), window[1], window[2], window[1], window[3]);
            gr.DrawLine(new Pen(Color.Black), window[0], window[2], window[1], window[2]);
            gr.DrawLine(new Pen(Color.Black), window[0], window[3], window[1], window[3]);
        }

        private void DoAnimation(Graphics graphics)
        {
            SetFirstPosition(graphics);
            WriteFigure(currentPoints, graphics, mainPen);
            timer1.Enabled = true;
        }

        private void SetFirstPosition(Graphics graphics)
        {
            Tuple<Point, Point> startPositions = GetStartPosition();
            PointF[] fisrtFigurePoints = GetFirstFigurePoints(startPositions.Item1);
            endPoints = GetSecondFigurePoints(startPositions.Item2);
            startPoints = GetStartPoints(fisrtFigurePoints);
            startPoints.CopyTo(currentPoints, 0);
            if (moveTypeRadioButton.Checked)
            {
                WriteFigure(endPoints, graphics, mainPen);
            }
            SetOffsets(currentPoints, GetMidlePoints());
        }

        private PointF[] GetMidlePoints()
        {
            float x = (float)pictureBox1.Width/2;
            float midY = (float)pictureBox1.Height/2;
            return new PointF[]
            {
                new PointF(x, midY - MidleSize/2), new PointF(x, midY - MidleSize*0.3f), new PointF(x, midY - MidleSize*0.1f), new PointF(x, midY + MidleSize*0.1f),
                new PointF(x, midY + MidleSize*0.3f), new PointF(x, midY +MidleSize/2),
            };
        }

        private Tuple<Point, Point> GetStartPosition()
        {
            return new Tuple<Point, Point>(new Point(FirstSize/2 + 20, FirstSize/2 + 20),
                new Point(pictureBox1.Width - SecondSize - 20, pictureBox1.Height - SecondSize - 20));
        }

        private PointF[] GetSecondFigurePoints(Point startPosition)
        {
            return new PointF[]
            {
                new PointF(startPosition.X - SecondSize/2, startPosition.Y - (int)(SecondSize*Sin60)), 
                new PointF(startPosition.X + SecondSize/2, startPosition.Y - (int)(SecondSize*Sin60)),
                new PointF(startPosition.X + SecondSize, startPosition.Y),
                new PointF(startPosition.X + SecondSize/2, startPosition.Y + (int)(SecondSize*Sin60)),
                new PointF(startPosition.X - SecondSize/2, startPosition.Y + (int)(SecondSize*Sin60)),
                new PointF(startPosition.X - SecondSize, startPosition.Y),
            };
        }

        private PointF[] GetFirstFigurePoints(Point startPosition)
        {
            return new PointF[]
            {
                new PointF(startPosition.X - FirstSize/2, startPosition.Y - FirstSize/2),
                new PointF(startPosition.X + FirstSize/2, startPosition.Y - FirstSize/2),
                new PointF(startPosition.X + FirstSize/2, startPosition.Y + FirstSize/2),
                new PointF(startPosition.X - FirstSize/2, startPosition.Y + FirstSize/2)
            };
        }

        private void WriteFigure(PointF[] points, Graphics gr, Pen pen)
        {
            var lines = new List<Line>();
            for (int i = 0; i < points.Length; i++)
            {
                lines.Add(new Line
                {
                    Start =  points[i],
                    End = points[(i + 1)%points.Length]
                });
            }
            List<Line> drawLines = SizzerlandCouen.Work(lines, bigWindow, false);
            drawLines.AddRange(SizzerlandCouen.Work(lines, littleWindow, true));

            //List<Line> drawLines = SizzerlandCouen.Work( SizzerlandCouen.Work(lines, bigWindow, true),littleWindow, false);

            foreach (var line in drawLines)
            {
                gr.DrawLine(pen, line.Start, line.End);
            }
        }

        private PointF[] GetStartPoints(PointF[] fisrtFigurePoints)
        {
            return new PointF[] { fisrtFigurePoints[0], fisrtFigurePoints[1], fisrtFigurePoints[2], fisrtFigurePoints[3], fisrtFigurePoints[0], fisrtFigurePoints[1]};
        }

        private void SetOffsets(PointF[] firstFigurePoints, PointF[] secondFigurePoints)
        {
            for (int i = 0; i < firstFigurePoints.Length; i++)
            {
                offsets[i, 0] = (double)(secondFigurePoints[(i + 1) % secondFigurePoints.Length].X - firstFigurePoints[i].X) /
                                (StepsCount/2);
                offsets[i, 1] = (double)(secondFigurePoints[(i + 1) % secondFigurePoints.Length].Y - firstFigurePoints[i].Y) /
                                (StepsCount/2);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            stepNumber++;
            if (stepNumber == StepsCount)
            {
                timer1.Enabled = false;
            }
            else
            {
                WriteNewFigure();
                WriteFigure(startPoints, graphics, mainPen);
                /*timer1.Enabled = false;
                MessageBox.Show("fr");
                timer1.Enabled = true;*/
            }
        }

        private void WriteNewFigure()
        {
            if (moveTypeRadioButton.Checked)
            {
                WriteFigure(currentPoints, graphics, backPen);
            }
            if (stepNumber == StepsCount/2)
            {
                SetOffsets(currentPoints, endPoints);
            }
            SetNextPosition();
            WriteFigure(currentPoints, graphics, mainPen);
            pictureBox1.Image = image;
        }

        private void SetNextPosition()
        {
            for (int j = 0; j < currentPoints.Length; j++)
            {
                currentPoints[j].X += (float) offsets[j, 0];
                currentPoints[j].Y += (float) offsets[j, 1];
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
WapSter, 2019-08-16
@mastaJoe

You need to include jQuery

C
cicatrix, 2017-11-10
@Yhak

You must draw on the Graphics object provided by Form_Paint.
When the Paint event fires, the PaintEventArgs argument will give you a Graphics object, with which you need to draw.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question