N
N
Nem0_o2015-02-14 16:37:47
Programming
Nem0_o, 2015-02-14 16:37:47

How to make lines not overlap shapes (Windows Forms)?

I am making a graph editor, and I ran into one problem - when drawing graph edges, the lines overlap the vertex, since I draw a line from the center to the center of the circle:
860c423fb6544995a695812ec08c272d.png
Can I somehow make the lines not overlap the circles? Those. like this:
0fb305e152b34c0dac1660844abe4fc9.png
Code (C#):

//рисует вершину графа
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            // проверка флага нажатия
            if (!crc_OK)
                return;

            if (step == 9)
                button1.Enabled = false;

            Point pointLocation = pictureBox1.PointToClient(Cursor.Position);
            if (pictureBox1.Image == null) { pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height); }
            var bmp = new Bitmap(pictureBox1.Image);
            var g = Graphics.FromImage(bmp);
            g.DrawString(step.ToString(), DefaultFont, Brushes.Black, e.X - 5, e.Y - 5);
            g.DrawEllipse(Pens.Black, e.X - 10, e.Y - 10, 20, 20);
            label1.Text = "Добавлена вершина " + Convert.ToString(step);
            pictureBox1.Image = bmp;
            masX[step] = e.X;
            masY[step] = e.Y;
            label3.Text = "X:" + Convert.ToString(masX[step]);
            label2.Text = "Y:" + Convert.ToString(masY[step]);
            step++;
            crc_OK = false;
        }

        //рисует ребро
        private void button2_Click(object sender, EventArgs e)
        {
            int first_toop = Convert.ToInt16(textBox1.Text);
            int second_toop = Convert.ToInt16(textBox2.Text);
            if (first_toop > 9 || second_toop > 9)
            {
                MessageBox.Show("Недопустимое значение.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            if (pictureBox1.Image == null) { pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height); }
            var bmp = new Bitmap(pictureBox1.Image);
            var g = Graphics.FromImage(bmp);
            g = Graphics.FromImage(pictureBox1.Image);
            g.DrawLine(Pens.DarkGray, masX[first_toop], masY[first_toop], masX[second_toop], masY[second_toop]);
            label1.Text = "Добавлено ребро " + Convert.ToString(first_toop) + " - " + Convert.ToString(second_toop);
            pictureBox1.Refresh();
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sumor, 2015-02-14
@Sumor

Actually two options.
1. Draw the vertices at the end and fill them with the background using FillEllipse
2. Since the size of the graph vertex is known, draw lines to the vertex border.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question