Showing posts with label flipflop. Show all posts
Showing posts with label flipflop. Show all posts

Thursday, 20 August 2020

Chapter 15 // Exercise 5 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and modified versions of the graphics files used throughout the chapters. You can find those versions through the link below.

Chapter 15 // Exercise 5

"Animate" (as in section 15.5) the series 1-1/3+1/5-1/7+1/9-1/11+.... It is known as Leibniz's series and converges to pi/4.

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2015/Exercise%205

This took me a while to wrap my head around and I'm still not entirely convinced at how the lambda expression works but whatever.

So pi/4 == 0.7853989 (not precisely but you have to stop somewhere)

The problem with this exercise is that each frame looks identical as it gets closer to pi/4. Probably not the best function to graph. Either that or I've completely miscalculated.

Friday, 15 May 2020

Chapter 13 // Exercise 9 - 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 13 // Exercise 9

Tile a part of the window with Regular_hexagons (use at least eight hexagons).

Github: https://github.com/l-paz91/principles-practice/tree/master/Chapter%2013/Exercise%209

With this I wasn't sure if I should give the tiling a boundary, like a 400x400 area to tile and you pass in that area into a constructor. Or if I should push a load of hexagons back into a vector and change the x/y positions with each hexagon. I decided on giving a boundary. When you tile a hexagon, there should be no spaces in-between which makes it easy to determine where the next hexagon should be.

Considering we draw the hexagon from the center with a given radius, that means on the x axis, the next hexagon center point should be 2 radii + a side length away. On the y axis, the next hexagon center point is an apothem away. The apothem is the radius of the incircle of a polygon. 

To find the side length of a polygon, use the formula:
sideLength = 2Radius * sin(PI/numOfSides);

The apothem:
apothem = radius * cos(PI/numOfSides);

Chapter 13 // Exercise 9 - Principles & Practice Using C++

To get the hexagons to draw at different x values on a new line I used a simple flipflop:

Chapter 13 // Exercise 9 - Principles & Practice Using C++

Here, flipflop is a bool set to 0. When x becomes more than the edge, the flipflop is inverted. So if flipflop is true, !flipflop is false and then !flipflop again becomes true. We want the x co-ordinate to always be either 0 or (radius + half sideLength). If you multiply an int by a bool it will either be 0 or the number you want. This reduces the amount of branching the cpu has to do.