locked
How can I achieve this ? RRS feed

  • Question

  • How can I create triangle with text with below configuration as Winforms App ?

    Thursday, April 9, 2020 10:32 PM

Answers

  • Hi Rehan Mubarak, 
    >> double X = (Xleft + Xright + Xtop) / 3; double Y = (Yleft + Ytop + Yleft) / 3;
    First, I put the text in the center of gravity of the triangle in my code example, you can find the center of the triangle and put the text into it.
    >> e.Graphics.DrawPolygon(new Pen(Color.Blue), points);
    Second, the parameter of this method is int type. So when you convert double to int, there will be missing precision.
    Finally, I draw a triangle by three points. but you only changed the values of X and Y, the other two points are not changed(W,H,Angle,Spacing), so they will overlap.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Rehan Mubarak Monday, April 13, 2020 2:56 PM
    Monday, April 13, 2020 9:08 AM

All replies

  • Hi Rehan Mubarak,
    First, you can use DataGridView control to store the corresponding data. Then get three ponits from the datagridview by using "dataGridView1.CurrentRow.Cells[].Value". And use Graphics.DrawPolygon method to draw a triangle.
    Finally you can use Graphics.DrawString method to draw the text value within the triangle.
    Here is a code example you can refer to.

    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        var g = e.Graphics;
        var strFont = new Font("Tahoma", 10);
        double Xtop;
        double Ytop;
        double Xleft;
        double Yleft;
        double Xright;
        double Yright;
        // convert dataGridView value to double
        bool xtop = double.TryParse(dataGridView1.CurrentRow.Cells[2].Value.ToString(), out Xtop);
        bool ytop = double.TryParse(dataGridView1.CurrentRow.Cells[3].Value.ToString(), out Ytop);
        bool xleft = double.TryParse(dataGridView1.CurrentRow.Cells[4].Value.ToString(), out Xleft);
        bool yleft = double.TryParse(dataGridView1.CurrentRow.Cells[5].Value.ToString(), out Yleft);
        bool xright = double.TryParse(dataGridView1.CurrentRow.Cells[6].Value.ToString(), out Xright);
        bool yright = double.TryParse(dataGridView1.CurrentRow.Cells[7].Value.ToString(), out Yright);
        if (xtop && ytop && xleft && yleft && xright && yright) 
        {
            Point[] points = { new Point(Convert.ToInt32(Xtop), Convert.ToInt32(Ytop)), new Point(Convert.ToInt32(Xleft), Convert.ToInt32(Yleft)), new Point(Convert.ToInt32(Xright), Convert.ToInt32(Yright)) };
            // draw a triangle with three points       
            e.Graphics.DrawPolygon(new Pen(Color.Blue), points);
            string text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            double X = (Xleft + Xright + Xtop) / 3;
            double Y = (Yleft + Ytop + Yleft) / 3;
            //draw the value of "Text display" within the triangle.
            g.DrawString(text, strFont, Brushes.Black, Convert.ToInt32(X), Convert.ToInt32(Y));
        }
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        //store the date in the DataGridView 
        DataTable dt = new DataTable();
        dt.Columns.Add("Type", typeof(System.String));
        dt.Columns.Add("Text display", typeof(System.String));
        dt.Columns.Add("X", typeof(System.Double));
        dt.Columns.Add("Y", typeof(System.Double));
        dt.Columns.Add("W", typeof(System.Double));
        dt.Columns.Add("H", typeof(System.Double));
        dt.Columns.Add("Angle", typeof(System.Double));
        dt.Columns.Add("Spacing", typeof(System.Double));
        DataRow dr = dt.NewRow();
        dr[0] = "triangle";
        dr[1] = "98";
        dr[2] = 50.9;
        dr[3] = 121.2;
        dr[4] = 10.0;
        dr[5] = 10.0;
        dr[6] = 70;
        dr[7] = 31.3;
        dt.Rows.Add(dr);
        dataGridView1.DataSource = dt;
    }

    The result:


    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Rehan Mubarak Friday, April 10, 2020 8:54 AM
    • Unmarked as answer by Rehan Mubarak Friday, April 10, 2020 1:25 PM
    Friday, April 10, 2020 6:14 AM
  • Hi Daniel,

    I tried the code but I ran into a different problem .When I decrease the height and width of the triangle, the letter 98 was not inside the triangle,How do I take of that ?  

    And also when I tried to verify the x,y coordinates of the triangle it gives me a different value .

      private void panel1_MouseMove(object sender, MouseEventArgs e)
            {
                textBox1.Text = string.Format("X: {0} , Y: {1}", Cursor.Position.X, Cursor.Position.Y);
            }

    Also, When I am trying to have two inputs of type triangle and change only the coordinates , they seems to overlap each other. Below is the output I get 

    Please let me know what I am doing wrong



    Friday, April 10, 2020 1:34 PM
  • Hi Rehan Mubarak, 
    >> double X = (Xleft + Xright + Xtop) / 3; double Y = (Yleft + Ytop + Yleft) / 3;
    First, I put the text in the center of gravity of the triangle in my code example, you can find the center of the triangle and put the text into it.
    >> e.Graphics.DrawPolygon(new Pen(Color.Blue), points);
    Second, the parameter of this method is int type. So when you convert double to int, there will be missing precision.
    Finally, I draw a triangle by three points. but you only changed the values of X and Y, the other two points are not changed(W,H,Angle,Spacing), so they will overlap.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Rehan Mubarak Monday, April 13, 2020 2:56 PM
    Monday, April 13, 2020 9:08 AM