在控制台上画一条带星号的线
如何在控制台上画一条带星号的线?我将接受用户的坐标(x1,y1),(x2,y2)。
How can I draw a line with asterisks on the console? I will accept the coordinates from the user (x1,y1), (x2,y2).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设控制台功能相当强大,您可以将 ANSI 转义代码组合用于水平和垂直位置 (HPV) 与Bresenham画线算法。
附录:由于这是家庭作业,Bresenham 算法太过分了。因为这是一项常见作业,所以您可以查看其他人如何解决该问题。此外,您可以编辑问题以包含您的代码和有关作业的其他详细信息。
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.
我首先会编写一个方法,在给定的 x 坐标处打印出一颗星。例如给定 x 坐标 7,打印出 6 个空格,然后打印一个星号。
然后,我将确定斜率 (y2-y1)/(x2-x1),确保 x1 是具有较高 y 值的点(在较高的线上,但这可能是较低的实际值)。 循环在每次迭代中按斜率增加 x 值
然后通过应该执行此操作的
。编辑:
不小心用 x 值进行了循环,并且由于您想逐行进行循环,因此应该使用 y 值。还阐明了如何选择哪个点是 (x1,y1)
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
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)