简体   繁体   中英

Find right scale to draw an iteration of L-system

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?

You could do some analysis of the L-system to determine its range, and scale appropriately. However, this is not much different than just drawing it with an arbitrary size (say, 1) seeing how big it is, and scaling (once) to fit the screen (not just X/2 until works). For example, if you draw it with scale=1 and it is 40 units in size, and your screen is 400 units, you know you can draw with scale=10 and still fit. You can also use this first pass to determine XY offset so you can center it.

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 incerement W . If m1 is maximal possible value of W and m2 is minimal value (maybe, < 0) of W during process then total width of you diagram is padding + linewidth * (m1-m2)

For example: let's painter initially looks to East.

AAAAABABAAAAAABABA

ie

<<<<<.
>.>>>>

During process W will change this way:

 AAAAABABAAAAA A B A BA
01234555543210-1-1-1-10

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 and m2 = -1 and you need canvas of size 5+(-1) multiply line width.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM