在Java中使用海龟图形?
因此,我在 Java 中使用了一组特殊的海龟图形类(但它们具有所有常规命令:移动、绘画、转动等)。我正在尝试绘制一个六角星(实际上是两个三角形)。
谁能给出一些关于如何绘制星星的伪代码?我了解图形的工作原理,并且可以计算点的角度(它们是 30 度),但我真的不明白如何将它们放在一起......?
So, I'm using a special turtle graphics set of classes in Java (but they have all the regular commands: move, paint, turn, etc.). I'm trying to draw a six-point star (which is effectively two triangles).
Could anyone perhaps give some pseudo-code as to how I could draw the star? I understand how the graphics work, and I can calculate the angles of the points (they're 30 degrees) but I don't really get how I could put it all together...?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从星星的顶部开始,面向北(向上)。
向南转(顺时针 180 度),然后逆时针转角度(30 度)。走(距离)。
向北转(逆时针 150 度),然后顺时针转(60 度)。走(距离)。
向南转(顺时针 120 度),然后逆时针转(90 度)。走(距离)。
等等。这应该让您对如何为每个步骤编写算法有一个相对简单的想法。
Start at the top of the star, facing north (up).
Turn south (180 clockwise), and then anti-clockwise the angle (30). Go (distance).
Turn north (150 anti-clockwise), and then clockwise (60). Go(distance).
Turn south (120 clockwise), and then anti-clockwise (90). Go(distance).
Et cetera. This should give you a relatively simple idea as to how to write an algorithm for each step.
首先,我不确定你所说的角度为 30 度是什么意思。等边三角形的内角都是60度。 (它们的总和必须为 180,还记得吗?)。但乌龟必须通过外角(这些角的补角)转动,即 120 度。
您需要弄清楚的另一件事是在绘制三角形之间将乌龟移动多远。这是包围星星的六边形边的长度。有了一点几何知识,你就可以算出这个长度是三角形一边的长度除以 3 的平方根。
这是给你的一些 Logo 代码(这肯定比伪代码更好,对吧?)
您可以在这个在线徽标解释器中使用它:
http://www.calormen.com/logo/
First, I'm not sure what you mean by saying the angles are 30 degrees. The interior angles of an equilateral triangle are all 60 degrees. (They have to add up to 180, remember?). But a turtle has to turn through the exterior angles (the supplements of these), which are 120 degrees.
The other thing you need to figure out is how far to move the turtle in between drawing the triangles. This is the length of a side of the hexagon that circumscribes your star. With a bit of geometry, you can figure out that this length is the length of a side of the triangle divided by the square root of 3.
Here's some Logo code for you (that's got to be better than pseudo-code, right?)
You can play with it at this online Logo interpreter:
http://www.calormen.com/logo/
既然人们声称 Python 是可执行的伪代码,那么怎么样:
您需要提供
HEIGHT
作为构成星形的三角形之一的高度。您可能还需要根据您希望星星指向的方向设置起始方向:Since folks claim that Python is executable pseudo-code, how about:
You'll need to supply
HEIGHT
as the height of one of the triangles that make up the star. You may also need to set the starting orientation depending on which way you want your star to point: