用C#绘制坐标轴及坐标轴刻度

用C#绘制坐标轴及坐标轴刻度

private void DrawXY(Bitmap bmp)
        {
            /* 描绘坐标轴函数 */
            Graphics g = Graphics.FromImage(bmp); /* 利用图片对象生成画板 */
            Pen pAxis = new Pen(Brushes.Black, 2);

            AdjustableArrowCap aac = new AdjustableArrowCap(5, 10); /* 定义可调箭头 */
            pAxis.CustomEndCap = aac;

            /* 整体内缩move像素 */
            float move = 50f;
            float newX = bmp.Width - move;
            float newY = bmp.Height - move;

            /* 绘制X轴 */
            PointF px1 = new PointF(move + 30f, newY);
            PointF px2 = new PointF(newX, newY);
            g.DrawLine(pAxis, px1, px2);

            /* 绘制Y轴 */
            PointF py1 = new PointF(move + 30f, move);
            PointF py2 = new PointF(move + 30f, newY);

            g.DrawLine(pAxis, py2, py1);

        }

        public static float[] DrawXLine(Bitmap bmp, double minX, double maxX, int len, int n) // 画出X轴上的刻度线,从任意值开始
        {
            Graphics g = Graphics.FromImage(bmp);
            Pen pAxis = new Pen(Brushes.Black, 2);

            float move = 50f;
            float LenX = bmp.Width - 4 * move;
            float[] axis = new float[len + 1]; /* 用于存储X坐标 */

            for (int i = 0; i <= len; i++)
            {
                axis[i] = LenX * i / len + move + 50 + 15f;
                PointF py1 = new PointF(axis[i], bmp.Height - move - 4);
                PointF py2 = new PointF(axis[i], bmp.Height - move);
                string sy = ((maxX - minX) * i / len + minX).ToString(tag);
                g.DrawLine(pAxis, py1, py2);
                int temp = 40; /* 坐标轴刻度字符的偏移量 */
                switch (sy.Length)
                {
                    /* 根据字符的位数不同设置不同的偏移量 */
                    case 1:
                        temp = 45; break;
                    case 2:
                        temp = 42; break;
                    case 3:
                        temp = 40; break;
                    case 4:
                        temp = 37; break;
                    case 5:
                        temp = 35; break;
                    case 6:
                        temp = 33; break;
                    case 7:
                        temp = 31; break;
                    case 8:
                        temp = 29; break;
                    case 9:
                        temp = 27; break;
                    case 10:
                        temp = 25; break;
                    case 11:
                        temp = 23; break;
                    default:
                        break;
                }
                g.DrawString(sy, new Font("宋体", 8f), Brushes.Black,
                    new PointF(LenX * i / len + move + 15f + temp, bmp.Height - move / 1.1f));
            }

            Pen pen = new Pen(Color.Black, 1);
            string str = "时间";
           
            g.DrawString(str, new Font("宋体", 10f), Brushes.Black,
                new PointF(bmp.Width - 10 * move / 1.5f - 100, bmp.Height - move / 1.5f + 5f));
            g.Dispose();
            return axis;
        }

        public static void DrawYLine(Bitmap bmp, double minY, double maxY, int len, int n) // 画出Y轴上的刻度线,从任意值开始
        {
            /* len表示刻度间隔数目 */
            Graphics g = Graphics.FromImage(bmp);
            Pen pAxis = new Pen(Brushes.Black, 2);

            float move = 50f;
            float LenY = bmp.Height - 4 * move;
            float tempY;

            for (int i = len; i >= 0; i--)    
            {
                tempY = LenY * i / len + move + 50;
                PointF px1 = new PointF(move + 30f, tempY);
                PointF px2 = new PointF(move + 30f + 4, tempY);
                string sx = (maxY - (maxY - minY) * (len - i) / len).ToString(tag);
                g.DrawLine(pAxis, px1, px2);
                StringFormat drawFormat = new StringFormat();
                drawFormat.Alignment = StringAlignment.Far;
                drawFormat.LineAlignment = StringAlignment.Center;
                g.DrawString(sx, new Font("宋体", 8f), Brushes.Black,
                    new PointF(move / 1.2f + 30f, tempY), drawFormat);
            }
            Pen pen = new Pen(Color.Black, 1);
            string str = "位移";
            g.DrawString(str, new Font("宋体", 10f), Brushes.Black, new PointF(move / 3, move / 2f));
            g.Dispose();
        }

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用C#和VS2005绘制坐标曲线的示例代码: 1. 在窗体中添加一个Panel控件,用于绘制坐标曲线。 2. 在窗体的Load事件中添加以下代码: ```csharp private void Form1_Load(object sender, EventArgs e) { // 设置Panel控件的背景颜色和边框样式 panel1.BackColor = Color.White; panel1.BorderStyle = BorderStyle.FixedSingle; } ``` 3. 在Panel控件的Paint事件中添加以下代码: ```csharp private void panel1_Paint(object sender, PaintEventArgs e) { // 创建Graphics对象 Graphics g = e.Graphics; // 设置画笔的颜色和宽度 Pen pen = new Pen(Color.Black, 2); // 绘制坐标轴 g.DrawLine(pen, 50, 250, 450, 250); // x轴 g.DrawLine(pen, 50, 250, 50, 50); // y轴 // 绘制坐标轴刻度和标签 Font font = new Font("宋体", 10); Brush brush = new SolidBrush(Color.Black); for (int i = 0; i <= 10; i++) { g.DrawLine(pen, 50 + i * 40, 250, 50 + i * 40, 245); // x轴刻度 g.DrawString((i * 10).ToString(), font, brush, 45 + i * 40, 255); // x轴标签 g.DrawLine(pen, 50, 250 - i * 20, 55, 250 - i * 20); // y轴刻度 g.DrawString((i * 5).ToString(), font, brush, 25, 245 - i * 20); // y轴标签 } // 绘制坐标曲线 pen.Color = Color.Red; Point[] points = new Point[11]; for (int i = 0; i <= 10; i++) { int x = 50 + i * 40; int y = 250 - i * i; points[i] = new Point(x, y); } g.DrawCurve(pen, points); } ``` 4. 运行程序,就可以看到绘制的坐标曲线了。 注意:以上代码仅为示例,实际应用中需要根据具体需求进行修改和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值