dpw5865 2018-03-01 13:35
浏览 10
已采纳

相同的算法,Go中有多个输入和输出类型的可能性?

I'm currently using draw2d lib to render some image. I noticed the core algorithm and method are the same for building SVG, or PNG images.

I do need to render this images as SVG (for web use) and PNG (for PDF use)

The only difference is at the entry type and output.

for PNG rendering I've

as input:

var gc *draw2dimg.GraphicContext
var img *image.RGBA

img = image.NewRGBA(image.Rect(0, 0, xSize, ySize))
gc = draw2dimg.NewGraphicContext(img)

as output:

draw2dimg.SaveToPngFile(FileName, img)

and for SVG I've:

as input:

var gc *draw2dsvg.GraphicContext
var img *draw2dsvg.Svg

img = draw2dsvg.NewSvg()
gc = draw2dsvg.NewGraphicContext(img)

as output:

draw2dsvg.SaveToSvgFile(FileName, img)

between input and output I've the same implementation.

Is there any way in Go to use different input type and get the same implementation without have to duplicate some code?

  • 写回答

2条回答 默认 最新

  • douyi9787 2018-03-02 07:23
    关注

    As I mentioned in the comment, try to refactor your code by moving the core algorithm part into a function or may be to a different package. To illustrate the idea, bellow is the refactored version of example in https://github.com/llgcode/draw2d README.

    package main
    
    import (
        "image"
        "image/color"
    
        "github.com/llgcode/draw2d"
        "github.com/llgcode/draw2d/draw2dimg"
        "github.com/llgcode/draw2d/draw2dpdf"
        "github.com/llgcode/draw2d/draw2dsvg"
    )
    
    func coreDraw(gc draw2d.GraphicContext) {
        // Set some properties
        gc.SetFillColor(color.RGBA{0x44, 0xff, 0x44, 0xff})
        gc.SetStrokeColor(color.RGBA{0x44, 0x44, 0x44, 0xff})
        gc.SetLineWidth(5)
    
        // Draw a closed shape
        gc.BeginPath()    // Initialize a new path
        gc.MoveTo(10, 10) // Move to a position to start the new path
        gc.LineTo(100, 50)
        gc.QuadCurveTo(100, 10, 10, 10)
        gc.Close()
        gc.FillStroke()
    }
    
    func main() {
        format := "svg"
    
        switch format {
        case "png":
            dest := image.NewRGBA(image.Rect(0, 0, 297, 210.0))
            gc := draw2dimg.NewGraphicContext(dest)
            coreDraw(gc)
            draw2dimg.SaveToPngFile("hello.png", dest)
        case "pdf":
            dest := draw2dpdf.NewPdf("L", "mm", "A4")
            gc := draw2dpdf.NewGraphicContext(dest)
            coreDraw(gc)
            draw2dpdf.SaveToPdfFile("hello.pdf", dest)
        case "svg":
            img := draw2dsvg.NewSvg()
            gc := draw2dsvg.NewGraphicContext(img)
            coreDraw(gc)
            draw2dsvg.SaveToSvgFile("hello.svg", img)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动