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

后端 未结 12 864
不知归路
不知归路 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:38

    For best accuracy, and also good performance for thicker lines especially, you can draw the line as a polygon. Some pseudo code:

    draw_line(x1,y1,x2,y2,thickness)
      Point p[4];
      angle = atan2(y2-y1,x2-x1);
      p[0].x = x1 + thickness*cos(angle+PI/2);
      p[0].y = y1 + thickness*sin(angle+PI/2);
      p[1].x = x1 + thickness*cos(angle-PI/2);
      p[1].y = y1 + thickness*sin(angle-PI/2);
      p[2].x = x2 + thickness*cos(angle-PI/2);
      p[2].y = y2 + thickness*sin(angle-PI/2);
      p[3].x = x2 + thickness*cos(angle+PI/2);
      p[3].y = y2 + thickness*sin(angle+PI/2);
      draw_polygon(p,4)
    

    And optionally a circle could be drawn at each end point.

提交回复
热议问题