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.

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

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源