{"id":13069,"date":"2023-04-14T18:01:36","date_gmt":"2023-04-14T10:01:36","guid":{"rendered":"https:\/\/blog.ywdevops.cn\/?p=13069"},"modified":"2023-04-25T09:17:06","modified_gmt":"2023-04-25T01:17:06","slug":"image","status":"publish","type":"post","link":"https:\/\/blog.ywdevops.cn\/index.php\/2023\/04\/14\/image\/","title":{"rendered":"Go\u8bed\u8a00\u4e4b\u56fe\u50cf\u5904\u7406"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">golang\u7684image\u5e93\u63d0\u4f9b\u4e86\u56fe\u50cf\u5904\u7406\u57fa\u672c\u529f\u80fd<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4e00\u3001\u8bed\u6cd5\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1\u3001image\u5e93\u7684<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">Rect()<\/mark><\/strong>\u51fd\u6570\u53ef\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u77e9\u5f62\u5bf9\u8c61\uff0c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func Rect(x0, y0, x1, y1 int) Rectangle<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5176\u4e2d\uff0cx0, y0 \u4e3a\u5de6\u4e0a\u89d2\u5750\u6807\uff0cx1, y1 \u4e3a\u53f3\u4e0b\u89d2\u5750\u6807\u3002\u8fd9\u4e2a\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a Rectangle \u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u8868\u793a\u4ee5\u6307\u5b9a\u5750\u6807\u4e3a\u5de6\u4e0a\u89d2\u548c\u53f3\u4e0b\u89d2\u6240\u6784\u6210\u7684\u77e9\u5f62\uff0c\u4f8b\u5982\uff1ar := image.Rect(0, 0, 100, 50) \u8868\u793a\u521b\u5efa\u4e00\u4e2a\u4ece\u5de6\u4e0a\u89d2\u5750\u6807 (0, 0) \u5230\u53f3\u4e0b\u89d2\u5750\u6807 (100, 50) \u7684\u77e9\u5f62\uff0c\u5c06\u5176\u4fdd\u5b58\u5728 r \u53d8\u91cf\u4e2d\uff0cRectangle\u7ed3\u6784\u4f53\u5b9e\u9645\u662fPoint\u7ed3\u6784\u4f53\u5d4c\u5957\u800c\u6765\uff0c\u56e0\u6b64image.Rect(0,0,100,50)\u8fd8\u53ef\u4ee5\u5199\u4e3aimage.Rectangle{image.Point{0,0},image.Point{100,50}}\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"389\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/04\/1-5.png\" alt=\"\" class=\"wp-image-13117\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/04\/1-5.png 708w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/04\/1-5-300x165.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/04\/1-5-600x330.png 600w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u6ce8\uff1aGolang\u7684\u56fe\u50cf\u5750\u6807\u7cfb\u4e2d,Y\u8f74\u5411\u4e0b\u662f\u6b63\u65b9\u5411\uff0cX\u8f74\u5411\u53f3\u662f\u6b63\u65b9\u5411<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2\u3001image\u5e93\u7684<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">NewRGBA()<\/mark><\/strong> \u51fd\u6570\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a RGBA \u7c7b\u578b\u7684\u56fe\u50cf\u5bf9\u8c61\uff0c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func NewRGBA(r Rectangle) *RGBA<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5176\u4e2d\uff0cr \u8868\u793a\u56fe\u50cf\u7684\u5927\u5c0f\uff0c\u662f\u4e00\u4e2a Rectangle \u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u8fd9\u4e2a\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u6307\u5411 RGBA \u7c7b\u578b\u7684\u56fe\u50cf\u5bf9\u8c61\u7684\u6307\u9488<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff1a\u521b\u5efa\u4e00\u4e2a\u5bbd\u4e3a100\u3001\u9ad8\u4e3a50\u7684 RGBA \u7c7b\u578b\u7684\u56fe\u50cf\u5bf9\u8c61\uff0c\u7ed3\u5408\u7b2c\u4e00\u6b65\u7684Rect()\u51fd\u6570\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>r := image.Rect(0, 0, 100, 50)\nimg := image.NewRGBA(r)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u4ee3\u7801\u4f1a\u521b\u5efa\u4e00\u4e2a\u7531 100&#215;50 \u4e2a\u50cf\u7d20\u70b9\u7ec4\u6210\uff0c\u6bcf\u4e2a\u50cf\u7d20\u70b9\u6709\u7ea2\u3001\u7eff\u3001\u84dd\u548c\u900f\u660e\u5ea6\u56db\u4e2a\u901a\u9053\u7684 RGBA \u7c7b\u578b\u7684\u56fe\u50cf\u5bf9\u8c61\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u5728 img \u53d8\u91cf\u4e2d\uff0c\u521b\u5efa\u5b8c\u56fe\u50cf\u5bf9\u8c61\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u6bcf\u4e2a\u50cf\u7d20\u70b9\u7684\u989c\u8272\u503c\u6765\u7ed8\u5236\u56fe\u50cf<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3\u3001image\/color\u5e93\u7684<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">RGBA<\/mark><\/strong>\u7ed3\u6784\u4f53\u662f\u4e00\u4e2a\u4ee3\u8868 RGB \u989c\u8272\u6a21\u578b\u4e2d\u7684\u4e00\u79cd\u989c\u8272\u7684\u7ed3\u6784\u4f53\u3002\u5b83\u7531\u56db\u4e2a 8 \u4f4d\u65e0\u7b26\u53f7\u6574\u6570\u7ec4\u6210\uff0c\u5206\u522b\u8868\u793a\u7ea2\u3001\u7eff\u3001\u84dd\u548c alpha\uff08\u900f\u660e\u5ea6\uff09\u901a\u9053\u7684\u503c\u3002\u5177\u4f53\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type RGBA struct {\n    R, G, B, A uint8\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5176\u4e2dR\u3001G\u3001B\u5206\u522b\u8868\u793a\u7ea2\u3001\u7eff\u3001\u84dd\u4e09\u539f\u8272\u7684\u989c\u8272\u503c\uff0c\u5176\u4e2d 0 \u8868\u793a\u5b8c\u5168\u900f\u660e\uff0c255 \u8868\u793a\u5b8c\u5168\u4e0d\u900f\u660e\uff0c<code>color.RGBA<\/code> \u7ed3\u6784\u4f53\u5b9e\u73b0\u4e86 <code>color.Color<\/code> \u63a5\u53e3\uff0c\u53ef\u4ee5\u5728\u56fe\u50cf\u7ed8\u5236\u7b49\u573a\u666f\u4e2d\u4f7f\u7528<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">4\u3001draw\u5e93<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">Draw()<\/mark><\/strong>\u51fd\u6570\u662f\u4e00\u4e2a\u7528\u4e8e\u5728\u76ee\u6807\u56fe\u50cf\u4e0a\u7ed8\u5236\u6e90\u56fe\u50cf\u7684\u51fd\u6570\u3002\u5b83\u53ef\u4ee5\u5b9e\u73b0\u5c06\u4e00\u4e2a\u56fe\u50cf\u7c98\u8d34\u5230\u53e6\u4e00\u4e2a\u56fe\u50cf\u4e0a\u7684\u529f\u80fd\uff0c\u652f\u6301\u591a\u79cd\u4e0d\u540c\u7684\u64cd\u4f5c\u6a21\u5f0f\u548c\u6df7\u5408\u6a21\u5f0f\u3002\u4f7f\u7528\u65b9\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import \"image\"\nimport \"image\/draw\"\n\/\/ \u521b\u5efa\u76ee\u6807\u56fe\u50cf\u548c\u6e90\u56fe\u50cf\ndst := image.NewRGBA(image.Rect(0, 0, 100, 100))\nsrc := image.NewUniform(color.RGBA{255, 0, 0, 255})\n\/\/ \u5728 dst \u4e0a\u7ed8\u5236 src\ndraw.Draw(dst, dst.Bounds(), src, image.Point{}, draw.Src)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u5c06\u5728 dst \u56fe\u50cf\u4e0a\u7ed8\u5236\u4e00\u4e2a\u7ea2\u8272\u56fe\u5f62\uff0c\u5927\u5c0f\u4e3a dst \u7684\u8fb9\u754c\u6846\uff08\u672c\u4f8b\u4e2d\u4e3a [0,0,100,100]\uff09\u3002draw.Draw() \u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u76ee\u6807\u56fe\u50cf\u5bf9\u8c61\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u7ed8\u5236\u533a\u57df\u7684\u77e9\u5f62\u8fb9\u754c\u6846\uff0c\u7b2c\u4e09\u4e2a\u53c2\u6570\u662f\u6e90\u56fe\u50cf\u5bf9\u8c61\uff0c\u7b2c\u56db\u4e2a\u53c2\u6570\u662f\u5f00\u59cb\u7ed8\u5236\u7684\u4f4d\u7f6e\uff0c\u7b2c\u4e94\u4e2a\u53c2\u6570draw.Src\u8868\u793a\u7ed8\u5236\u6a21\u5f0f\uff0c\u6709\u591a\u4e2a\u53c2\u6570\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Over: \u9ed8\u8ba4\u64cd\u4f5c,\u76f4\u63a5\u5c06\u6e90\u56fe\u50cf\u7ed8\u5236\u5230\u76ee\u6807\u56fe\u50cf<\/li>\n\n\n\n<li>Src: \u53ea\u7ed8\u5236\u6e90\u56fe\u50cf\u7684\u989c\u8272,\u5ffd\u7565\u76ee\u6807\u56fe\u50cf\u7684\u989c\u8272\u00a0<\/li>\n\n\n\n<li>Dst: \u53ea\u4fdd\u7559\u76ee\u6807\u56fe\u50cf\u7684\u989c\u8272,\u5ffd\u7565\u6e90\u56fe\u50cf\u7684\u989c\u8272<\/li>\n\n\n\n<li>Clear: \u7528\u900f\u660e\u8272\u586b\u5145\u76ee\u6807\u56fe\u50cf<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">5\u3001image\u5e93\u7684RGBA\u7ed3\u6784\u4f53\u7684<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">Bounds()<\/mark><\/strong>\u65b9\u6cd5\u7528\u4e8e\u8fd4\u56de\u8be5\u56fe\u50cf\u5bf9\u8c61\u7684\u8fb9\u754c\u6846\u3002\u5177\u4f53\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func (p *RGBA) Bounds() Rectangle<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Bounds()<\/code> \u65b9\u6cd5\u8fd4\u56de\u7684\u662f\u4e00\u4e2a <code>image.Rectangle<\/code> \u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u8868\u793a\u8be5\u56fe\u50cf\u5bf9\u8c61\u7684\u8fb9\u754c\u6846\u3002\u8fd9\u4e2a\u8fb9\u754c\u6846\u7684\u5de6\u4e0a\u89d2\u5750\u6807\u4e3a (0, 0)\uff0c\u53f3\u4e0b\u89d2\u5750\u6807\u4e3a (w, h)\uff0c\u5176\u4e2d w \u548c h \u5206\u522b\u4e3a\u56fe\u50cf\u7684\u5bbd\u5ea6\u548c\u9ad8\u5ea6\uff08\u4ee5\u50cf\u7d20\u4e3a\u5355\u4f4d\uff09\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a <code>img<\/code> \u7684 <code>image.RGBA<\/code> \u5bf9\u8c61\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u83b7\u53d6\u5176\u8fb9\u754c\u6846\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bounds := img.Bounds()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a <code>image.Rectangle<\/code> \u7c7b\u578b\u7684\u5bf9\u8c61 <code>bounds<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u5176 <code>Min<\/code> \u548c <code>Max<\/code> \u65b9\u6cd5\u83b7\u53d6\u5176\u5de6\u4e0a\u89d2\u548c\u53f3\u4e0b\u89d2\u7684\u5750\u6807\u3002\u4f8b\u5982\uff0c\u8981\u83b7\u53d6\u56fe\u50cf\u7684\u5bbd\u5ea6\u548c\u9ad8\u5ea6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>w, h := bounds.Max.X, bounds.Max.Y<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">6\u3001image\u5e93\u7684<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">NewUniform()<\/mark><\/strong>\u51fd\u6570\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u5355\u4e00\u989c\u8272\u7684\u56fe\u50cf\u5bf9\u8c61\u3002\u5b83\u63a5\u53d7\u4e00\u4e2a <code>color.Color<\/code> \u7c7b\u578b\u7684\u53c2\u6570\u4f5c\u4e3a\u8f93\u5165\uff0c\u8fd4\u56de\u4e00\u4e2a\u5b9e\u73b0\u4e86 <code>image.Image<\/code> \u63a5\u53e3\u7684\u65b0\u56fe\u50cf\u5bf9\u8c61\uff0c\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import \"image\"\nimport \"image\/color\"\n\n\/\/ \u521b\u5efa\u4e00\u4e2a 100x100 \u7684\u7ea2\u8272\u56fe\u50cf\nc := color.RGBA{255, 0, 0, 255}\nimg := image.NewUniform(c)\n#\u6ce8\u610f\uff1aimage.Rectangle{image.Point{0, 0}, image.Point{100, 100}}\u7b49\u4ef7\u4e8eimage.Rect(0,0,100,100)\ndst := image.NewRGBA(image.Rectangle{image.Point{0, 0}, image.Point{100, 100}})\ndraw.Draw(dst, dst.Bounds(), img, image.Point{}, draw.Src)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u8ff0\u4ee3\u7801\u5c06\u521b\u5efa\u4e00\u4e2a\u5927\u5c0f\u4e3a 1&#215;1\uff0c\u989c\u8272\u4e3a {255, 0, 0, 255}\uff08\u7ea2\u8272\uff09\u7684 <code>color.RGBA<\/code> \u5bf9\u8c61\uff0c\u5e76\u5c06\u5176\u4f20\u9012\u7ed9 <code>image.NewUniform()<\/code> \u51fd\u6570\uff0c\u4ee5\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u8be5\u989c\u8272\u7684\u65b0\u56fe\u50cf\u5bf9\u8c61\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86 <code>draw.Draw()<\/code> \u51fd\u6570\u5c06\u8be5\u56fe\u50cf\u7ed8\u5236\u5230\u53e6\u4e00\u4e2a\u56fe\u50cf\u4e0a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">7\u3001image\u5e93\u7684<strong> <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">Point<\/mark><\/strong> \u662f\u4e00\u4e2a\u8868\u793a\u4e8c\u7ef4\u5e73\u9762\u4e0a\u7684\u70b9\u7684\u7ed3\u6784\u4f53\u3002\u5b83\u7531\u4e24\u4e2a\u6574\u6570\u7c7b\u578b\u7684 X \u548c Y \u5750\u6807\u7ec4\u6210\uff0c\u5206\u522b\u8868\u793a\u70b9\u5728\u6c34\u5e73\u548c\u5782\u76f4\u65b9\u5411\u7684\u4f4d\u7f6e\u3002\u5177\u4f53\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type Point struct {\n    X, Y int\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>image.Point<\/code> \u53ef\u4ee5\u7528\u4e8e\u8868\u793a\u56fe\u50cf\u7684\u50cf\u7d20\u5750\u6807\u3001\u77e9\u5f62\u7684\u5de6\u4e0a\u89d2\u548c\u53f3\u4e0b\u89d2\u5750\u6807\u7b49\u4f4d\u7f6e\u4fe1\u606f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>image.Point<\/code> \u6765\u6307\u5b9a\u4e00\u4e2a\u77e9\u5f62\u7684\u5de6\u4e0a\u89d2\u548c\u53f3\u4e0b\u89d2\u5750\u6807\uff0c\u7136\u540e\u4f7f\u7528\u8be5\u77e9\u5f62\u4f5c\u4e3a\u53c2\u6570\u8c03\u7528 <code>image.NewRGBA()<\/code> \u51fd\u6570\u521b\u5efa\u4e00\u4e2a RGBA \u56fe\u50cf\u5bf9\u8c61\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import \"image\"\n\n\/\/ \u521b\u5efa\u4e00\u4e2a 100x100 \u7684 RGBA \u56fe\u50cf\nrect := image.Rectangle{image.Point{0, 0}, image.Point{100, 100}}\nimg := image.NewRGBA(rect)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u8ff0\u4ee3\u7801\u5c06\u521b\u5efa\u4e00\u4e2a\u5927\u5c0f\u4e3a 100&#215;100 \u7684 RGBA \u56fe\u50cf\u5bf9\u8c61\uff0c\u5e76\u4e14\u5de6\u4e0a\u89d2\u5750\u6807\u4e3a (0,0)\uff0c\u53f3\u4e0b\u89d2\u5750\u6807\u4e3a (100,100)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4e8c\u3001\u5b9e\u6218\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1\u3001\u4e0b\u9762\u4f8b\u5b50\u8868\u793a\u521b\u5efa\u4e00\u4e2a\u80cc\u666f\u4e3a\u767d\u8272\u7684\u77e9\u5f62\u56fe\u50cf\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"572\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/04\/11-2.png\" alt=\"\" class=\"wp-image-13080\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/04\/11-2.png 708w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/04\/11-2-300x242.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/04\/11-2-600x485.png 600w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>13\u884c\uff1a\u9996\u5148\u901a\u8fc7image.Rect()\u51fd\u6570\u521b\u5efa\u957f300\u9ad8300\u7684\u77e9\u5f62\u5bf9\u8c61\uff0c\u5e76\u901a\u8fc7NewRGBA\u521b\u5efa\u56fe\u50cf\u5bf9\u8c61<\/li>\n\n\n\n<li>14\u884c\uff1a\u521b\u5efa\u4e00\u4e2a\u7a7a\u56fe\u7247\uff0c\u8fd4\u56de\u4e00\u4e2aFile\u6307\u9488\u548cerror<\/li>\n\n\n\n<li>18\u884c\uff1a\u901a\u8fc7bufio.NewWriter()\u521d\u59cb\u5316\u4e00\u4e2aWriter\u7ed3\u6784\u4f53\u5bf9\u8c61\uff0c\u8fd4\u56debufio.Writer\u7ed3\u6784\u4f53\u6307\u9488<\/li>\n\n\n\n<li>19\u884c\uff1a\u901a\u8fc7png.Encode()\u5c06\u56fe\u50cf\u5bf9\u8c61newimage\u7f16\u7801\u4e3apng\u683c\u5f0f\u6570\u636e\uff0c\u5982\u679c\u8bfb\u53d6\u6587\u4ef6\u5c31\u8981\u89e3\u7801(Decode)<\/li>\n\n\n\n<li>23\u884c\uff1a\u8c03\u7528bufio\u7684Writer\u7ed3\u6784\u4f53\u7684Flush()\u5c06\u6570\u636e\u5199\u5165\u78c1\u76d8\u4e2d<\/li>\n\n\n\n<li>27\u884c\uff1a\u901a\u8fc7os\u6253\u5f00\u6216\u8005\u64cd\u4f5c\u7684\u6587\u4ef6\u9700\u8981\u5173\u95ed<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u4e4b\u540e\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\u751f\u6210test.png\u56fe\u7247\uff0c\u6253\u5f00\u56fe\u7247\u53ef\u4ee5\u770b\u5230\u80cc\u666f\u4e3a\u900f\u660e\uff0c\u50cf\u7d20\u4e3a300*300\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"706\" height=\"374\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/04\/12-2.png\" alt=\"\" class=\"wp-image-13083\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/04\/12-2.png 706w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/04\/12-2-300x159.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/04\/12-2-600x318.png 600w\" sizes=\"auto, (max-width: 706px) 100vw, 706px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">2\u3001\u4e0b\u9762\u4f8b\u5b50\u8868\u793a\u7ed8\u5236\u4e00\u4e2a\u4e24\u4e2a\u5706\u5f62\uff0c\u5e76\u8bbe\u7f6e\u4e24\u4e2a\u5706\u4e4b\u95f4\u8fb9\u6846\u989c\u8272\u4e3a\u7ea2\u8272\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n\t\"fmt\"\n\t\"image\"\n\t\"image\/color\"\n\t\"image\/draw\"\n\t\"image\/png\"\n\t\"math\"\n\t\"os\"\n)\n\nfunc main() {\n\t\/\/backgroundColor := color.RGBA{0, 0, 0, 0}        \/\/\u900f\u660e\n\timg := image.NewRGBA(image.Rect(0, 0, 600, 600)) \/\/\u5b9a\u4e49\u56fe\u50cf\u5bf9\u8c61\n\t\/\/\u5728\u56fe\u50cf\u5bf9\u8c61img\u4e0a\u7ed8\u5236\u65b0\u900f\u660e\u8272\n\t\/\/draw.Draw(img, img.Bounds(), image.NewUniform(backgroundColor), image.Point{}, draw.Src)\n\t\/\/\u5706\u7684\u4e2d\u5fc3\u5750\u6807\uff0c\u56e0\u4e3a\u6211\u4eec\u5e0c\u671b\u753b\u7684\u5706\u5f62\u53ef\u4ee5\u4f4d\u4e8e\u56fe\u50cf\u5bf9\u8c61\u7684\u4e2d\u5fc3\uff0c\u56e0\u4e3a\u8bbe\u7f6e\u4e3a300\uff0c\u53ef\u6839\u636e\u9700\u8981\u81ea\u5b9a\u4e49\n\tcenterX := 300\n\tcenterY := 300\n\t\/\/\u8bbe\u7f6e\u5916\u5c42\u5706\u7684\u534a\u5f84\uff0c\u5982\u679c\u5e0c\u671b\u5916\u5c42\u5706\u4e0e\u56fe\u50cf\u5bf9\u8c61\u76f8\u63a5\uff0c\u53ef\u8bbe\u7f6e\u534a\u5f84\u4e3a300\n\tradius := 300\n\t\/\/\u8bbe\u7f6e\u5185\u5c42\u5706\u4e0e\u5916\u5c42\u5706\u4e4b\u95f4\u7684\u8fb9\u6846\u5bbd\u5ea6\n\tborderWidth := 10\n\t\/\/\u5b9a\u4e49\u5916\u5c42\u5706\u7684\u989c\u8272\uff0c\u672c\u4f8b\u5b50\u8bbe\u7f6e\u4e3a\u7ea2\u8272\n\tborderColor := color.RGBA{255, 0, 0, 255}\n\t\/\/\u5b9a\u4e49\u5916\u5c42\u5706   2*math.Pi\u76f8\u5f53\u4e8e\u5706\u7684\u4e00\u5468\uff0c\u904d\u5386\u5706\u7684\u4e00\u5468\u4e0a\u7684\u70b9\u4e0e\u6c34\u5e73\u65b9\u5411\u7684\u89d2\u5ea6\n\tfor angle := 0.0; angle &lt; 2*math.Pi; angle += 0.001 {\n\t\tx := int(float64(radius)*math.Cos(angle)) + centerX\n\t\ty := int(float64(radius)*math.Sin(angle)) + centerY\n\t\timg.Set(x, y, borderColor)\n\t}\n\t\/\/\u5b9a\u4e49\u5185\u5c42\u5706\n\tfor angle := 0.0; angle &lt; 2*math.Pi; angle += 0.001 {\n\t\tx := int(float64(radius-borderWidth)*math.Cos(angle)) + centerX\n\t\ty := int(float64(radius-borderWidth)*math.Sin(angle)) + centerY\n\t\timg.Set(x, y, borderColor)\n\t}\n\t\/\/\u586b\u5145\u5185\u5706\u4e0e\u5916\u4fa7\u5706\u4e4b\u95f4\u7684\u533a\u57df\u4e3a\u7ea2\u8272\n\tfor angle := 0.0; angle &lt; 2*math.Pi; angle += 0.001 {\n\t\t\/\/\u53f3\u4e0a\u56db\u5206\u4e4b\u4e00\u5706\n\t\tfor x := int(float64(radius-borderWidth)*math.Cos(angle)) + centerX; x &lt;= int(float64(radius)*math.Cos(angle))+centerX; x++ {\n\t\t\tfor y := int(float64(radius)*math.Sin(angle)) + centerY; y &lt;= int(float64(radius-borderWidth)*math.Sin(angle))+centerY; y++ {\n\t\t\t\timg.Set(x, y, borderColor)\n\t\t\t}\n\t\t}\n\t\t\/\/\u5de6\u4e0a\u56db\u5206\u4e4b\u4e00\u5706\n\t\tfor x := int(float64(radius)*math.Cos(angle)) + centerX; x &lt;= int(float64(radius-borderWidth)*math.Cos(angle))+centerX; x++ {\n\t\t\tfor y := int(float64(radius)*math.Sin(angle)) + centerY; y &lt;= int(float64(radius-borderWidth)*math.Sin(angle))+centerY; y++ {\n\t\t\t\timg.Set(x, y, borderColor)\n\t\t\t}\n\t\t}\n\t\t\/\/\u5de6\u4e0b\u56db\u5206\u4e4b\u4e00\u5706\n\t\tfor x := int(float64(radius)*math.Cos(angle)) + centerX; x &lt;= int(float64(radius-borderWidth)*math.Cos(angle))+centerX; x++ {\n\t\t\tfor y := int(float64(radius-borderWidth)*math.Sin(angle)) + centerY; y &lt;= int(float64(radius)*math.Sin(angle))+centerY; y++ {\n\t\t\t\timg.Set(x, y, borderColor)\n\t\t\t}\n\t\t}\n\t\t\/\/\u53f3\u4e0b\u56db\u5206\u4e4b\u4e00\u5706\n\t\tfor x := int(float64(radius-borderWidth)*math.Cos(angle)) + centerX; x &lt;= int(float64(radius)*math.Cos(angle))+centerX; x++ {\n\t\t\tfor y := int(float64(radius-borderWidth)*math.Sin(angle)) + centerY; y &lt;= int(float64(radius)*math.Sin(angle))+centerY; y++ {\n\t\t\t\timg.Set(x, y, borderColor)\n\t\t\t}\n\t\t}\n\t}\n\t\/\/ \u5c06\u56fe\u7247\u8f93\u51fa\u5230\u6587\u4ef6\n\toutputFile, err := os.Create(\"circle.png\")\n\tif err != nil {\n\t\tfmt.Println(err)\n\t\tos.Exit(1)\n\t}\n\tdefer outputFile.Close()\n\tpng.Encode(outputFile, img)\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bf4\u660e\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u7684\u5916\u5c42\u5706\u7684 for \u5faa\u73af\uff0c\u904d\u5386\u4ece 0 \u5230 2\u03c0\uff08\u5373\u4e00\u5468\uff09\u4e4b\u95f4\u7684\u5f27\u5ea6\u503c\uff0c\u5e76\u4e14\u6bcf\u6b21\u9012\u589e0.001\uff0c\u5177\u4f53\u6765\u8bf4\uff0c<code>radius<\/code> \u8868\u793a\u70b9\u5230\u5706\u5fc3\u7684\u8ddd\u79bb\uff0c\u4e5f\u5c31\u662f\u6211\u4eec\u8bbe\u7f6e\u7684\u5916\u5c42\u5706\u7684\u534a\u5f84\uff0c<code>angle<\/code> \u8868\u793a\u5706\u4e0a\u7684\u70b9\u4e0e\u5706\u5fc3\u8fde\u7ebf\u540e\u4e0eX\u8f74\u7684\u5939\u89d2(\u5f27\u5ea6)\uff0c<code>centerX<\/code> \u8868\u793a\u5706\u5fc3\u5728\u6c34\u5e73\u65b9\u5411\u4e0a\u7684\u5750\u6807\u3002\u8fd9\u6bb5\u4ee3\u7801\u5148\u5c06 <code>radius<\/code> \u8f6c\u6362\u6210\u6d6e\u70b9\u6570\u7c7b\u578b\u5e76\u4e58\u4ee5 <code>math.Cos(angle)<\/code> \u8ba1\u7b97\u51fa\u70b9\u5728\u6c34\u5e73\u65b9\u5411\u4e0a\u7684\u8ddd\u79bb\uff08\u5373 x \u5750\u6807\uff09\uff0c\u6b64\u5750\u6807\u662f\u76f8\u5bf9\u4e8e\u5706\u5fc3\u7684\u5750\u6807\uff0c\u56e0\u6b64\u9700\u8981\u52a0\u4e0a <code>centerX<\/code>\u624d\u80fd\u5f97\u5230\u70b9\u5728\u6c34\u5e73\u65b9\u5411\u4e0a\u7684\u5b9e\u9645\u4f4d\u7f6e\u5750\u6807\uff0cy\u5750\u6807\u4e00\u6837\uff0c\u6700\u540e\u901a\u8fc7\u8c03\u7528 <code>img.Set(x, y, borderColor)<\/code>\uff0c\u5c06\u8be5\u70b9\u7684\u989c\u8272\u8bbe\u7f6e\u4e3a <code>borderColor<\/code>\uff0c\u672c\u4f8b\u5b50\u4e2d\u4e3a\u7ea2\u8272\uff0c\u56e0\u6b64\u5c31\u753b\u51fa\u4e86\u4e00\u4e2a\u7ea2\u8272\u7684\u5706\u5f62<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5185\u5c42\u5706\u7684\u539f\u7406\u4e00\u6837\u7684\uff0c\u9700\u8981\u5148\u7528\u5916\u5c42\u5706\u7684\u534a\u5f84\u51cf\u53bb\u5185\u5916\u5c42\u5706\u4e4b\u95f4\u7684\u8fb9\u6846\u8ddd\u79bb\uff0c\u7136\u540e\u753b\u5706\u5373\u53ef\uff0c\u753b\u597d\u7684\u56fe\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"706\" height=\"610\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/04\/18-4.png\" alt=\"\" class=\"wp-image-13115\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/04\/18-4.png 706w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/04\/18-4-300x259.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/04\/18-4-600x518.png 600w\" sizes=\"auto, (max-width: 706px) 100vw, 706px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u518d\u6765\u8bf4\u4e00\u4e0b\u5982\u4f55\u7ed9\u5185\u5706\u548c\u5916\u5706\u4e4b\u95f4\u7684\u8fb9\u6846\u533a\u57df\u6dfb\u52a0\u989c\u8272\u4e3a\u7ea2\u8272<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u539f\u7406\uff1a\u8fd8\u662f\u901a\u8fc7for\u5faa\u73af\u904d\u5386\u5185\u5916\u5706\u7684\u5404\u4e2a\u70b9\u4e0a\u7684\u5f27\u5ea6\uff0c\u6839\u636e\u5f27\u5ea6\u503c\u8ba1\u7b97\u51fa\u6bcf\u4e2a\u70b9\u7684X,Y\u5750\u6807\uff0c\u7136\u540e\u901a\u8fc7image\u5e93\u7684RGBA\u7ed3\u6784\u4f53\u7684Set()\u65b9\u6cd5\u8bbe\u7f6e\u5750\u6807\u533a\u95f4\u7684\u989c\u8272<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9996\u5148\u6211\u4eec\u770b\u4e0a\u9762\u4ee3\u7801\u7684\u7b2c\u4e09\u4e2afor\u5faa\u73af\uff0c\u8fd8\u662f\u904d\u5386\u4e00\u4e2a\u5706\u5468(2*math.Pi)\u4e0a\u7684\u70b9\uff0c\u83b7\u53d6\u8fd9\u4e2a\u70b9\u7684\u5f27\u5ea6\u5236\uff0c\u7136\u540e\u901a\u8fc7math.Cos()\u548cmath.Sin()\u6765\u83b7\u53d6\u6bcf\u4e2a\u70b9\u5bf9\u5e94\u7684\u5b9e\u9645\u5750\u6807\uff0c\u5728\u8bbe\u7f6e\u989c\u8272\u7684\u65f6\uff0c\u5176\u5b9e\u662f\u901a\u8fc7(600,300)\u8fd9\u4e2a\u70b9\u7136\u540e\u987a\u65f6\u9488\u753b\u7684\uff0c\u4e5f\u5c31\u662f\u5148\u753b\u7684\u53f3\u4e0b\u56db\u5206\u4e4b\u4e00\u5706\uff0c\u7136\u540e\u5de6\u4e0b\u56db\u5206\u4e4b\u4e00\u5706\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u56e0\u6b64\u5728for\u5faa\u73af\u4e2d\u53c8\u5305\u542b\u4e86\u56db\u4e2afor\u5faa\u73af\uff0c\u8bf4\u660e\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><strong>\u53f3\u4e0b\u56db\u5206\u4e4b\u4e00\u5706\uff1a<\/strong><\/mark>\u4ece\u904d\u5386\u540ex,y\u7684\u5750\u6807\u6765\u770b\uff0c\u53f3\u4e0b\u56db\u5206\u4e4b\u4e00\u5706\u7684\u5916\u5c42\u5706\u8d77\u59cb\u5750\u6807\u4e3a(600,300)\uff0c\u5185\u5c42\u5706\u7684\u8d77\u59cb\u5750\u6807\u4e3a(590,300)\uff0c\u5916\u5c42\u5706\u7ec8\u6b62\u5750\u6807(300,600)\uff0c\u5185\u5c42\u5706\u7ec8\u6b62\u5750\u6807\u4e3a(300,590)\uff0c\u5e76\u4e14\u6574\u4e2a\u5916\u5c42\u5706\u7684x\u5750\u6807\u59cb\u7ec8\u5927\u4e8e\u7b49\u4e8e\u5185\u5c42\u5706\u7684x\u5750\u6807\uff0c\u5916\u5c42\u5706\u7684y\u5750\u6807\u59cb\u7ec8\u5927\u4e8e\u5185\u5c42\u5706\u7684y\u5750\u6807\uff0c\u56e0\u6b64\u901a\u8fc7for\u5faa\u73af\u4ece\u5168\u90e8\u7684\u7ed3\u679c\u4e2d\u904d\u5386\u7b26\u5408\u7684\u5750\u6807\u4e3a:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for x := int(float64(radius-borderWidth)*math.Cos(angle)) + centerX; x &lt;= int(float64(radius)*math.Cos(angle))+centerX; x++ {\n  for y := int(float64(radius-borderWidth)*math.Sin(angle)) + centerY; y &lt;= int(float64(radius)*math.Sin(angle))+centerY; y++ {\n\t\timg.Set(x, y, borderColor)\n  }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u5de6\u4e0b\u56db\u5206\u4e4b\u4e00\u5706\uff1a<\/mark><\/strong>\u5de6\u4e0b\u56db\u5206\u4e4b\u4e00\u5706\u7684\u5916\u5c42\u5706\u8d77\u59cb\u5750\u6807\u4e3a(300,600)\uff0c\u5185\u5c42\u5706\u4e3a(300,590)\uff0c\u5916\u5c42\u5706\u7ec8\u6b62\u5750\u6807\u4e3a(0,300)\uff0c\u5185\u5c42\u5706\u7ec8\u6b62\u5750\u6807\u4e3a(10,300)\uff0c\u56e0\u4e3a\u8fb9\u6846\u539a\u5ea6\u4e3a10\uff0c\u56e0\u6b64\u5185\u5c42\u5706\u7684X\u5750\u6807\u4e3a10\uff0c\u4ece\u904d\u5386\u7684\u5750\u6807\u7ed3\u679c\u770b\uff0c\u5916\u5c42\u5706\u7684x\u5750\u6807\u59cb\u7ec8\u5c0f\u4e8e\u7b49\u4e8e\u5185\u5c42\u5706\u7684\u5750\u6807\uff0c\u5916\u5c42\u5706\u7684y\u5750\u6807\u4f7f\u7528\u5927\u4e8e\u7b49\u4e8e\u5185\u5c42\u5706\u7684Y\u5750\u6807\uff0c\u56e0\u6b64\u901a\u8fc7for\u5faa\u73af\u4ece\u5168\u90e8\u7684\u7ed3\u679c\u4e2d\u904d\u5386\u7b26\u5408\u7684\u5750\u6807\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for x := int(float64(radius)*math.Cos(angle)) + centerX; x &lt;= int(float64(radius-borderWidth)*math.Cos(angle))+centerX; x++ {\n\tfor y := int(float64(radius-borderWidth)*math.Sin(angle)) + centerY; y &lt;= int(float64(radius)*math.Sin(angle))+centerY; y++ {\n\timg.Set(x, y, borderColor)\n   }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u5de6\u4e0a\u56db\u5206\u4e4b\u4e00\u5706\uff1a<\/mark><\/strong>\u5de6\u4e0a\u56db\u5206\u4e4b\u4e00\u5706\u7684\u5916\u5c42\u5706\u8d77\u59cb\u5750\u6807\u4e3a(0,300)\uff0c\u5185\u5c42\u5706\u4e3a(300,10)\uff0c\u5916\u5c42\u5706\u7ec8\u6b62\u5750\u6807\u4e3a(300,0)\uff0c\u5185\u5c42\u5706\u4e3a(300,10)\uff0c\u4ece\u904d\u5386\u7684\u5750\u6807\u7ed3\u679c\u770b\uff0c\u5916\u5c42\u5706\u7684x\u5750\u6807\u59cb\u7ec8\u5c0f\u4e8e\u7b49\u4e8e\u5185\u5c42\u5706\u7684x\u5750\u6807\uff0c\u5916\u5c42\u5706\u7684y\u5750\u6807\u59cb\u7ec8\u5c0f\u4e8e\u7b49\u4e8e\u5185\u5c42\u5706\u7684y\u5750\u6807\uff0c\u56e0\u6b64\u901a\u8fc7for\u5faa\u73af\u4ece\u5168\u90e8\u7684\u7ed3\u679c\u4e2d\u904d\u5386\u7b26\u5408\u7684\u5750\u6807\u4e3a:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for x := int(float64(radius)*math.Cos(angle)) + centerX; x &lt;= int(float64(radius-borderWidth)*math.Cos(angle))+centerX; x++ {\n\tfor y := int(float64(radius)*math.Sin(angle)) + centerY; y &lt;= int(float64(radius-borderWidth)*math.Sin(angle))+centerY; y++ {\n\timg.Set(x, y, borderColor)\n   }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u53f3\u4e0a\u56db\u5206\u4e4b\u4e00\u5706\uff1a<\/mark><\/strong>\u53f3\u4e0a\u56db\u5206\u4e4b\u4e00\u5706\u7684\u5916\u5c42\u5706\u8d77\u59cb\u5750\u6807\u4e3a(300,0)\uff0c\u5185\u5c42\u5706\u7684\u8d77\u59cb\u5750\u6807\u4e3a(300,10)\uff0c\u5916\u5c42\u5706\u7684\u7ec8\u6b62\u5750\u6807\u4e3a(600,300)\uff0c\u5185\u5c42\u5706\u7684\u7ec8\u6b62\u5750\u6807\u4e3a(590,300)\uff0c\u4ece\u904d\u5386\u7684x,y\u5750\u6807\u7ed3\u679c\u770b\uff0c\u5916\u5c42\u5706\u7684x\u5750\u6807\u59cb\u7ec8\u5927\u4e8e\u7b49\u4e8e\u5185\u5c42\u5706\u7684x\u5750\u6807\uff0c\u5916\u5c42\u5706\u7684Y\u5750\u6807\u59cb\u7ec8\u5c0f\u4e8e\u7b49\u4e8e\u5185\u5c42\u5706\u7684Y\u5750\u6807\uff0c\u56e0\u6b64\u901a\u8fc7for\u5faa\u73af\u4ece\u5168\u90e8\u7684\u7ed3\u679c\u4e2d\u904d\u5386\u7b26\u5408\u7684\u5750\u6807\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for x := int(float64(radius-borderWidth)*math.Cos(angle)) + centerX; x &lt;= int(float64(radius)*math.Cos(angle))+centerX; x++ {\n\tfor y := int(float64(radius)*math.Sin(angle)) + centerY; y &lt;= int(float64(radius-borderWidth)*math.Sin(angle))+centerY; y++ {\n\timg.Set(x, y, borderColor)\n\t}\n}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"707\" height=\"566\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/04\/2-5.png\" alt=\"\" class=\"wp-image-13121\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/04\/2-5.png 707w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/04\/2-5-300x240.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/04\/2-5-600x480.png 600w\" sizes=\"auto, (max-width: 707px) 100vw, 707px\" \/><\/figure>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u6ce8\u610f\uff1a\u5982\u679c\u53ea\u662f\u753b\u4e00\u4e2a\u5185\u5916\u5706\u5e76\u8bbe\u7f6e\u8fb9\u6846\u5bbd\u5ea6\u4e3a\u7ea2\u8272\uff0c\u7b2c1\u30012\u4e2a\u7684for\u5faa\u73af2*math.Pi\u5176\u5b9e\u53ef\u4ee5\u4e0d\u5199\u7684,\u4e5f\u53ef\u4ee5\u8fbe\u5230\u6548\u679c\uff0c\u4f46\u662f\u5982\u679c\u53ea\u662f\u753b\u4e24\u4e2a\u7a7a\u5fc3\u5706\uff0c\u90a3\u4e48\u7b2c\u4e00\u4e2a\u7b2c\u4e8c\u4e2a\u5c31\u5fc5\u987b\u8981\u5199<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5176\u4ed6\uff1a\u4e0a\u9762\u6211\u4eec\u8ba1\u7b97\u70b9\u7684\u5750\u6807\u662f\u6839\u636e\u7684\u5f27\u5ea6\u503c\uff0c\u901a\u8fc7for\u5faa\u73af\u904d\u53862*math.Pi\u83b7\u53d6\u7684\u76f4\u63a5\u5c31\u662f\u5f27\u5ea6\u503c\uff0c\u5982\u679c\u7ed9\u5b9a\u89d2\u5ea6\uff0c\u6765\u83b7\u53d6\u5750\u6807\u503c\uff0c\u53ef\u4ee5\u5148\u5c06\u89d2\u5ea6\u503c\u8f6c\u6362\u4e3a\u5f27\u5ea6\u503c\uff0c\u7136\u540e\u5728\u8ba1\u7b97\u5750\u6807\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>angle := 45                     \/\/\u7ed9\u5b9a\u89d2\u5ea6\nradian := angle *math.Pi \/180   \/\/\u6839\u636e\u89d2\u5ea6\u6c42\u5f27\u5ea6\u503c\nres := int(float64(radius)*math.Cos(radian)) + centerX  \/\/\u6839\u636e\u5f27\u5ea6\u83b7\u53d6X\u5750\u6807\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>golang\u7684image\u5e93\u63d0\u4f9b\u4e86\u56fe\u50cf\u5904\u7406\u57fa\u672c\u529f\u80fd \u4e00\u3001\u8bed\u6cd5\uff1a 1\u3001image\u5e93\u7684Rect()\u51fd\u6570\u53ef\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":13070,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[111],"tags":[],"class_list":["post-13069","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":8}},"_links":{"self":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/13069","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/comments?post=13069"}],"version-history":[{"count":65,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/13069\/revisions"}],"predecessor-version":[{"id":13184,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/13069\/revisions\/13184"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media\/13070"}],"wp:attachment":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media?parent=13069"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/categories?post=13069"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/tags?post=13069"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}