Matplotlib尝试

浅浅尝试从功能上复刻Python的MatPlotLib功能,基于CGdip

目前进度8.15:仅支持plot函数 x,y均大于0情况下的图形绘制,显示效果一般。支持Ctrl+s保存图片。

; Author: Mono
; Time: 2022.8.15
; MatPlotLib Test

#Include "CGdip.ahk"

Class MatPlotLib
{
    Class AHKPlot
    {
        Static SetWidth := 800
        Static SetHeight := 600
        
        Static Create_Line(Graphics, x0, y0, x1, y1, Color := "#ff0000", Options*)
        {
            Opt := 
            {
                color : "#ff0000"
            }
            
            For i in Options
            {
                
            }
            
            pPen := CGdip.Pen.Create("0xff" SubStr(Opt.color, 2), 3)
            Graphics.DrawLine(pPen, x0, y0, x1, y1)
        }
        
        Static Create_FillRectangle(Graphics, x, y, w, h, Color := "#ffffff")
        {
            Opt := 
            {
                color : "#ffffff"
            }
            
            pBrush := CGdip.Brush.SolidFill("0xff" SubStr(Opt.color, 2))
            Graphics.FillRectangle(pBrush, x, y, w, h)
        }
        
        Static FindMax(Arr)
        {
            MaxNum := Arr[-1]
            
            For i in Arr
                Maxnum := (i > MaxNum) ? i : MaxNum
            
            Return MaxNum
        }
        
        Static Plot(xPoints, yPoints)
        {
            this.AHKGui := Gui()
            this.AHKGui.Title := "未命名"
            this.AHKGui.Opt("-Caption +E0x80000")
            
            if !pToken := CGdip.Startup()
                MsgBox "Load Gdip Error!"
            
            this.Width := A_ScreenWidth, this.Height := A_ScreenHeight
            this.hdc := CreateCompatibleDC()
            this.hbm := CreateDIBSection(this.Width, this.Height)

            obm := SelectObject(this.hdc, this.hbm)
            this.canvas := CGdip.Graphics.FromHDC(this.hdc)
            this.canvas.SetSmoothingMode(4)
            Maxx := this.FindMax(xPoints)
            Maxy := this.FindMax(yPoints)
            Mulx := this.Width / Maxx
            Muly := this.Height / Maxy
            Mul2x := this.SetWidth / this.Width
            Mul2y := this.SetHeight / this.Height
            Addx := this.Width / 2 - this.SetWidth / 2
            Addy := this.Height / 2 - this.SetHeight / 2
            this.Create_FillRectangle(this.canvas, -5 + Addx, -5 + Addy, this.SetWidth + 10, this.SetHeight + 10)
            
            Loop xPoints.Length - 1
            {
                x0 := xPoints[A_Index] * Mulx * Mul2x + Addx
                y0 := (this.Height - yPoints[A_Index] * Muly) * Mul2y + Addy
                x1 := xPoints[A_Index + 1] * Mulx * Mul2x + Addx
                y1 := (this.Height - yPoints[A_Index + 1] * Muly) * Mul2y + Addy
                this.Create_Line(this.canvas, x0, y0, x1, y1)
            }
            
            Return this.AHKGui
        }
        
        Static SetWin(Width, Height)
        {
            this.SetWidth := Width
            this.SetHeight := Height
        }
        
        Static Show()
        {
            Hotkey("^s", Save)
            this.AHKGui.Show("NA")
            UpdateLayeredWindow(this.AHKGui.hwnd, this.hdc, 0, 0, this.Width, this.Height)
            
            Save(*)
            {
                SelectedFolder := DirSelect(, 3)
                
                if !SelectedFolder
                    Return
                
                DllCall("gdiplus\GdipCreateBitmapFromHBITMAP", "Ptr", this.hbm, "Ptr", 0, "Ptr*", &pBitmap := 0)
                CGdip.Bitmap(pBitmap).Save(SelectedFolder "\" A_Now ".png")
                Hotkey("^s", "Off")
                ExitApp
            }
        }
    }
}

测试代码:

MatPlotLib.AHKPlot.Plot([0,3,5,6],[0,7,80,100])
MatPlotLib.AHKPlot.Show()

效果图:

Matplotlib尝试

所需库文件:

CGdip.ahk

提取码:mono复制
解压码:无

给TA捐赠
共{{data.count}}人
人已捐赠
其他应用

影子输入法集成工具之剪切板工具

2022-8-15 7:35:19

其他

用Vim当命令行工具替换内容

2022-8-15 20:40:46

2 条回复 A文章作者 M管理员
  1. hexuren

    ??

  2. hexuren

个人中心
购物车
优惠劵
有新私信 私信列表
搜索