在Java中使用海龟图形?

发布于 2024-12-11 19:40:38 字数 163 浏览 0 评论 0原文

因此,我在 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 技术交流群。

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

发布评论

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

评论(3

淡水深流 2024-12-18 19:40:38

从星星的顶部开始,面向北(向上)。

向南转(顺时针 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.

懒的傷心 2024-12-18 19:40:38

首先,我不确定你所说的角度为 30 度是什么意思。等边三角形的内角都是60度。 (它们的总和必须为 180,还记得吗?)。但乌龟必须通过外角(这些角的补角)转动,即 120 度。

您需要弄清楚的另一件事是在绘制三角形之间将乌龟移动多远。这是包围星星的六边形边的长度。有了一点几何知识,你就可以算出这个长度是三角形一边的长度除以 3 的平方根。

这是给你的一些 Logo 代码(这肯定比伪代码更好,对吧?)

TO Star6 :size
  ; Draw First Triangle
  REPEAT 3 [FD :size RT 120]

  ; Reposition for Second Triangle
  PU
  RT 90
  FD :size / SQRT 3
  LT 90
  PD

  ; Draw Second Triangle
  REPEAT 3 [FD :size LT 120]

  ; Return to starting position
  PU
  LT 90
  FD :size / SQRT 3
  RT 90
  PD
END

; Draw some stars of various sizes and colors
CS
SETCOLOR "Red
Star6 50
SETCOLOR "Green
Star6 100
SETCOLOR "Blue
Star6 200

您可以在这个在线徽标解释器中使用它:
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?)

TO Star6 :size
  ; Draw First Triangle
  REPEAT 3 [FD :size RT 120]

  ; Reposition for Second Triangle
  PU
  RT 90
  FD :size / SQRT 3
  LT 90
  PD

  ; Draw Second Triangle
  REPEAT 3 [FD :size LT 120]

  ; Return to starting position
  PU
  LT 90
  FD :size / SQRT 3
  RT 90
  PD
END

; Draw some stars of various sizes and colors
CS
SETCOLOR "Red
Star6 50
SETCOLOR "Green
Star6 100
SETCOLOR "Blue
Star6 200

You can play with it at this online Logo interpreter:
http://www.calormen.com/logo/

反目相谮 2024-12-18 19:40:38

既然人们声称 Python 是可执行的伪代码,那么怎么样:

SIDE_LENGTH = 2 * HEIGHT / sqrt(3)
CIRCUMSCRIBED_RADIUS = 2 * HEIGHT / 3

for triangle in (1, 2):
    turtle.penup()
    turtle.right(150)
    turtle.forward(CIRCUMSCRIBED_RADIUS * triangle)
    turtle.right(30)
    turtle.pendown()

    for side in (1, 2, 3):
        turtle.right(120)
        turtle.forward(SIDE_LENGTH)

您需要提供 HEIGHT 作为构成星形的三角形之一的高度。您可能还需要根据您希望星星指向的方向设置起始方向:

在此处输入图像描述

Since folks claim that Python is executable pseudo-code, how about:

SIDE_LENGTH = 2 * HEIGHT / sqrt(3)
CIRCUMSCRIBED_RADIUS = 2 * HEIGHT / 3

for triangle in (1, 2):
    turtle.penup()
    turtle.right(150)
    turtle.forward(CIRCUMSCRIBED_RADIUS * triangle)
    turtle.right(30)
    turtle.pendown()

    for side in (1, 2, 3):
        turtle.right(120)
        turtle.forward(SIDE_LENGTH)

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:

enter image description here

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