## Generating some shapes

Now I haven’t gotten around to explaining the whole point of this blog yet but meanwhile I’m actually doing “experiments” with the code I’ve got and so I’ll jump out of sequence and report an experiment I just did.

Let’s just say I’ve developed a program, an “interpreter” (so it’s easier to experiment than updated C# code itself) that has a little “language”. I just type in a “program” which will depend of some “parameters” which I can specify separately. Then I have some UI controls to determine how the program is run. The result is figures drawn on my UI, but also a “montage” that is created that I can save somewhere (previously just in my OneNote log, now in this blog as well).

So without explaining all the details, here’s the function I’m looking at, one of the most basic (in polar form):

# r = sin(A×θ)

and to convert to X,Y (Cartesian) coordinates (which I need to actually draw the plot)

## x = r×cos(θ)  y=r×sin(θ)

Your basic math lesson for the day. If this is too tough for you the math part of these (only high school level) may be tough for you.

So you get: Note: The blue stuff is the polar coordinates grid.

And to get the orientation of this figure I wanted I pulled a little sneaky on you

# r = sin(A×θ + Φ)

where Φ is known as “phase” and in this case I used 90º for Φ which really converts the sine into a cosine (remember your high school trig, eh?)

So I implement this with my code like this:

```A 0.6666667
u sin(T A 90)
c cos(T)
s sin(T)
x multiply(r c)
y multiply(r s)```

Note: I hate wasting time writing parsers so I don’t do expressions in my language and instead use functions, plus all “constants” are just a single upper case letter and “variables” are a single lower case variables (saves having build symbol tables). I’m just lazy on this part and parsers aren’t interesting to me.

So now we get to the part where my program can get something to look at fairly quickly. In addition to just setting A to some value (which then draws difference figures) I built a simple “iterator” for A, wie so:

`@ A 0.25 0.5 3`

So this repeats the drawing of the programming but with values of A {1/2 3/4 1-1/2 …} so I can see variations of the basic parametric equation. This process creates a “montage” of thumbnails of the figure (or a “film strip” with just one parameter being iterated) so now we see some results: Note: WordPress makes my graphics kinda ugly, I’ll have to figure out how to tune my resolution to get them to look better, but you get the idea.

This is just iterating A from 1 to 9 creating “flowers”, a really basic shape. So let’s look at some variations: This is really the same as above but using cosine instead of sine to generate the values of r, resulting in a rotation of the figure (can you deduce how much the rotation is in each figure?)

Now things get a little more interesting when I use fractional values of A instead of just integers, but then to draw the figure takes more “cycles” (the number of 360º periods), so here’s A starting at 0.5 and incrementing by 0.5: or A starting at 1/3 and incrementing by 1/3 or A starting at 1/4 and incrementing by 1/4 Getting a little tedious, eh? But I can crank these “filmstrips” out really easily just by changing some text inputs in my UI rather than having to go back and modify C# and recompile and rerun, and that’s the point, easy to try lots of combinations looking for some that may be interesting.

Now if we get bored with these, my “language” also allows me to quickly change the “program” so what about instead of:

I use:

## r = sin(A×θ) ^ ½

(^ ½ is standard notation for taking square root of function – I can easily use any power, even square root, which is a bit tricky as sine functions generate negative numbers and I don’t want to mess with complex numbers (those containing i or √-1)) and I get: And if that is getting a little boring how about using some different type of wave function:

Here’s an ordinary sine wave (2 periods): But let’s try this one instead: otherwise known as a triangle wave for obvious reasons and it produces: The “flowers” instead of being curved at their tips get kinda “pointy” and we can easily vary this a bit, but just taking cube root of triangle which looks like this: and produces: but there’s another thing we can do with triangle waves and that is change their “duty cycle” and make them asymmetrical, like this: and the cube root of that triangle produces “flowers” that look like this: and I can keep playing with these parameters and see what I get, maybe interesting, maybe not.

But let’s look at one more variation. Instead of a simple wave I can use a “composite” of several sine’s with different frequencies and phases, like this: and a composite wave (not the one above, a bit different) can produce And on and on, easy to generate these things, again some interesting, some not, but so what? This basic shapes can then be modified (scaled, rotated) and place on some path, like 12 of them around the outside of a circle, and there is part of a mandala.

So here’s one of these variations I like and might use as a shape in a mandala: Or perhaps this one: So end of math lesson and a look ahead at the applications I’m writing with more to come about how I put this all together and how I got started on all this. 