擅长:python、mysql、java
<p>大多数图形库都有一些直接绘制线的方法。</p>
<p>在<a href="http://www.cs.uic.edu/~troy/fall04/cs100/7_Picture_Functions.html" rel="nofollow noreferrer">JES</a>中有<code>addLine</code>函数,因此您可以</p>
<pre><code>addLine(picture, 0, 0, width, height)
</code></pre>
<p>如果你一直在设置单个像素,你应该看看<a href="http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm" rel="nofollow noreferrer">Bresenham Line Algorithm</a>,这是最有效的画线算法之一。</p>
<p>代码注释:使用两个嵌套循环执行的操作如下</p>
<pre><code>for each column in the picture
for each row in the current column
set the pixel in the current column and current row to black
</code></pre>
<p>所以基本上你用黑色像素填充整个图像。</p>
<p><strong>编辑</strong></p>
<p>要在整个图像上绘制多条对角线(在它们之间留有空间),可以使用以下循环</p>
<pre><code>width = getWidth(picture)
height = getHeight(picture)
space = 10
for x in range(0, 2*width, space):
addLine(picture, x, 0, x-width, height)
</code></pre>
<p>这给了你一个像(例子是手绘的…)</p>
<p><img src="https://i.stack.imgur.com/5O944.png" alt="diagonal lines"/></p>
<p>这利用了大多数图形库提供的<em>剪裁</em>功能,也就是说,不在图像中的线条部分被忽略。请注意,如果没有<code>2*width</code>(即,如果<code>x</code>只上升到<code>with</code>),则只会绘制左上半行。。。</p>