how do I create a line of arbitrary thickness using Bresenham?

后端 未结 12 818
不知归路
不知归路 2020-12-01 03:31

I am currently using Bresenham\'s algorithm to draw lines but they are (of course) one pixel in thickness. My question is what is the most efficient way to draw lines of arb

12条回答
  •  失恋的感觉
    2020-12-01 04:22

    For those who want a Python version here goes the code (based on the answer of @Fabel):

    def drawline(x1,y1,x2,y2,**kwargs):  
        if kwargs.get('thickness')==None:
            thickness=1
        else:
            thickness=kwargs['thickness']
        if kwargs.get('roundcap')==None:
            roundcap=False
        else:
            roundcap=True
        angle = np.arctan2(y2-y1,x2-x1)
        xx = np.zeros(4)
        yy = np.zeros(4)
        xx[0] = np.round(x1 + thickness*np.cos(angle+np.pi/2))
        yy[0] = np.round(y1 + thickness*np.sin(angle+np.pi/2))
        xx[1] = np.round(x1 + thickness*np.cos(angle-np.pi/2))
        yy[1] = np.round(y1 + thickness*np.sin(angle-np.pi/2))
        xx[2] = np.round(x2 + thickness*np.cos(angle-np.pi/2))
        yy[2] = np.round(y2 + thickness*np.sin(angle-np.pi/2))
        xx[3] = np.round(x2 + thickness*np.cos(angle+np.pi/2))
        yy[3] = np.round(y2 + thickness*np.sin(angle+np.pi/2))
        u,v=polygon(xx,yy)    
        if roundcap:
            temp1x, temp1y = circle(x1,y1,thickness)
            temp2x, temp2y = circle(x1,y1,thickness)
            u = np.append(u,temp1x,temp2x)
            v = np.append(v,temp1y,temp2y)
        return u,v
    

    When calling the function you can optionally specify thickness and roundcap. For example:

    drawline(10,10,50,50,thickness=3,roundcap=False)
    

提交回复
热议问题