Bart Snapp
Bart Snapp

Reputation: 113

Reflections (geometric transformations) in LOGO

Is it possible to do geometric transformations in LOGO?

In particular are horizontal reflections possible?

Starting with:

to ngon :n :s
  repeat :n [ fd :s lt 360/:n]
end

ngon 5 50

I would like a command that would reflect across the line y=0 that would work something like

reflect ngon 5 50

and would produce the same result as

repeat 5 [ fd 50 rt 360/5]

Upvotes: 1

Views: 63

Answers (1)

Tim Goodman
Tim Goodman

Reputation: 23976

Well, you can't change the behavior of lt and rt. But there are some options.

Option 1

You could write your own reversible versions of lt and rt, and use these in your procedures where you previously used the regular versions:

make "reversed 0

to lt1 :a
    ifelse :reversed [rt :a] [lt :a]
end

to rt1 :a
    ifelse :reversed [lt :a] [rt :a]
end

to reflect :fn
    make "reversed 1
    run :fn
    make "reversed 0
end

to ngon :n :s
    repeat :n [ fd :s lt1 360/:n]
end

ngon 5 50
reflect [ngon 5 50]

Option 2

You could wrap commands inside your procedures with a wrapper that executes the command after swapping lt and rt as needed:

make "reversed 0

to reflect :fn
    make "reversed 1
    run :fn
    make "reversed 0
end

to replace_all :s :find :replace
   make "head first :s
   make "tail butfirst :s
   if (list? :head) [make "head (list replace_all :head :find :replace)]
   if (:head = :find) [make "head :replace]
   if (count :tail) = 0 [output :head]
   output (se :head replace_all :tail :find :replace) 
end

to wrapper :func
    if :reversed [
        make "func replace_all :func "lt "lt1
        make "func replace_all :func "rt "lt
        make "func replace_all :func "lt1 "rt
    ]
    run :func
end

to ngon :n :s
    wrapper [ repeat :n [ fd :s lt 360/:n] ]
end

ngon 5 50
reflect [ngon 5 50]

Ideally there'd be a Logo command to get the body of an existing procedure, and then we could apply the wrapper to each line of the procedure dynamically, without needing to modify the procedure definition itself. But, I don't think such a command exists in Logo.

It's also worth noting that there are other commands that can change the heading or x-coordinate of the turtle. But we could imagine code similar to above that also updates seth, xcor, setx, pos, setpos, etc.

Upvotes: 1

Related Questions