Pages

Friday, 27 March 2020

Chapter 12 // Exercise 12 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and the graphics files found here:
https://github.com/l-paz91/principles-practice/tree/master/Graphics%20Files

Chapter 12 // Exercise 12

A superellipse is a two-dimensional shape defined by the equation
Look up superellipse on the web to get a better idea of what such shapes look like. Write a program that draws "starlike" patters by connecting points on a superellipse. Take a, b, m, n, and N as arguments. Select N points on the superellipse defined by a, b, m, and, n. Make the points equally spaced for some definition of 'equal'. Connect each of those points to one or more other points (if you like you can make the number of points to which to connect a point another argument or just use N-1, i.e., all the other points).

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2012/Exercise%2012/Exercise%2012%20-%20Complete

Eurgh; another mind blower. I was upset as well because there is nothing on super ellipses on Maths is Fun. After searching and failing to understand code examples dotted across the internet; I ended up watching this video which was extremely helpful:
https://youtu.be/z86cx2A4_3E

He does code in javascript but it's easy enough to adapt. Whilst trying to draw points in a circle I realised that we really need a way to add doubles to the mix as values were getting narrowed left right and centre (pun intended). I changed Point to use doubles instead and then had to change a couple other constructors in order for it to build. There will be many, many warnings though. Through following the video I managed to draw a circle using a Polyline:


The + 300 on each line is just to change the default centre from 0,0 to wherever you want it. From there he then showed that the formula we care about to make a super ellipse is this one from wikipedia:
I'm really glad he pointed this out and took the time to explain it as I think I understand now. Using these formulas we can easily write a function to create super ellipses given a, b and n.

The full code for this mini program can be found here:
https://github.com/l-paz91/principles-practice/blob/master/Chapter%2012/Exercise%2012/SuperEllipseProgram

Now for what doesn't really seem like the hard part anymore. The closed_polyline contains a vector of points. We can just choose to select every N points or so and add those points to another polyline.


This one took me a good 3+ hours but once I got into it I think it was one of the most enjoyable. As much as I love the command window it's so nice to be drawing things and doing cool shit with maths. I'm really sad.

No comments:

Post a Comment