none
Saving a triangle in an object list and draw another one RRS feed

  • Question

  • 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);
                }
    Thursday, September 13, 2018 12:13 AM

All replies

  • Greetings HeWhoDoesn'tKnow.

    Can you be a bit more specific about how it's not working? Is it not drawing any triangles at all, or is it only drawing the first one, or is it erasing the first one before it draws the second, or what? That's a fair bit of code to go through without being able to narrow down what we're looking for.

    Thursday, September 13, 2018 12:52 AM
  • Yes, it doesn't work the way you described it. It's drawing the first one but erased after drawing a new one.
    Thursday, September 13, 2018 1:17 AM
  • I think your problem might be where you declare the list of triangles. Every time you create a "new" list, you are effectively erasing the old list. You need to make sure that line only gets called once, or when you really do need to erase the old list.

        // Move the next line to where it is only called once.
        // Unless you really want to erase the list here.
        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);
                }

    Thursday, September 13, 2018 1:25 AM
  • I declared the list of triangles in the shape class together with the rectangle/square and circle/ellipse lists.
    Thursday, September 13, 2018 1:33 AM
  • public class Shape
        {
            public Draw draw;
            public float width;
            public float height;
            public float x;
            public float y;
            public PointF points;
            public PointF point1;
            public PointF point2;
            public PointF point3;
            public PointF[] tPoints;
            public int strokeThickness;
            public Color color;
            public List<Shape> _rectangles = new List<Shape>();
            public List<Shape> _circles = new List<Shape>();
            public List<Shape> _triangle = new List<Shape>();
            public float acceptedStroke;
            public enum Buttons
            {
                rectangle, square, circle, ellipse, triangle
            }
            public void DrawRectangle(Color c, int stroke, PointF points, float w, float h,Graphics g)
            {
                this.points = points;
                this.width = w;
                this.height = h;
                this.strokeThickness = stroke;

                //Aliasing for smooth graphics when drawing and resizing
                g.InterpolationMode = InterpolationMode.High;
                g.SmoothingMode = SmoothingMode.HighSpeed;
                g.DrawRectangle(new Pen(c,stroke), points.X, points.Y,w,h);
            }
            public void DrawSquare(Color c, int stroke, PointF points, float w, float h, Graphics g)
            {
                this.points = points;
                this.width = w;
                this.height = h;
                this.strokeThickness = stroke;

                //Aliasing for smooth graphics when drawing and resizing
                g.InterpolationMode = InterpolationMode.High;
                g.SmoothingMode = SmoothingMode.HighSpeed;
                g.DrawRectangle(new Pen(c, stroke), points.X, points.Y, w, h);
            }
            public void DrawCircle(Color c, int stroke, PointF points, float w, float h, Graphics g)
            {
                this.points = points;
                this.width = w;
                this.height = h;
                this.strokeThickness = stroke;

                //Aliasing for smooth graphics when drawing and resizing
                g.InterpolationMode = InterpolationMode.High;
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.DrawEllipse(new Pen(c, stroke), points.X, points.Y, w, h);
            }
            public void DrawEllipse(Color c, int stroke, PointF points, float w, float h, Graphics g)
            {
                this.points = points;
                this.width = w;
                this.height = h;
                this.strokeThickness = stroke;

                //Aliasing for smooth graphics when drawing and resizing
                g.InterpolationMode = InterpolationMode.High;
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.DrawEllipse(new Pen(c, stroke), points.X, points.Y, w, h);
                
            }
            public void DrawTriangle(Color c, int stroke,PointF[] tpoints, float x, float y, float w, Graphics g)
            {
                tPoints = new PointF[3];
                this.width = w;
                this.strokeThickness = stroke;
                this.x = x;
                this.y = y;
                this.tPoints = tpoints;
                float angle = 0;
                tpoints[0].X = x;
                tpoints[0].Y = y;
                tpoints[1].X = (float)(x + w * Math.Cos(angle));
                tpoints[1].Y = (float)(y + w * Math.Sin(angle));
                tpoints[2].X = (float)(x+ w * Math.Cos(angle - Math.PI / 3));
                tpoints[2].Y = (float)(y + w * Math.Sin(angle - Math.PI / 3));
                g.InterpolationMode = InterpolationMode.High;
                g.SmoothingMode = SmoothingMode.HighSpeed;
                g.DrawPolygon(new Pen(c, stroke), tPoints);
            }
        }

    }
    Thursday, September 13, 2018 1:33 AM
  • I'm just passing the values in the form1 to shape class. Shape class does all the drawing but saving is on the form1, giving the objects to the list in shape class..
    Thursday, September 13, 2018 1:35 AM
  • That code looks hideous, for a number of reasons. It's a bit of a miracle the other shapes work.

    I'm not sure of the exact problem, but I suspect you might be passing the same x, y, and w to each call of DrawTriangle. That's because the other points - tpoints - are just reset but never used.

    If the Shape object already contains the list of points, colour, and stroke, the drawing method should be much, much simpler. Something like so.

            public void DrawTriangle(Graphics g)
            {
                // Set the interpolation and smoothing.
                g.InterpolationMode = InterpolationMode.High;
                g.SmoothingMode = SmoothingMode.HighSpeed;
    
                // Draw using the polygon, thickness, and colour which we should already have (if we don't, then why are they in the object at all?).
                g.DrawPolygon(new Pen(color, strokeThickness), tPoints);
            }

    Thursday, September 13, 2018 2:09 AM
  • I also notice the angle in your DrawTriangle method is always set to zero. That would mean your triangles always have the same shape. Is that correct?
    Thursday, September 13, 2018 2:22 AM
  • yes, it's supposed to be equilateral
    Thursday, September 13, 2018 3:06 AM