在控制台上画一条带星号的线

发布于 2024-10-09 03:23:33 字数 48 浏览 0 评论 0原文

如何在控制台上画一条带星号的线?我将接受用户的坐标(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三生路 2024-10-16 03:23:33

假设控制台功能相当强大,您可以将 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.

轻许诺言 2024-10-16 03:23:33

我首先会编写一个方法,在给定的 x 坐标处打印出一颗星。例如给定 x 坐标 7,打印出 6 个空格,然后打印一个星号。

然后,我将确定斜率 (y2-y1)/(x2-x1),确保 x1 是具有较高 y 值的点(在较高的线上,但这可能是较低的实际值)。 循环在每次迭代中按斜率增加 x 值

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

然后通过应该执行此操作的

。编辑:
不小心用 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

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文