2013-03-23 62 views
1

嗨即时尝试绘制对角线在图像右上角到左下角这里是我的代码到目前为止。在图像上绘制对角线

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

你应该提到你正在使用的图形库... – MartinStettner

+0

即时为学生使用jython环境 – user2194374

回答

1

哪里你picture对象来自哪里?它是什么?到目前为止还没有工作?你试图使用什么样的图像访问? (我的意思是,你从哪里得到,或打算获得“getWidth,getHeight,getPixel,setColor)从?

我认为没有图书馆,给你一个”像素“作为一个整体对象,可以在setColor呼叫是存在的,如果是这样,它将是世界上最慢的东西 - 也许在银河系中。

另一方面,如果这些方法确实存在,并且你的图片,上面的代码将覆盖所有的图像黑色 - 您将在图像的所有可能的x值(从0到宽度)内获得所有可能的“y”值(从0到高度),并着色每个黑色

绘制一条线需要您更改x和y同时,更像是:

(使用另一种“虚库”,而是一个更合理:

for x, y in zip(range(0, width), range(0, height)): 
    picture.setPixel((x,y), Black)) 

这将排序工作,但线不会是完美的,除非形象是完美的正方形 - 否则它会跳过像素图像最宽的方向。为了解决这个问题,需要一个更精确的算法 - 但这是第二个你有一个真正的方法来访问图像上的像素 - 就像使用Python的成像库(PIL或Pillow)或pygame或其他库。

+0

即时通讯使用jython环境为学生创建图片较早,是的,它是非常缓慢的获得所有的像素。是的,我认为它也是全黑的,但是它从左到右在左侧形成了一条粗黑线。 – user2194374

+1

事实上,JES(Jython Environmen for Students)确实会给你一个“像素对象”作为getPixel的返回值... – MartinStettner

+0

感谢您澄清@MartinStettner - 我永远不会猜测这样的事情是真的。 – jsbueno

4

大多数图形库都有一些直接绘制直线的方法。

JES存在addLine功能,让你可以做

addLine(picture, 0, 0, width, height) 

如果你坚持设定的单个像素,你应该看看Bresenham Line Algorithm,这是最有效的算法,一个画线。

的说明你的代码:你现在带着两个嵌套循环做的是以下

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 

所以基本上您选择填充黑色像素的整个图像。

编辑

要绘制在整个图像的多个斜线(让他们之间有一个空格),可以使用下面的循环

width = getWidth(picture) 
height = getHeight(picture) 
space = 10 
for x in range(0, 2*width, space): 
    addLine(picture, x, 0, x-width, height) 

这给你喜欢(例子中的图像是手绘...)

diagonal lines

这使得使用剪辑功能,大多数图形库提供,即不在图像内的行的部分被忽略。请注意,如果没有2*width(即如果x去只到with),只有线的上左半部分将被绘制...

+0

我确实使用了addLine,但后来意识到我需要它贯穿整个图像 – user2194374

+0

“addLine”生成的行出了什么问题?如果您使用(0,0)和(widht,height)作为线条的终点,则它应该在整个图像之后... – MartinStettner

+0

是的,除了它只会在图片的高度图片左侧。我可以添加一个步骤来扩展线条的范围,但我不知道如何使它们成为对角线。 – user2194374

1

我想一些数学方面的考虑加入讨论......

(只是因为它是可悲的是JES的addLine功能只消耗黑线,是相当有限的......)

注:下面的代码使用Bresenham直线算法 POIN通过MartinStettner(谢谢他)结出。

的布雷森汉姆直线算法是一种算法,它确定为了形成近似于两个给定的点之间的直线。由于像素是一个原子实体,所以只能使用某种近似方法在计算机屏幕上绘制一条线。

注:要理解下面的代码,你需要记住你的基本的学校数学课程(直线方程&三角)一点点。

代码:

# The following is fast implementation and contains side effects... 

import random 

# Draw point, with check if the point is in the image area 
def drawPoint(pic, col, x, y): 
    if (x >= 0) and (x < getWidth(pic)) and (y >= 0) and (y < getHeight(pic)): 
    px = getPixel(pic, x, y) 
    setColor(px, col) 


# Draw line segment, given two points 
# From Bresenham's line algorithm 
# http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm 
def drawLine(pic, col, x0, y0, x1, y1): 

    dx = abs(x1-x0) 
    dy = abs(y1-y0) 
    sx = sy = 0 

    #sx = 1 if x0 < x1 else -1 
    #sy = 1 if y0 < y1 else -1 

    if (x0 < x1): 
    sx = 1 
    else: 
    sx = -1 
    if (y0 < y1): 
    sy = 1 
    else: 
    sy = -1 

    err = dx - dy 

    while (True): 

    drawPoint(pic, col, x0, y0) 

    if (x0 == x1) and (y0 == y1): 
     break 

    e2 = 2 * err 
    if (e2 > -dy): 
     err = err - dy 
     x0 = x0 + sx 

    if (x0 == x1) and (y0 == y1): 
     drawPoint(pic, col, x0, y0) 
     break 

    if (e2 < dx): 
     err = err + dx 
     y0 = y0 + sy 


# Draw infinite line from segment 
def drawInfiniteLine(pic, col, x0, y0, x1, y1): 
    # y = m * x + b 
    m = (y0-y1)/(x0-x1) 
    # y0 = m * x0 + b => b = y0 - m * x0 
    b = y0 - m * x0 

    x0 = 0 
    y0 = int(m*x0 + b) 
    # get a 2nd point far away from the 1st one 
    x1 = getWidth(pic) 
    y1 = int(m*x1 + b) 

    drawLine(pic, col, x0, y0, x1, y1) 


# Draw infinite line from origin point and angle 
# Angle 'theta' expressed in degres 
def drawInfiniteLineA(pic, col, x, y, theta): 

    # y = m * x + b 
    dx = y * tan(theta * pi/180.0) # (need radians) 
    dy = y 

    if (dx == 0): 
    dx += 0.000000001 # Avoid to divide by zero 

    m = dy/dx 

    # y = m * x + b => b = y - m * x 
    b = y - m * x 

    # get a 2nd point far away from the 1st one 
    x1 = 2 * getWidth(pic) 
    y1 = m*x1 + b 

    drawInfiniteLine(pic, col, x, y, x1, y1) 


# Draw multiple parallele lines, given offset and angle 
def multiLines(pic, col, offset, theta, randOffset = 0): 
    # Range is [-2*width, 2*width] to cover the whole surface 
    for i in xrange(-2*getWidth(pic), 2*getWidth(pic), offset): 
     drawInfiniteLineA(pic, col, i + random.randint(0, randOffset), 1, theta) 

# Draw multiple lines, given offset, angle and angle offset 
def multiLinesA(pic, col, offsetX, offsetY, theta, offsetA): 
    j = 0 
    # Range is [-2*width, 2*width] to cover the whole surface 
    for i in xrange(-2*getWidth(pic), 2*getWidth(pic), offsetX): 
     drawInfiniteLineA(pic, col, i, j, theta) 
     j += offsetY 
     theta += offsetA 



file = pickAFile() 
picture = makePicture(file) 
color = makeColor(0, 65, 65) #pickAColor() 
#drawline(picture, color, 10, 10, 100, 100) 
#drawInfiniteLine(picture, color, 10, 10, 100, 100) 
#drawInfiniteLineA(picture, color, 50, 50, 135.0) 
#multiLines(picture, color, 20, 56.0) 
#multiLines(picture, color, 10, 56.0, 15) 
multiLinesA(picture, color, 10, 2, 1.0, 1.7) 

show(picture) 


输出(绘画由皮尔·索拉格斯):


enter image description here

enter image description here

enter image description here


希望这给了一些有趣的想法JES学生......和其他人,以及...

+0

@everyone当'drawInfiniteLine()'的开始处'm'趋于无限时(浮点溢出),会出现一个重要副作用。这可以通过执行如下操作来避免:'if(abs(m)> 100.0):m = 100.0'紧接在计算'm'之后... –

+0

我不再编辑此答案,但是此* *修正我说的可以在这里看到**:http://stackoverflow.com/a/17288511/1715716 –