使用浮点数绘制图表
来源:stackoverflow
时间:2024-02-23 21:48:25 245浏览 收藏
一分耕耘,一分收获!既然都打开这篇《使用浮点数绘制图表》,就坚持看下去,学下去吧!本文主要会给大家讲到等等知识点,如果大家对本文有好的建议或者看到有不足之处,非常欢迎大家积极提出!在后续文章我会继续更新Golang相关的内容,希望对大家都有所帮助!
目前,我正在尝试将现有的 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
的坐标在图像上绘制矩形。
现在,我尝试将代码转换为 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
参数在图像上绘制矩形吗?
解决方案
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
。详情请参见Golang Round to Nearest 0.05。
最好是在“开始”坐标上执行此操作,以便循环可以使用整数值,例如:
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)
另请参阅相关:Draw a rectangle in Golang?
到这里,我们也就讲完了《使用浮点数绘制图表》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习