doupuchao4256 2016-07-10 14:52
浏览 71
已采纳

去图像绘制正方形

I currently want to set pixel images on a loop

func ParseMap(path string) {
    ...
    for _, h := range serverMap.Houses {
        houseData := Houses.GetHouse(h.ID)
        houseImage := image.NewRGBA(image.Rect(int(houseData.EntryX)-32, int(houseData.EntryY)-32, int(houseData.EntryX)+32, int(houseData.EntryY)+32))
        draw.Draw(houseImage, houseImage.Bounds(), &image.Uniform{
            backgroundColor,
        }, image.ZP, draw.Src)
        for _, tile := range h.Tiles {
            pos := tile.Position()
            if pos.Z != uint8(houseData.EntryZ) {
                continue
            }
            drawSquare(houseImage, tileColor, 12, int(pos.X), int(pos.Y))
        imgFile, _ := os.Create(fmt.Sprintf("%v/%v/%v.png", pigo.Config.String("template"), "public/houses", houseData.Name))
        png.Encode(imgFile, houseImage)
        imgFile.Close()
    }
    ...
}

I loop on a slice of Tiles that contains X, Y, Z fields but since 1 pixel looks very small I want to be each pixel 6 pixel square with the given function

func drawSquare(img *image.RGBA, c color.Color, size int, x, y int) {
    patch := image.NewRGBA(image.Rect(0, 0, size, size))
    draw.Draw(patch, patch.Bounds(), &image.Uniform{
        c,
    }, image.ZP, draw.Src)
    draw.Draw(img, image.Rect(x, y, x+size, y+size), patch, image.ZP, draw.Src)
}

However there is a problem with this function. If I want to paint one pixel this is how it will look

enter image description here

The red borders means how big the square will be BUT if I go to the next pixel the size will be overwritten

enter image description here

Instead of beeing what I am looking for

enter image description here

I hope it is clear enough what I want to achieve but I dont really know what type of algorithm I should use (if I even need one) or its just pure logic

  • 写回答

1条回答 默认 最新

  • dongluobei9359 2016-07-11 08:44
    关注

    After pondering it a bit, I think I know the issue. It is with this line:

    draw.Draw(img, image.Rect(x, y, x+size, y+size), patch, image.ZP, draw.Src)
    

    You add the size only to make each rectangle large, but you do not multiply it with the position.

    draw.Draw(img, image.Rect(x*size, y*size, x+size, y+size), patch, image.ZP, draw.Src)
    

    You might need to tweek this depending on your grid.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作