At third, if you want to learn more about how that script works keep reading.
1. Script Parameters and variables:- @CenterX & @CenterY - Starting point of the initial triangle. It is set to (0,0), but really does not matter unless you have special requirements.
- @Radius - That is a Radius of Circumscribed circle of the Initial Triangle. It can be any, but do not make it too small because you can loose precision.
- @Angle - An Angle you want your Initial Triangle to be turned. It really does not matter, you just have ability to rotate Fractal on the screen. Try to use value "15" for better effect visibility.
- @MaxLevel - Specifies how many levels deep you want to draw your Fractal. That value can't be big. With Max Level value "9" I've got almost 2 millions of objects. So, you can try more if you want to wait. However SSMS will not be able to show you so many objects. The maximum Number I've got was little bit lower than 250K.
- @CurrLevel - Just a variable to track current Level of Fractal's Triangles
- @Rate - Rate with which triangles will be decreasing in size. With Rate = "1" child triangle's Radius of Circumscribed circle will be twice as less Radius of the Parent's triangle. You can play with that value, but pictures might be not so nice.
2. Temporary TableIt contains all parameters of created triangles such as
- Center point;
- Coordinates of all Corners of a triangle;
- Radius of Circumscribed circle;
- Triangle Inclination Angle;
- Level of the triangle (1 is the highest);
- Processing Bit Indicator. Set to 1 if triangle already has children;
- ID Identity field;
- Calculated Spatial field. It allows you to see graphical results by just simple selecting that field from the table.
3. Triangle construction.There is no sacred magic in the original triangle construction, so, will start from the child generation level in Step 2.1.
3.1. Lines 59-64. Here we direct how many child triangles will have each side of a parent. To demonstrate that change value of variable "@MaxLevel = 2" and replace that section with the following and run the whole script:
As the result you have to have something like this:
3.2. Lines 68-69 - Simple extraction of Parent triangle parameters.
3.3. Lines 73-80 - Radius, Angles and center points of child triangles. Lines 76 & 79 are necessary to construct non-central children.
3.4. Lines 83-92 - Generation of corners of children triangles.
3.5. Lines 95-96 - Marking filled parent triangles and new children.
3.6. Line 99 - Moving to the next cycle level.
4. Triangle presentationThat is most difficult operation. At first, SSMS can't show more than 5000 separate objects at the same time. In order to show more we have to construct "MULTIPOLYGON" or "GEOMETRYCOLLECTION". That only the way to fit more objects into SSMS screen. However it is still limited.
In order to combine triangles in a single object we divide them in buckets (Line 106).
In this example I just making number of buckets approximately equal to a number of objects within each bucket. Making lower number of buckets will increase processing speed, but produce less colors. All objects in one collection will have the same color.
Also, I wrapped the last query in extra CTE to have more flexibility on results formation.
5. Playing with resultsAs I mentioned in #4, you can change Line 124 to produce slightly different results.
For instance you can replace it with that:
"SELECT Polygon.STBoundary() FROM Collections"
That will show ONLY the boundaries of all triangles and you might see something like this:
You also can play with number of buckets in Line 106, replacing it by following:
"Bucket = TriangleID % 5000"
6. Playing with configuration.Besides of changing an inclination angle the biggest change is number of children triangles.
Do the following:
- Replace Lines 59-64 by following:
- Change "@Rate = 1.1"
- Change"@MaxLevel = 9"
You might have something like this:
I understand that those fractals are not "Classical", but it is something to start with. You've got an idea and that is most important.