开发者

Drawing a line with asterisks on the console

How can I draw a line 开发者_如何学Cwith asterisks on the console? I will accept the coordinates from the user (x1,y1), (x2,y2).


Assuming a reasonably capable console, you can combine the ANSI escape code for Horizontal and Vertical Position (HPV) with Bresenham's line drawing algorithm.

Addendum: As this is homework, Bresenham's algorithm is overkill. Because it's a common assignment, you might look at how others have approached the problem. In addition, you can edit your question to include your code and other details about the assignment.


I would first write a method that prints out a star at a given x-coordinate. Such as given an x coordinate 7, print out 6 spaces and then an asterisk.

I would then determine the slope (y2-y1)/(x2-x1) make sure that x1 is the point with the higher y value(on a higher line, but this might be a lower actual value). Then increment the x value by the slope on each iteration through a loop

for(i = y1; i<=y2;i++)
 asteriskprintfunction(x1+(slope*i));

that should do it.

edit: had accidentally made the loop with x values and since you want to go line by line, you should use the y values. also clarified how to pick which point is (x1,y1)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜