亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

用“浮點數”繪制圖像

用“浮點數”繪制圖像

Go
一只名叫tom的貓 2023-07-17 14:22:06
目前,我正在嘗試將現有的 C# 項目轉換為 GoLang。該項目采用一個包含一堆坐標的 XML 文件并將它們繪制在圖像上。在 C# 中,在圖像上繪制矩形的代碼如下:public void DrawRectangle(Graphics graphics, RectangleShape rectangle){    using (var drawingPen = new Pen(Color.Black))    {        graphics.DrawRectangle(            drawingPen,            rectangle.StartX,            rectangle.StartY,            rectangle.Width,            rectangle.Height);    }}矩形由以下類定義:internal sealed class RectangleShape{    internal RectangleShape(float startX, float startY, float width, float height)    {        this.StartX = startX;        this.StartY = startY;        this.Width = width;        this.Height = height;    }    internal float StartX { get; }    internal float StartY { get; }    internal float Width { get; }    internal float Height { get; }}這意味著 C# 能夠使用定義為 的坐標在圖像上繪制矩形float?,F在,我嘗試將代碼轉換為 GoLang,其中我使用以下代碼繪制一個矩形:// DrawRect draws a rectangle with the given dimensions on the given image.func DrawRect(img *image.RGBA, rect Rectangle) {    endX := rect.X + rect.Width    endY := rect.Y + rect.Height    drawHLine(img, rect.X, rect.Y, endX)    drawHLine(img, rect.Y, endY, endX)    drawVLine(img, rect.Y, rect.X, endY)    drawVLine(img, rect.Y, endX, endY)}// PRIVATE: drawHLine draws a horizontal line with the given coordinates on the given image.func drawHLine(img *image.RGBA, startX, y, endX float32) {    col := color.RGBA{0x00, 0x00, 0x00, 0xff}    for ; startX <= endX; startX++ {        img.Set(startX, y, col)    }}// PRIVATE: drawVLine draws a vertical line with the given coordinates on the given image.func drawVLine(img *image.RGBA, startY, x, endY float32) {    col := color.RGBA{0x00, 0x00, 0x00, 0xff}    for ; startY <= endY; startY++ {        img.Set(x, startY, col)    }}矩形由以下結構定義:// Rectangle represents a rectangular shape.type Rectangle struct {    X      float32    Y      float32    Width  float32    Height float32}Go 中的示例不起作用,因為Set圖像上的函數具有以下結構:func (p *RGBA) Set(x, y int, c color.Color) {Go 有什么辦法可以使用float參數在圖像上繪制矩形嗎?
查看完整描述

1 回答

?
烙印99

TA貢獻1829條經驗 獲得超13個贊

image.Image類型是一個接口,它將圖像定義為具有整數坐標的像素,可通過以下Image.At()方法訪問:

At(x,?y?int)?color.Color

您使用的具體實現image.RGBA允許再次使用整數坐標和方法來更改像素RGBA.Set()

func?(p?*RGBA)?Set(x,?y?int,?c?color.Color)

最簡單的解決方案是將浮點坐標轉換為整數坐標。簡單地將浮點轉換為整數就是截斷,因此您應該使用舍入。0.5在轉換之前添加一個簡單的舍入。

最好是在“開始”坐標上執行此操作,以便循環可以使用整數值,例如:

func drawHLine(img *image.RGBA, startX, y, endX float32) {

? ? col := color.RGBA{0x00, 0x00, 0x00, 0xff}


? ? x1, x2 := int(startX + 0.5), int(endX + 0.5)

? ? y1 := int(y + 0.5)

? ? for x := x1; x <= x2; x++ {

? ? ? ? img.Set(x, y1, col)

? ? }

}

請注意,但是這種將浮點坐標轉換為整數的“最簡單”解決方案可能不是“最佳”。例如,如果您需要在坐標處繪制一條水平線x = 0.1,則上述解決方案將在 處繪制一條線x = 0。一種不同的解決方案可能是在 處畫一條“較強”的線x = 0,在 處畫一條“較弱”的線,從而給出從遠處觀察時x = 1該線實際上位于 處的效果。x = 0.1如果線條不是水平和垂直的,這種混疊技術肯定會給出更好的結果,但需要更多的計算(因此速度更慢)。

如果您在繪圖時確實需要浮動精度,您可以使用第 3 方庫,例如github.com/fogleman/gg它允許您傳遞float64坐標,并且它使用抗鋸齒來實現良好的結果。

例如,在圖像github.com/fogleman/gg上繪制一個矩形image.RGBA,如下所示:

var img *image.RGBA = // create your RGBA image


ctx := gg.NewContextForRGBA(img)


// Set drawing color:

ctx.SetColor(color.RGBA{0x00, 0x00, 0x00, 0xff})

// Or simply: ctx.SetRGB(0, 0, 0)


// Draw rectangle: x, y, w, h are all float64 parameters

ctx.DrawRectangle(x, y, w, h)


查看完整回答
反對 回復 2023-07-17
  • 1 回答
  • 0 關注
  • 190 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號