首页 > 代码库 > F# 在窗体里的PictureBox中绘制心脏线

F# 在窗体里的PictureBox中绘制心脏线

心脏线方程:r=a(1-sinθ)

转换为参数方程

x=2r(sin(t)-sin(2t)/2)

y=2r(cos(t)-cos(2t)/2)

程序代码:

// 在 http://fsharp.net 上了解有关 F# 的更多信息
// 请参阅“F# 教程”项目以获取更多帮助。

//DLL如果没有则需要手动添加引用
open System.Drawing
open System.Windows.Forms

[<EntryPoint>]
let main argv = 

    System.Console.WriteLine("心脏线绘制程序 By Tsybius")
    System.Console.WriteLine("2014年9月21日 21:53:52")
    System.Console.WriteLine("=============================\n")

    System.Console.WriteLine("正在构建窗体(Form)")

    //添加窗体Form
    let frmHeart = new Form()
    
    //设置窗体属性
    frmHeart.Name <- "frmHeart"
    frmHeart.Text <- "心脏线 r=a(1-sinθ)"
    frmHeart.AutoScaleDimensions <- new System.Drawing.SizeF(6.0f, 12.0f)
    frmHeart.AutoScaleMode <- System.Windows.Forms.AutoScaleMode.Font
    frmHeart.ClientSize <- new System.Drawing.Size(480, 458)
    frmHeart.FormBorderStyle <- System.Windows.Forms.FormBorderStyle.Fixed3D
    frmHeart.StartPosition <- System.Windows.Forms.FormStartPosition.CenterScreen
    //frmHeart.BackColor <- System.Drawing.SystemColors.ActiveCaption
    frmHeart.MaximizeBox <- false
    frmHeart.MinimizeBox <- false
    frmHeart.ShowIcon <- false
    frmHeart.TopMost <- true
    
    System.Console.WriteLine("正在构建图片框(PictureBox)")

    //设立图画框PictureBox
    let picImage = new System.Windows.Forms.PictureBox()
    
    picImage.Dock <- System.Windows.Forms.DockStyle.Fill
    picImage.Location <- new System.Drawing.Point(0, 0)
    picImage.Name <- "picImage"
    picImage.Size <- new System.Drawing.Size(480, 458)
    picImage.BackColor <- System.Drawing.SystemColors.ActiveCaption
    picImage.Image <- new Bitmap(picImage.Width, picImage.Height)
    frmHeart.Controls.Add(picImage)
    
    System.Console.WriteLine("开始绘制图像")

    //绘制图像
    let grph: Graphics = Graphics.FromImage(picImage.Image)
    
    System.Console.WriteLine("绘制横轴")

    //横轴
    grph.DrawLine((new Pen(Brushes.Green, 5.0f)), 30, 135, 450, 135) 
    grph.FillPolygon(Brushes.Green, //左侧箭头 
        [| new Point(60, 125); 
           new Point(10, 135); 
           new Point(60, 145); 
           new Point(90, 135) |]);
    grph.FillPolygon(Brushes.Green, //右侧箭头
        [| new Point(420, 125); 
           new Point(470, 135); 
           new Point(420, 145); 
           new Point(390, 135) |]);
           
    System.Console.WriteLine("绘制纵轴")

    //纵轴
    grph.DrawLine((new Pen(Brushes.Green, 5.0f)), 240, 30, 240, 428) 
    grph.FillPolygon(Brushes.Green, //上侧箭头 
        [| new Point(240, 20);
           new Point(225, 30); 
           new Point(240, 70); 
           new Point(255, 30) |]);
    grph.FillPolygon(Brushes.Green, //下侧箭头 
        [| new Point(240, 370);
           new Point(225, 400); 
           new Point(240, 445); 
           new Point(255, 400) |]);
           
    System.Console.WriteLine("绘制心脏线")

    //心脏绘制开始

    let r = 50.0
    let funt (t: int) = (float)t * 2.0 * 3.1416 / 10000.0 : float
    //横坐标
    let funx (t: float) = 
        -1 * (int)(2.0 * r * (sin(t) - sin(2.0 * t) / 2.0 + 0.5)) : int
    //纵坐标
    let funy (t: float) = 
        -1 * (int)(2.0 * r * (cos(t) - cos(2.0 * t) / 2.0 + 0.5)) : int

    let p: Point[] = 
        [| for t in 0 .. 10000 -> 
            new Point(funx(funt(t)) + 290, funy(funt(t)) + 235) |]

    grph.FillPolygon(Brushes.Pink, p)
    grph.DrawPolygon((new Pen(Brushes.Red, 5.0f)), p)
    
    //心脏绘制结束
    
    System.Console.WriteLine("绘制字符")

    let brushstr1 = new SolidBrush(Color.Blue)
    let fontstr1 = new Font("Consolas", 24.0f)
    grph.DrawString("CARDIOID", fontstr1, brushstr1, 160.0f, 175.0f )
    grph.DrawString("r=a(1-sinθ)", fontstr1, brushstr1, 140.0f, 215.0f )
    
    System.Console.WriteLine("绘制签名:Tsybius")

    let brushstr2 = new SolidBrush(Color.White)
    let fontstr2 = new Font("Consolas", 24.0f)
    grph.DrawString("TSYBIUS 2014/9/21", fontstr2, brushstr2, 150.0f, 405.0f )
    
    System.Console.WriteLine("打开窗体")

    //打开窗体
    let result = frmHeart.ShowDialog()
    
    System.Console.WriteLine()

    //按任意键继续(退出)
    System.Console.WriteLine("按任意键继续")
    let temp = System.Console.ReadKey()
    let temp = System.Console.Write("\b\t") //删除按下的“任意键”字符

    0 // 返回整数退出代码

运行示例:

END

F# 在窗体里的PictureBox中绘制心脏线