C# 使用Graphics根据4个点绘制曲线

iwidth, iheight为自定义画布的长宽。(x1,y1),(x2,y3),(x3,y3),(x4,y4)为4四个不在一条线上的随机点。

Bitmap image = new Bitmap(iwidth, iheight);
            Graphics Image3 = Graphics.FromImage(image);

Image3.Clear(Color.White);//背景
            Pen pen = new Pen(Color.Red, 1);//画笔

double xLeft = x1; double yTop = y1; double xRight = x2; double yBottom = y2;
                        double xStart = x3; double yStart = y3; double xEnd = x4; double yEnd = y4;

                        //C#方法DrawArc的关键点参数
                        double x, y, width, height, startAngle, sweepAngle;
                        //startAngle椭圆中心点从x正轴顺时针起始角度,sweepAngle扫过的角度
                        //1.
                        x = xLeft;  //左起始点x坐标
                        y = yTop;   //左起始点y坐标
                        width = xRight - xLeft;    //椭圆矩形宽度
                        height = yBottom - yTop;    //椭圆矩形高度

                        //2.求圓心
                        double xc = x + width / 2;
                        double yc = y + width / 2;

                        //3.计算半径
                        double R = width / 2;

                        //4.计算起始点方向角
                        double cos_cita = (xStart - xc) / R;
                        const double PI = 3.14;
                        startAngle = Math.Acos(cos_cita) / PI * 180;
                        startAngle += 90; //acos计算出来是逆时针方向,需要转换成C#是顺时针方向,

                        //说明:根据现有数据得出的圆心是在梁图的下缘,与实际不符。且这个计算的起始角是X正轴逆时针所得,下方终止角也是
                        //所以下方画图方法将此角度转换成x正轴顺时针,并且将中心点放到梁图上缘查看,画的图才是所要的,所以需要从终止角开始画弧

                        //5.计算终止点方向角
                        cos_cita = (xEnd - xc) / R;
                        double endAngle = Math.Acos(cos_cita) / PI * 180;
                        endAngle += 90;

                        //6.计算旋转角
                        sweepAngle = startAngle - endAngle;

                        //绘制曲线
                        Image3.DrawArc(pen, (float)(x), (float)(y), (float)(width), (float)(height), (float)(endAngle - 90), (float)(sweepAngle));
                        

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值