Drawing a super-ellipse with a turtle

2019-03-05 12:37发布

问题:

Obviously, any shape drawable by other means can be drawn by a turtle. Circles and squares are easy

rt 1 fd .0

and

if ticks mod 100 = 0 [rt 90]
fd 1

Super-ellipses not so much. (regular ellipses are not trivial either.) The Wikipedia article on super-ellipses if you need to be refreshed on the topic.

Any input is appreciated.

Using a pendown turtle is there way to make a super-ellipse that emerges from turtle movement?

回答1:

I have 1/4 of it, I suppose you could piece-wise put the other three together. Other values of n are not tested here. (using the Wiki notation, plus phi as an angle of rotating the whole thing.) And the placement of reset-ticks, pen-down, is sloppy, I know.

to go2
  clear-all
  reset-ticks
  let a 6
  let b 5
  let phi 0
  let n 3.5
  create-turtles 1 [
    let iNdx 1
    repeat 90 [
      show iNdx
      show cos(iNdx)
      if cos(iNdx) > 0 and sin(iNdx) > 0 [
        let tx (a * (cos(iNdx) ^ (2 / n)))
        let ty (b * (sin(iNdx) ^ (2 / n)))
        let tx2 tx * cos(phi) - ty * sin(phi)
        let ty2 tx * sin(phi) + ty * cos(phi)
        setxy tx2 ty2
        ]
      pen-down
      set iNdx iNdx + 1
      ]
    ]
  end

The ellipse looks simpler, but you be the judge

to go
  clear-all
  reset-ticks
  let a 6
  let b 5
  let phi 45
  create-turtles 1 [

    let iNdx 1
    repeat 360 [
      let tx (a * cos(iNdx))
      let ty (b * sin(iNdx))
      let tx2 tx * cos(phi) - ty * sin(phi)
      let ty2 tx * sin(phi) + ty * cos(phi)
      setxy tx2 ty2
      pen-down
      set iNdx iNdx + 1
      ]
    ]
  end

a generalization and simplification as a procedure.

to Super-ellipse [x y a b  m n]
 create-turtles 1 [
 let iNdx 1
 repeat 360 [
 setxy  (x + (abs cos iNdx)^(2 / m) * a * (sgn cos iNdx)) 
        (y + (abs sin iNdx)^(2 / n) * b * (sgn sin iNdx))
 pendown
set iNdx iNdx + 1]
]
end


回答2:

The generalized form of another answer seems to produce the sort of thing I was thinking of. the closer the pen starts to one of the foci the closer the drawing is to a square. the n<1 super-ellipses are not achieved.

globals[c]  
breed [pens pen] 
breed [foci focus]
foci-own [dist distx disty]
to setup
 ca
 create-pens 1 [set heading 45 fd 10 pendown set C self]
 ;create-foci 1 [setxy (random-xcor / 2) (random-ycor / 2)]
 create-foci 1 [setxy 10 10]
 create-foci 1 [setxy 10 -10]
 create-foci 1 [setxy -10 -10]
 create-foci 1 [setxy -10 10]
end

 to go
 repeat 5100
  [
  ask foci [ 
           set dist distance c 
           set distx xcor - [xcor] of c
           set disty ycor - [ycor] of c
         ]
ask c
 [
       set heading 90 + atan ( sum [distx / dist] of foci / sum [dist] of foci) 
                       ( sum [disty / dist] of foci / sum [dist] of foci) 
       FD .0125
  ]
  ]
end