I made an OCaml program which draws on a Graphics window the representation of a given l-system using his definition,his commands interpretation and iteration. The drawing is made using Turtle graphics(the turtle can draw a line,move to a given point and turn for a given angle).
The problem I have is that all the lines have the same size(and this is how it needs to be) and when I draw an L-system if i don't give the right line size the drawing goes out of graphics window as you can see on that picture.
I know I can move the drawing on the left but I always start drawing from the center of the window.What I need help for is how to set the right line size for a given sequence of cammands for example: I have that list of instructions below : ACAABAABABACACAACACACACAACAABABACAABAABABACACAACACACACAACAABABACAABAABABACACAACACACACAACAABABACAABAABABACACAACACACACAACAABA.
where A means: Draw a line of "X" size B : turn π/2 C: turn -π/2.
How can I find the best value for X (size of the line) in order to have a drawing that stays on the graphics window.
The only solution I've found is to start from a given value (Example X=20) and try to draw the l-system with this value, if it goes out then try again with X/2 until it works !
Does anybody have a better idea?
My idea is to make one pass to evaluate sizes of your labyrinth. Let
(W: int)
be your width variable. When painter moves West you decrement W and when your painter moves East you incerementW
. Ifm1
is maximal possible value ofW
andm2
is minimal value (maybe, < 0) ofW
during process then total width of you diagram ispadding + linewidth * (m1-m2)
For example: let's painter initially looks to East.
i.e.
During process
W
will change this way:Robot makes 5 steps to East, moves up, and moves 6 steps on West, moves down and returns to start location. In this case
m1 = 5
andm2 = -1
and you need canvas of size5+(-1)
multiply line width.