在图像上绘制对角线 [英] Drawing diagonal lines on an image

查看:135
本文介绍了在图像上绘制对角线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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...

这篇关于在图像上绘制对角线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆