tree

This is a discussion on tree within the logo forums in Programming Languages category; Hi I am looking for explanations on how to draw a flowchart of tree with side say 40 and angle 40 and level 2 I would like to understand how to draw the tree in terms of a flowchart Thanks...

Go Back   Application Development Forum > Programming Languages > logo

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 04-03-2008, 10:29 PM
a11
Guest
 
Default tree

Hi

I am looking for explanations on how to draw a flowchart of tree with
side say 40 and angle 40 and level 2


I would like to understand how to draw the tree in terms of a
flowchart

Thanks
Reply With Quote
  #2  
Old 04-04-2008, 12:07 PM
Andreas Micheler
Guest
 
Default Re: tree

a11 wrote:
> I am looking for explanations on how to draw a flowchart of tree with
> side say 40 and angle 40 and level 2
>
> I would like to understand how to draw the tree in terms of a
> flowchart


Oh, that's a bigger problem, depending on how complex your tree is.
You might try this in aUCBLogo as a first guess:

to testflowchart
draw_proc "testProcedure
end

to testProcedure x
print 5
if lessP x 1 [stop]
ifelse greaterP x 10
[ print "x>10
][ print "x<=10
]
testProcedure x-1
end

to testProcedure2 x
print 5
if x < 1 [stop]
ifelse x > 10
[ ifelse x > 10
[ print "x>10
][ print "x<=10
]
][ print "x<=10
]
testProcedure2 x-1
end

to draw_proc procname
local [tree startPos s]
; tree=butFirst makeTreeFromBody Text procname
tree=
[ [print 5]
[if [:x < 1][stop]]
[ifelse [:x > 10][print "x>10][print "x<=10]]
[testProcedure [- :x 1]]
]
clearText
show :tree
clearScreen
WindowMode
hideTurtle
enableTextureFont
PenUp
setXY 0 300
s=draw_start
startPos=Pos-(list s.1/2 -s.2/2)
draw_arrowxy Pos (list xCor yCor-30)
draw_flowChart :tree
; draw_end
end

to draw_flowChart x
local [s]
foreach x
[ case ?.1
[ [[if] [draw_if ?]]
[[ifelse] [draw_ifelse ?]]
[procname [ draw_arrowxy Pos (list xCor-300 yCor+20)
draw_arrowyx Pos startPos stop]]
[[stop output] [s=draw_text ? setY yCor-20 stop]]
[else [s=draw_text ?]]
]
draw_arrowxy Pos (list xCor yCor-20)
]
end

to draw_start
output draw_text "Start
end

to draw_end
output draw_text "End
end

to draw_if x
local [s p1 p2]
s=draw_diamond_text first butFirst x
p1=Pos
p1.1=p1.1+s.1/2
p1.2=p1.2+s.2/2
draw_yes
draw_flowChart butFirst butFirst x
p2=Pos
PenUp
setPos p1
draw_no
draw_arrowyx Pos p2
end

to draw_ifelse x
local [s p1 p2]
s=draw_diamond_text first butFirst x
p1=Pos
p1.1=p1.1+s.1/2
p1.2=p1.2+s.2/2
draw_yes
draw_flowChart butLast butFirst butFirst x
p2=Pos
PenUp
setPos p1
draw_no
draw_flowChart butFirst butFirst butFirst x
draw_arrowyx Pos p2
end

to draw_yes
local [p s]
s=(LabelSize "Yes)/4
p=Pos
relXY s.1 -s.2/2
Label "Yes
setPos p
draw_arrowxy Pos (List xCor yCor-20)
end

to draw_no
local [p s]
s=(LabelSize "No)/4
p=Pos
relXY s.1/3 s.2/2
Label "No
setPos p
draw_arrowxy Pos (List xCor+150 yCor-20)
end

to draw_text txt
local [s w h]
s=(LabelSize txt)/3
w=s.1
h=s.2
setY yCor-h/2-5
draw_rect w h
setHeading 90
Label txt
setY yCor-h/2
output s
end

to draw_diamond_text txt
local [s w h]
s=(LabelSize txt)/2
w=s.1
h=s.2
setY yCor-h/2-5
draw_diamond w h
setHeading 90
Label txt
setY yCor-h/2
output s
end

to draw_rect width height
PenUp
setXY xCor-width/2 yCor-height/2
PenDown
setX xCor+width
setY yCor+height
setX xCor-width
setY yCor-height
PenUp
setXY xCor+width/2 yCor+height/2
end

to draw_diamond width height
PenUp
setY yCor+height/2
PenDown
setXY xCor-width/2 yCor-height/2
setXY xCor+width/2 yCor-height/2
setXY xCor+width/2 yCor+height/2
setXY xCor-width/2 yCor+height/2
PenUp
setY yCor-height/2
end

to draw_arrowxy s d
local [x y]
y=(signum d.2-s.2)*10
x=5
PenUp
setPos s
PenDown
setX d.1
setY d.2
relXY -x -y
relXY x y
relXY x -y
relXY -x y
PenUp
end

to draw_arrowyx s d
local [x y]
x=(signum d.2-s.2)*10
y=5
PenUp
setPos s
PenDown
setY d.2
setX d.1
relXY -x y
relXY x -y
relXY -x -y
relXY x y
PenUp
end

to relXY x y
setXY xCor+x yCor+y
end

For other procedures, just uncomment the line:
; tree=butFirst makeTreeFromBody Text procname
and delete the other definiton of tree.
(I only wrote the second definition,
because it looks nicer on graphics screen without the tabs.
You could also improve it
using "replace" with the tab character on the tree.)

If you need to draw loops,
you can implement them similar as draw_if.
Or ask again in the group.

Cheers,
Andreas
Reply With Quote
  #3  
Old 04-04-2008, 01:52 PM
Andreas Micheler
Guest
 
Default Re: tree

Hallo again,

I've been interested in the flowchart problem a bit more,
so I improved my demo a bit in respect of the sizes of sub-elements,
I used replace to avoid the list formatting chars,
and I implemented draw_while.

Have fun!

;_________________________________________________ _______________________

to testflowchart
size=0.7
show draw_proc "testProcedure2
end

to testProcedure x
print 5
if lessP x 1 [stop]
ifelse greaterP x 10
[ print "x>10
][ print "x<=10
]
testProcedure x-1
end

to testProcedure2 x
if lessP x 1 [stop]
y=1
while [lessP y 20]
[ ifelse greaterP y 10
[ ifelse greaterP y 15
[ print "y>15
][ print "10<y<=15
]
][ print "y<=10
]
y=y+1
]
testProcedure2 x-1
end

to draw_proc procname [shouldDraw true]
local [r tree startPos]
tree=butFirst makeTreeFromBody Text procname
clearText
show :tree
clearScreen
WindowMode
hideTurtle
enableTextureFont
setLabelSize [20 20]*size
PenUp
setXY 0 300
r=draw_start
startPos=r.1
startPos.2=startPos.2+(r.(2).2-r.(1).2)/2
r=addrr r draw_arrowxy Pos (list xCor yCor-30*size)
r=addrr r draw_flowChart :tree
; draw_end
output r
end

to draw_flowChart x [shouldDraw true]
local [p0 r]
p0=pos
r=(List Pos Pos)
foreach x
[ case ?.1
[ [[if] [r=addrr r draw_if ?]]
[[ifelse] [r=addrr r draw_ifelse ?]]
[[while] [r=addrr r draw_while ?]]
[procname [ r=addrr r draw_arrowxy Pos (list r.(1).1-20*size r.(1).2)
r=addrr r draw_arrowyx Pos startPos output r]]
[[stop output] [r=addrr r draw_text ? setY yCor-20 output r]]
[else [r=addrr r draw_text ?]]
]
r=addrr r draw_arrowxy Pos (list xCor yCor-20*size)
]
if not shouldDraw [setPos p0]
output r
end

to draw_start
output draw_text "Start
end

to draw_end
output draw_text "End
end

to draw_if x
local [r ryes s p1 p2]
r=draw_diamond_text first butFirst x
p1=Pos
p1.1=r.(2).1
p1.2=r.(1).2+(r.(2).2-r.(1).2)/2
r=addrr r draw_yes
ryes=(draw_flowChart butFirst butFirst x shouldDraw)
r=addrr r ryes
p2=Pos
PenUp
setPos p1
r=addrr r draw_no ryes
r=addrr r draw_arrowyx Pos p2
output r
end

to draw_ifelse x
local [r ryes s p1 p2]
r=draw_diamond_text first butFirst x
p1=Pos
p1.1=r.(2).1
p1.2=r.(1).2+(r.(2).2-r.(1).2)/2
r=addrr r draw_yes
ryes=(draw_flowChart butLast butFirst butFirst x shouldDraw)
r=addrr r ryes
p2=Pos
PenUp
setPos p1
ryes=dontoverlap ryes
(draw_flowChart butFirst butFirst butFirst x false)
r=addrr r draw_no ryes
r=addrr r draw_flowChart butFirst butFirst butFirst x
r=addrr r draw_arrowyx Pos p2
output r
end

to draw_yes
local [r p s]
s=butLast (LabelSize "Yes)/4
p=Pos
r=(list p p+s+s/2)
relXY s.1 -s.2/2
Label "Yes
setPos p
r=addrr r draw_arrowxy Pos (List xCor yCor-20*size)
output r
end

to draw_no ryes
local [r x p s]
s=butLast (LabelSize "No)/4
p=Pos
r=(list p p+s+s/2)
relXY s.1/3 s.2/2
Label "No
setPos p
x=(max ryes.(2).1+20*size p.1)
r=addrr r draw_arrowxy Pos (List x yCor-20*size)
output r
end

to draw_while x
local [r ryes s p1 p2 p3]
r=draw_diamond_text first butFirst x
p1=Pos
p1.1=r.(2).1
p1.2=r.(1).2+(r.(2).2-r.(1).2)/2
p3=(list r.(1).1 p1.2)
r=addrr r draw_yes
ryes=(draw_flowChart butFirst butFirst x shouldDraw)
r=addrr r ryes
p2=Pos
r=addrr r draw_arrowxy Pos (list r.(1).1-20*size Pos.2+20*size)
r=addrr r draw_arrowyx Pos p3
p2.2=p2.2-20*size
PenUp
setPos p1
r=addrr r draw_no ryes
r=addrr r draw_arrowyx Pos p2
output r
end

to noformat txt
output replace "| | " replace "|
| " txt
end

to draw_text txt
local [r s w h]
s=(LabelSize txt)/3
w=s.1
h=s.2
setY yCor-h/2-5*size
r=draw_rect w h
setHeading 90
if shouldDraw
[ Label noformat txt
]
setY yCor-h/2
output r
end

to draw_diamond_text txt
local [r s w h]
s=(LabelSize txt)/2.5
w=s.1
h=s.2
setY yCor-h/2-5*size
r=draw_diamond w h
setHeading 90
if shouldDraw
[ Label noformat txt
]
setY yCor-h/2
output r
end

to draw_rect width height
local [r]
PenUp
setXY xCor-width/2 yCor-height/2
myPenDown
setX xCor+width
setY yCor+height
setX xCor-width
setY yCor-height
PenUp
setXY xCor+width/2 yCor+height/2
r=(list Pos-(list width/2 height/2) Pos+(list width/2 height/2))
output r
end

to draw_diamond width height
local [r]
PenUp
setY yCor+height/2
myPenDown
setXY xCor-width/2 yCor-height/2
setXY xCor+width/2 yCor-height/2
setXY xCor+width/2 yCor+height/2
setXY xCor-width/2 yCor+height/2
PenUp
setY yCor-height/2
r=(list Pos-(list width/2 height/2) Pos+(list width/2 height/2))
output r
end

to draw_arrowxy s d
local [x y]
y=(signum d.2-s.2)*10*size
x=5*size
PenUp
setPos s
myPenDown
setX d.1
setY d.2
relXY -x -y
relXY x y
relXY x -y
relXY -x y
PenUp
output (list s d)
end

to draw_arrowyx s d
local [x y]
x=(signum d.2-s.2)*10*size
y=5*size
PenUp
setPos s
myPenDown
setY d.2
setX d.1
relXY -x y
relXY x -y
relXY -x -y
relXY x y
PenUp
output (list s d)
end

to relXY x y
setXY xCor+x yCor+y
end

to myPenDown
if shouldDraw [PenDown]
end

to addrp r p
for [i 1 2]
[ if p.i < r.(1).i [r.(1).i=p.i]
if p.i > r.(2).i [r.(2).i=p.i]
]
output r
end

to addrr r r2
for [i 1 2]
[ if r2.(1).i < r.(1).i [r.(1).i=r2.(1).i]
if r2.(2).i > r.(2).i [r.(2).i=r2.(2).i]
]
output r
end

to dontoverlap r1 r2
local [r]
r=(list r1 r1)
if r2.(1).1 < r1.(2).1 [r.(2).1=r1.(2).1+(r2.(2).1-r2.(1).1)/2]
output r
end
;_________________________________________________ _______________________

Cheers,
Andreas
Reply With Quote
  #4  
Old 04-05-2008, 02:38 AM
Brian Harvey
Guest
 
Default Re: tree

a11 <ashabsurya@gmail.com> writes:
>I would like to understand how to draw the tree in terms of a
>flowchart


I'd like to hear more about the context of this desire. Usually, people make
flowcharts because their managers (who aren't programmers) insist. Flowcharts
aren't very good tools for understanding programs, and I think that's
particularly true for recursive procedures.

Are you trying to unfold the recursion and see the complete sequence of events
as the procedure runs? That's likely to be confusing; much better is to
convince yourself that you can just trust the recursive calls to draw
simpler, smaller trees and leave the turtle's position and heading unchanged.
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 02:06 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.