3

Is there any method to make a graph like following picture? In Gnuplot, have any command like "lineTo , moveTo , arc, ... etc"? If I want to produce some picture like this turtle graphics

What I should do for producing this picture? in turtle graphics, just need some codes

repeat 36 [rt 10 repeat 2 [fd 100 rt 90]]
Alex Stephens
  • 3,017
  • 1
  • 36
  • 41
LessonWang
  • 65
  • 5

3 Answers3

3

You can do similar things with gnuplot. Of course, gnuplot needs to know the coordinates of the lines' start and end points, so you somehow have to calculate them. Something like the code below: you write the coordinates into a datablock and plot it with vectors, also check help vectors. Graph created with gnuplot 5.2.8.

Code:

### vector plot similar to turtle graphics
reset session
set size square 
set angle degrees

x0 = 0
y0 = 0
a0 = 0
r0 = 10
set print $Data
    do for [i=1:36] {
        a0 = a0 - 10
        do for [j=1:2] {
            print sprintf("%g %g %g %g",x0,y0,x0=x0+r0*cos(a0),y0=y0+r0*sin(a0))
            a0 = a0 - 90
        }
    }
set print

plot $Data u 1:2:($3-$1):($4-$2) w vectors nohead notitle
### end of code

Result:

enter image description here

Addition:

By the way: couldn't this turtle graphic command actually be simplified to?

repeat 36 [fd 100 rt 110]

Yes, as @Friedrich shows, it can be done without datablock. Here is a modified version of my first shot without modulo %. The fifth column, i.e. (x0=x0+r*cos(a),y0=y0+r*sin(a),a=a-110) is not used for plotting, but just for calculation.

Code:

### vector plot similar to turtle graphics
reset session

set size square 
set angle degrees
set xrange[-2:12]
set yrange[-10:4]
r = 10
set samples 36
plot a=x0=y0=0 '+' u (x0):(y0):(r*cos(a)):(r*sin(a)): \
                (x0=x0+r*cos(a),y0=y0+r*sin(a),a=a-110) w vec nohead not
### end of code

Result: (similar to graph above)

theozh
  • 22,244
  • 5
  • 28
  • 72
1

A short solution using polar coordinates:

I just noticed a nice behaviour of combing polar mode and the special filename '+'. Along with the updated solution of @theozh using vec, it simplifies to

set size square 
set angle degrees
set polar

pl a=0, [i=1:36:1] '+' us (a=a+110):(1):(100):(0) w vec nohead

Thus, the turtle snippet can be translated almost directly.

Even a bit shorter, the same plot can be done with

set sample 36
pl '+' us ($0*10):(1):(100):(0) w vec nohead
0

Here is a version similar to @theozh without datablock

set size square 
set angle degrees

r = 10
pl a=x=y=0, [i=1:2*36+1:1] '+' us (a=a-int(i)%2*10-90, x=x+r*cos(a)) : (y=y+r*sin(a)) w l t ""

int(i)%2 emulates a second loop counter for an alternating subtraction of 10.