Designing L-System Data Structures (C ++)

I am trying to create a data structure to implement the L-System rewrite mechanism in C ++, and I just can't find anything: (.

I need to save a string of characters (s). There are several types of characters (which are given by the LSystem alphabet). Let's say we have types "A", "B", "C". Now each type of symbol can have different parameters. For example, a character of type A will have some distance, and character B will have an angle. The character C has no parameters. The string may then look like "ABABC".

Then I need to iterate through the string and perform some actions that are also associated with each type of character. “A” can mean “draw a line of length” (distance is parameter A), B is “rotation” is an angle is “degrees,” and C is a finish.

I tried to have a Symbol class and a child for each type of symbol (SymbolA class, SymbolB class, SymbolC class), but I don’t know how to create a string. I would like to avoid typecasting, etc.

Does anyone have a similar problem or have an idea that can help me?

+4
source share
2 answers

If you want to implement something complicated, like complete L-systems, I would suggest using a language of a higher level than C ++, for example Python or Common Lisp. Then you can profile the entire code snippet and implement speed bottlenecks in C / C ++.

I recently implemented L-systems for a course on chaos theory and fractals using Common Lisp. It wasn’t so difficult - just used a list of characters. I tried to find the code for it, but more than 7 years have passed, so no luck so far.

Anyway, this seems like a much more reasonable approach to me. Even if you are performing a slow implementation in a high-level language, this will give you a better idea of ​​how to implement it in C ++, which requires much more time for developers.

+1
source

It seems that you are on the right track: you need the SymbolBase class, which defines pure virtual functions for the operations that need to be implemented, and then have the SymbolA, SymbolB, etc. classes that each implements a specific functionality. To draw elements on the screen, each class will implement some function that takes a graphic object or is similar to an argument and draws a graphic object itself.

To represent them in a string, you need a linear collection of some type, an STL vector or a linked list, most likely a linked list is more efficient if you are going to change characters using L-system productions. You can then iterate over the list to display it on the screen. You will encounter the usual problem of storing instances of various classes in a collection, where the element type is a pointer to the base class. You can usually make it work without too many types (and always use dynamic_cast when you need it). If you correctly create your base class, then the calling code should be able to call pure abstract functions in the base class without worrying about which specific character class it actually interacts with.

0
source

Source: https://habr.com/ru/post/1343221/


All Articles