设计 L-System 数据结构 (C++)
我正在尝试为 C++ 中的 L-System 重写引擎的实现设计数据结构,但我似乎一无所获:(。
我需要存储一串符号(字符)。有几种类型符号(由 LSystem 的字母表指定)。现在每种类型的符号可以有不同的参数,例如类型 A 的符号有一定的距离,而符号 B 则有一定的距离。 C 符号没有参数,然后
我需要遍历该字符串并执行一些与每种类型的符号相关的操作。 ‘距离’长度”(距离是A的参数),B“转动‘角度’度数”,C完成绘画。
我尝试为每个符号类型创建一个Symbol类和一个子类(SymbolA类、SymbolB类、SymbolC类),但我不知道如何创建字符串,我想避免类型转换和类似的东西,
请问有人有类似的问题或有想法可以帮助我吗?
I'm trying to design data structure for an implementation of L-System rewriting engine in C++ and I just can't seem to get anywhere :(.
I need to store a string of symbols (characters). There are be several types of symbols (that's specified by the LSystem's alphabet). Let's say we have types "A", "B", "C". Now each type of symbol can have different parameters. For example symbol of type A will have some distance and symbol B an angle. C symbol has no parameters. The string then could look like "ABABC".
Then I need to iterate through the string and do some actions that are also connected to each type of symbol. "A" could mean "draw line of 'distance' length" (distance is parameter of A), B "turn 'angle' degrees" and C finish painting.
I tried to have class Symbol and a child for each symbol type (class SymbolA, class SymbolB, class SymbolC), but I don't know how to create the string. I would like to avoid type casts and stuff like that.
Is there somebody who had a similar problem or has an idea that can help me, please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想实现像完整 L 系统一样复杂的东西,我建议使用比 C++ 更高级的语言,例如 Python 或 Common Lisp。然后您可以分析整段代码并在 C/C++ 中实现速度瓶颈。
很久以前,我使用 Common Lisp 在混沌理论和分形课程中实现了 L 系统。做起来并不难——只需使用符号列表即可。我一直在试图找到它的代码,但已经七年多了,所以到目前为止还没有找到。
无论如何,这对我来说似乎是一个更合理的方法。即使您用高级语言进行缓慢的实现,它也会让您更好地了解如何用 C++ 实现它,这需要更多的开发人员时间来完成。
If you want to implement something as complicated as full L-systems, I'd suggest using a higher-level language than C++, such as Python or Common Lisp. Then you can profile the entire piece of code and implement the speed bottlenecks in C/C++.
I implemented L-systems a long time ago for a course in Chaos Theory and Fractals, using Common Lisp. It wasn't too hard to do - just used a list of symbols. I've been trying to find the code for it, but it's been more than 7 years, so no luck so far.
Anyway, this seems like a much more reasonable approach to me. Even if you make a slow implementation in a high-level language, it will give you a better idea of how to implement it in C++, which takes a lot more developer time to do.
在我看来,您走在正确的轨道上:您需要一个 SymbolBase 类,为需要实现的操作类型定义纯虚函数,然后拥有 SymbolA、SymbolB 等派生类,每个派生类都实现特定的功能。为了将元素绘制到屏幕上,每个类将实现一些函数,该函数采用图形对象或类似对象作为参数,并将其自身绘制到图形对象上。
为了在“字符串”中表示这些,您需要某种线性集合,最有可能的是 STL 向量或链表,如果您要使用 L 系统产生式重新排列符号,链表会更有效。然后,您将能够迭代该列表以便将其显示在屏幕上。您将遇到在集合中保存各种类的实例的常见问题,其中元素类型是指向基类的指针。通常不需要太多类型转换就可以让它工作(并且在需要时始终使用dynamic_cast)。如果正确设计基类,那么调用代码应该能够调用基类中的纯抽象函数,而无需关心它实际与哪个特定符号类交互。
Seems to me you're on the right track: You need a SymbolBase class that define pure virtual functions for the kind of operations that need to be implemented, and then have SymbolA, SymbolB, etc. derived classes that each implement the specific functionality. For painting the elements to the screen, each class would implement some function that takes a graphics object or similar as argument and paints itself to the graphics object.
In order to represent these in a 'string', you need a linear collection of some kind, STL vector or linked list most likely, a linked list is more efficient if you're going to rearrange the symbols using the L-system productions. You will then be able to iterate over the list in order to display it on the screen. You'll get into the usual problem of holding instances of various classes in a collection, where the element type is pointers to the base class. You can usually get it working without too many type cast (and always use dynamic_cast when you do need it). If you design your base class correctly, then the calling code should be able to call the pure abstract functions in the base class without ever caring which particular symbol class it's actually interacting with.