• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

在图像上绘制对角线

用户头像
it1352
帮助1

问题说明

您好,我试图在图像的右上角到左下角绘制对角线是我到目前为止的代码.

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)

谢谢

正确答案

#1

大多数图形库都有某种直接绘制线的方法.

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
系列文章
更多 icon
同类精品
更多 icon
继续加载