代码之家  ›  专栏  ›  技术社区  ›  HEWhoDoesn'tKnow

更新绘制形状的位置

  •  -1
  • HEWhoDoesn'tKnow  · 技术社区  · 7 年前

    我收集了一些形状。

    public List _shapes=新建列表();

    然后我想做的是调整所有形状的位置(x,y)在它里面,当我规模的三角形。这是第一张图片:

    enter image description here

    enter image description here

    if(rb_Both.Checked)
    {
        tbox_Width.Text = scaledSize.ToString();
        //Updates both width and height, x-axis and y-axis while scaling
        triangleWidth = (float)(((Convert.ToInt32(tbox_Width.Text)) * 96) / 25.4);
        triangleHeight = ((float)((triangleWidth * (Math.Sqrt(3))) / 2));
        tX = (s.center.X - (triangleWidth / 2f)) - 0.5f;
        tY = (s.center.Y + (triangleHeight / 3));
        trackBar_Size.Maximum = maximumValue;
    }
    
    //Adjusts the center of the triangle while it is scaled up
    if (s.tPoints[2].Y <= 15)
    {
        s.x = (s.center.X - (w / 2));
        s.y = (s.center.Y - (h / 2));
        s.center.Y = 250 + (((Convert.ToInt32(tbox_Width.Text)) - 110) * 2);
    }
    else if (Convert.ToInt32(tbox_Width.Text) <= 114)
       s.center.Y = 250;
    break;
    

    形状类

    public Draw draw;
        public float width { get; set; }
        public float height { get; set; }
        public float x { get; set; }
        public float y { get; set; }
        public PointF center = new PointF(250f,250f);
        public PointF points { get; set; }
        public PointF[] tPoints { get; set; }
        public float triangleOffset { get; set; }
        public int strokeThickness { get; set; }
        public Color color { get; set; }
        public float userDefinedWidth { get; set; }
        public float userDefinedHeight { get; set; }
        public int userDefinedStroke { get; set; }
        public ShapeType type;
        public Status status;
        public enum ShapeType
        {
            rectangle, square, circle, ellipse, triangle, image
        }
        public enum Status
        {
            editing, start
        }
        //Methods
        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;
            this.type = ShapeType.rectangle;
            //Aliasing for smooth graphics when drawing and resizing
            g.InterpolationMode = InterpolationMode.High;
            g.SmoothingMode = SmoothingMode.HighSpeed;
            Rectangle rect = new Rectangle((int)points.X, (int)points.Y, (int)w, (int)h);
            g.DrawRectangle(new Pen(c,stroke), points.X, points.Y,w,h);
        }
        public GraphicsPath RoundedRect(RectangleF Rect, float Radius)
        {
            GraphicsPath path2;
            if (Rect.Height >= Rect.Width)
            {
                Radius = (Rect.Height / 6f) * (Radius * 0.2f);
            }
            else
               {
                Radius = (Rect.Width / 6f) * (Radius * 0.2f);
            }
            float width = Radius * 2f;
            SizeF size = new SizeF(width, width);
            RectangleF rect = new RectangleF(Rect.Location, size);
            GraphicsPath path = new GraphicsPath();
            if (Radius == 0f)
            {
            path.AddRectangle(Rect);
            path2 = path;
            }
            else
            {
                path.AddArc(rect, 180f, 90f);
                rect.X = Rect.Right - width;
                path.AddArc(rect, 270f, 90f);
                rect.Y = Rect.Bottom - width;
                path.AddArc(rect, 0f, 90f);
                rect.X = Rect.Left;
                path.AddArc(rect, 90f, 90f);
                path.CloseFigure();
                path2 = path;
            }
            return path2;
        }
        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;
            this.type = ShapeType.square;
            //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;
            this.type = ShapeType.circle;
            //Aliasing for smooth graphics when drawing and resizing
            g.InterpolationMode = InterpolationMode.High;
            g.SmoothingMode = SmoothingMode.AntiAlias;
            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;
            this.type = ShapeType.ellipse;
            //Aliasing for smooth graphics when drawing and resizing
            g.InterpolationMode = InterpolationMode.High;
            g.SmoothingMode = SmoothingMode.AntiAlias;
            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;
            this.type = ShapeType.triangle;
            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);
        }
    

        public Shape s;
        public List<Shape> _shapes = new List<Shape>();
        public Shape.ShapeType type;
    
        //Methods
    
        public void DrawAllShapes(object sender, PaintEventArgs e)
        {
            foreach(Shape shape in _shapes)
            {
                switch (shape.type)
                {
                    case Shape.ShapeType.rectangle:
                        shape.DrawRectangle(shape.color, shape.strokeThickness, shape.points, shape.width, shape.height, e.Graphics);
                        break;
                    case Shape.ShapeType.square:
                        shape.DrawSquare(shape.color, shape.strokeThickness, shape.points, shape.width, shape.height, e.Graphics);
                        break;
                    case Shape.ShapeType.circle:
                        shape.DrawCircle(shape.color, shape.strokeThickness, shape.points, shape.width, shape.height, e.Graphics);
                        break;
                    case Shape.ShapeType.ellipse:
                        shape.DrawEllipse(shape.color, shape.strokeThickness, shape.points, shape.width, shape.height, e.Graphics);
                        break;
                    case Shape.ShapeType.triangle:
                        shape.DrawTriangle(shape.color, shape.strokeThickness, shape.tPoints.ToArray(), shape.x, shape.y, shape.width, e.Graphics);
                        break;
                }
            }
        }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   HEWhoDoesn'tKnow    7 年前

    通过将Shape类中的Center变量设置为 .

    推荐文章