1

I am trying to save the triangle I drew and draw again while the previous triangle is still there. I did this in rectangle, square, circle and ellipse and it worked. I don't know why it won't in Triangle. Is there something wrong in the code?

This is how I draw and "save (not working)"

Shape Class

    public void DrawTriangle(Color c, int stroke,PointF[] tpoints, float w, Graphics g)
    {
        this.width = w;
        this.strokeThickness = stroke;
        this.tPoints = tpoints;
        g.InterpolationMode = InterpolationMode.High;
        g.SmoothingMode = SmoothingMode.AntiAlias;
        g.DrawPolygon(new Pen(c, stroke), tpoints);
    }

Form 1

    public void DrawTriangle()
    {
        tC = Color.Red;
        strokeTriangle = trackBar_Stroke.Value;
        tW = Convert.ToInt32((Convert.ToInt32(tbox_Width.Text) * 96) / 25.4);
        tH = (Convert.ToInt32((tW * (Math.Sqrt(3))) / 2));
        tX = (pictureBox_Canvass.Width - tW) / 2;
        tY = ((pictureBox_Canvass.Width - (tH)) / 2) + tH;

        float angle = 0;
        t_Points[0].X = tX;
        t_Points[0].Y = tY;
        t_Points[1].X = (float)(tX + tW * Math.Cos(angle));
        t_Points[1].Y = (float)(tY + tW * Math.Sin(angle));
        t_Points[2].X = (float)(tX + tW * Math.Cos(angle - Math.PI / 3));
        t_Points[2].Y = (float)(tY + tW * Math.Sin(angle - Math.PI / 3));
    }
    public void AcceptTriangle()
    {
        Shape shape = new Shape();
        tC = Color.Gray;
        shape.strokeThickness = strokeTriangle;
        shape.width = tW;
        shape.x = tX;
        shape.y = tY;
        shape.tPoints = t_Points;
        s._triangle.Add(shape);
    }

 s.DrawTriangle(tC, strokeTriangle,t_Points, tX, tY, tW, e.Graphics);

This is how I iterate it.

public List<Shape> _triangle = new List<Shape>();
foreach(Shape shapes3 in s._triangle)
        {
            shapes3.DrawTriangle(shapes3.color, shapes3.strokeThickness, shapes3.tPoints, shapes3.width, e.Graphics);
        }
TaW
  • 53,122
  • 8
  • 69
  • 111
TerribleDog
  • 1,237
  • 1
  • 8
  • 31
  • How did you do that for Rectangle and why you cannot use the same solution for Triangle? What's the problem in the code which you shared? – Reza Aghaei Sep 12 '18 at 06:50
  • I told you that _Doesn't work_ is not a helpful problem description when just posted the exact same question yesterday. - Did you look at the data to check if they are triangles? Maybe you overwrite the same array all the time? We don't see it!! To test replace `this.tPoints = tpoints;` by `this.tPoints = tpoints.ToArray();` ! – TaW Sep 12 '18 at 07:05
  • @TaW but that really is my question, this process I'm trying with triangle - saving its object and iterating it, worked on other shapes. I don't know what I'm doing wrong here. Please bear with me. – TerribleDog Sep 13 '18 at 00:11
  • Did you try my suggestion? Also: What happens? Nothing? Errors? (Which?) Crashes? Wrong results? (Which?) – TaW Sep 13 '18 at 00:19
  • nothing happens. @TaW – TerribleDog Sep 13 '18 at 00:22
  • I just asked two questions and you answer only one. Rather unpolite, imo. – TaW Sep 13 '18 at 03:37
  • Nothing happens, so nothing to answer on the second part. lol – TerribleDog Sep 13 '18 at 06:19
  • I gave you a code fragment in my 1st comment. Did you test it??? Also: Did you use the debugger to test the data when saving and when re-drawing?? – TaW Sep 13 '18 at 07:57
  • Of course, It tried it. How the hell would I knoe if it doesn't work if I didn't. And YES – TerribleDog Sep 13 '18 at 09:01

2 Answers2

1

At two points in your code you write an array assignment like this.:

this.tPoints = tpoints;

.. and this:

shape.tPoints = t_Points;

It is a common error to assume that this creates an array with data. It doesn't. All it does is make an array variable point to an array that was there before.

The data are not duplicated. So when you overwrite the data or clear them the 'new' array now points to the changed or cleared data.

So all your objects have the very same points.

To correct create actual copies of the data!

The simplest way is to add a ToArray() call like this:

this.tPoints = tpoints.ToArray();

.. and this:

shape.tPoints = t_Points.ToArray();

To reiterate:

int[] p1 = new int[2] { 23, 42 };   // Now we have one array.
int[] p2 = new int[2] { 3, 2 };    // Now we have two arrays.
p2 = p1;                 //  Now we have only one array again.
p2[1]=1234;             // Now p1[1] is 1234
TaW
  • 53,122
  • 8
  • 69
  • 111
1

Accepting the Triangle (Saving to the list)

var triangle = new Shape
                {
                    strokeThickness = strokeTriangle,
                    color = tC,
                    tPoints = t_Points.ToArray(),
                    x=tX,
                    y=tY,
                    width = tW,
                };
                s._triangle.Add(triangle);

Iterating through the list

foreach(Shape shapes3 in s._triangle)
        {
            shapes3.DrawTriangle(shapes3.color, shapes3.strokeThickness,shapes3.tPoints.ToArray(), shapes3.x, shapes3.y, shapes3.width, e.Graphics);
        } 
TerribleDog
  • 1,237
  • 1
  • 8
  • 31