欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

C# 绘制正弦曲线

最编程 2024-04-22 09:53:06
...

题目是这样的,就是让任务管理器里面的 CPU 曲线以一条平衡的百分之五十的直线显现出来,

然后再写另外一个程序让 CPU 曲线以正弦曲线的形式显现出来,

呵呵,很有趣吧,昨晚看着看着都入迷了,不过由于太晚,所以没有太注意,

不过有一个我还是有点好奇的,因为我以前没有写过正弦曲线出来,

所以早上一起来便写正弦曲线,当然这条正弦曲线并不能使 CPU 曲线以正弦曲线显示,

而是纯粹的以一张图片来返回正弦曲线

 如果哪位牛人有兴趣的话,那绝对可以尝试一下上面的那个题目,这种问题都想得出,太牛逼了,呵呵

大致代码如下 

 try

{

    using (Bitmap sinImage = new Bitmap(360, 120))

    {

        using (Graphics myGraphics = Graphics.FromImage(sinImage))

        {

            myGraphics.Clear(Color.White);

            myGraphics.SmoothingMode = SmoothingMode.AntiAlias;

            Rectangle r1 = new Rectangle(0, 0, 360, 20);

            Rectangle r2 = new Rectangle(0, 20, 360, 40);

            Rectangle r3 = new Rectangle(0, 60, 360, 40);

            Rectangle r4 = new Rectangle(0, 100, 360, 20);


 

            Brush brush1 = new SolidBrush(Color.OrangeRed);

            Brush brush2 = new SolidBrush(Color.SkyBlue);

            Brush brush3 = new SolidBrush(Color.Pink);

            Brush brush4 = new SolidBrush(Color.YellowGreen);


 

            myGraphics.FillRectangle(brush1, r1);

            myGraphics.FillRectangle(brush2, r2);

            myGraphics.FillRectangle(brush2, r3);

            myGraphics.FillRectangle(brush1, r4);


 

            myGraphics.DrawString("0", new Font("宋体", 8), brush1, new PointF(3, 65));

            myGraphics.DrawString("90", new Font("宋体", 8), brush1, new PointF(85, 65));

            myGraphics.DrawString("180", new Font("宋体", 8), brush1, new PointF(170, 65));

            myGraphics.DrawString("360", new Font("宋体", 8), brush1, new PointF(336, 65));


 

            Point myPoint = new Point(0, 60);

            float sinValue = 0.0F;


 

            for (int i = 0; i < 360; i++)

            {

                sinValue = Convert.ToSingle(Math.Sin(Convert.ToSingle((i * Math.PI) / 180))) * 40;

                //事实上,这里根本无需注意 sinValue 的正负

                //当其为负时,  60-sinValue 则会变大

                Point thisPoint = new Point(i, Convert.ToInt32(60 - sinValue));

                myGraphics.DrawLine(new Pen(brush3), thisPoint, myPoint);

                myPoint = thisPoint;

            }


 

        }

        using (System.IO.MemoryStream ms = new System.IO.MemoryStream())

        {

            context.Response.ContentType = "Image/PNG";

            context.Response.Clear();

            context.Response.BufferOutput = true;

            sinImage.Save(ms, ImageFormat.Png);

            ms.Flush();

            context.Response.BinaryWrite(ms.GetBuffer());

        }

    }

}

catch

{

    return;

}