在图像上绘制对角线
问题说明
您好,我试图在图像的右上角到左下角绘制对角线是我到目前为止的代码.
Hi im trying to draw diagonal lines across an image top right to bottom left here is my code so far.
width = getWidth(picture)
height = getHeight(picture)
for x in range(0, width):
for y in range(0, height):
pixel = getPixel(picture, x, y)
setColor(pixel, black)
谢谢
正确答案
大多数图形库都有某种直接绘制线的方法.
Most graphic libraries have some way to draw a line directly.
在 JES 中,有addLine
功能,所以你可以做
In JES there is the addLine
function, so you could do
addLine(picture, 0, 0, width, height)
如果您坚持设置单个像素,则应查看 Bresenham线算法,这是绘制线条最有效的算法之一.
If you're stuck with setting single pixels, you should have a look at Bresenham Line Algorithm, which is one of the most efficient algorithms to draw lines.
代码注释:以下是两个嵌套循环的作用
A note to your code: What you're doing with two nested loops is the following
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
所以基本上您可以用黑色像素填充整个图像.
so basically youre filling the entire image with black pixels.
编辑
要在整个图像上绘制多条对角线(在它们之间留一个空格),可以使用以下循环
To draw multiple diagonal lines across the whole image (leaving a space between them), you could use the following loop
width = getWidth(picture)
height = getHeight(picture)
space = 10
for x in range(0, 2*width, space):
addLine(picture, x, 0, x-width, height)
这为您提供了类似的图像(示例是手绘的...)
This gives you an image like (the example is hand-drawn ...)
这利用了大多数图形库提供的 clipping 功能,即,将忽略不在图像中的行部分.请注意,如果没有2*width
(即如果x
仅上升到with
),则只会绘制线的左上半部分...
This makes use of the clipping functionality, most graphics libraries provide, i.e. parts of the line that are not within the image are simply ignored. Note that without 2*width
(i.e. if x
goes only up to with
), only the upper left half of the lines would be drawn...
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcfkeih
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20