# 2D Sine Wave Example Using PyOpenGL

Here is an example of a moving 2D sine wave using Python 3, PyGame, and PyOpenGL.  See a https://blog.gahooa.com/2018/02/11/pygame-and-opengl-on-windows-10/  for how to install them.

This sample program is designed to have a 100×60 unit working area with a 10 unit buffer around the edges.  You can see the axis in the lower-left (0,0) where Y+ is up, and X+ is to the right.

The structure of the program was created to make it super easy to work on the “guts” of the graphics without getting it confused with the “bookkeeping” end of OpenGL or PyGame.

Note: the glOrtho() command is how 2D “parallel perspective” is setup.  It defines the left, right, bottom, top, near plane, and far plane.  Because it is parallel, there is not the notion of a “camera” per-se, but rather section of the plane that should be viewed.  Documented here:

https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glOrtho.xml

Here is the code!

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.

 #!/usr/bin/env python ############################################################################### # Action Happens Here 50 times per second def tick(i): #glRotatef(1, 0, 0, 1) #glTranslatef(0, 0, 1) # Draw Axis axis(i) # Draw sinewave for x in range(200): x = x/2.0 y = math.sin(math.radians(x+i) * 10) * 30 + 30 cquad((x,y,0), 1, (y/60.0,0,x/100.0)) #(center, diameter, color) ############################################################################### # The rest of this is the bones that make it work import time import pygame from pygame.locals import * from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.arrays import vbo import math FPS_TARGET = 50 def axis(i): glBegin(GL_LINES) #x = red #y = green #z = blue glColor3f(1, 0, 0) glVertex3fv((0, 0, 0)) glVertex3fv((1, 0, 0)) glColor3f(0, 1, 0) glVertex3fv((0, 0, 0)) glVertex3fv((0, 1, 0)) glColor3f(0, 0, 1) glVertex3fv((0, 0, 0)) glVertex3fv((0, 0, 1)) glEnd() def quad(points, color): glBegin(GL_QUADS) glColor3f(*color) for p in points: glVertex3fv(p) glEnd() def cquad(point, size, color): glBegin(GL_QUADS) glColor3f(*color) x,y,z = point s = size/2.0 glVertex3fv((x–s,y–s,z)) glVertex3fv((x+s,y–s,z)) glVertex3fv((x+s,y+s,z)) glVertex3fv((x–s,y+s,z)) glEnd() def main(): #initialize pygame and setup an opengl display pygame.init() pygame.display.set_mode((1200,800), OPENGL|DOUBLEBUF) glEnable(GL_DEPTH_TEST) #use our zbuffer glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); #setup the camera glMatrixMode(GL_PROJECTION) #gluPerspective(45.0,1000/1000,0.1,1000.0) #setup lens #glOrtho(-10,10,-10,10,1,20) glOrtho(–10,110,–10,70,–1,1) #glTranslatef(0, 0, -100) #move back #glRotatef(-20, 1, 0, 0) #orbit higher nt = int(time.time() * 1000) for i in range(2**63): nt += 1000//FPS_TARGET #check for quit'n events event = pygame.event.poll() if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE): break glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) tick(i) pygame.display.flip() ct = int(time.time() * 1000) pygame.time.wait(max(1,nt – ct)) if i % FPS_TARGET == 0: print(nt–ct) if __name__ == '__main__': main()

view raw

sinewave.py

hosted with ❤ by GitHub

# How to Calculate Pi

π (pi) is the ratio between a circle’s circumference and it’s diameter. You can check this by using a tape measure. Measure around a circle. Then measure across the circle.

π = Around / Across

Did you know there is a way to calculate π? There are a lot of different ways, but one is called the Leibniz formula for π.

http://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80

Where the original Leibniz formula for π ends up calculating π/4, I’ve just factored this 4 into the infinite series.

π = 4/1 – 4/3 + 4/5 – 4/7 + 4/9 – 4/11 + 4/13 – 4/15 + 4/17 – … (forever)

The more terms you add/subtract to it, the closer it gets to being accurate. A problem with the Leibniz formula for π is that it takes a lot of calculations to get an accurate version of pi.

Here is a mini-program I wrote in Python 3 to repeat this one million times.

pi = 0
for n in range(1000000):
pi += ((-1)**n*4) / (2*n+1)
print(pi)

Here are some of the numbers from that calculation:
4.00
2.66
3.46
2.89
3.33
2.97
3.28
3.01
3.25
3.04
3.23
3.05
3.21
3.07
3.20
3.07
3.20
3.08
3.19
3.09
3.18
…999980 more times…
3.1415916535897

Here is real PI:
3.1415926535897

Isn’t it interesting that my version of PI, after a million iterations, is 1 digit off of real Pi, but the digit is in the middle? This has something to do with Euler numbers which you can read about at http://en.wikipedia.org/wiki/Euler_number .

For most practical purposes, 3.14159 is more than enough digits to use with Pi.

Ever wonder if you could use Pie to calculate Pi?
http://www.numberphile.com/videos/pie_with_pies.html

# Programming

Hi, this is Eli.  One of our school assignments is to completely learn our math tables by heart.   We wrote a python program to help us practice with this.  This is a program for multiplication tables 1-12.

Ezra and I are learning how to program in python.  Dad helped us get the structure of this program right, but we entered into IDLE and debugged it ourselves.

Here is an example of the program in action:

When you activate the program it  asks you how many problems you want – you can go from 1 to 144 problems.

When you are done with your problems it tells you how long you took and how many problems you got right and how many problems you got right and how long it took you to answer each problem.

That’s all for now!

# Stratahex Game

We’ve invented a new board game, and it’s a lot of fun.  Stratahex combines resource collection, transportation, logistics, planning, defense, offense, warfare, and more into a simple and easy-to-play boardgame.

The basic game pieces are:

2. Supply Trucks
3. Oil Rigs
4. Small Tanks
5. Artillery
6. Standard Ammo
7. High Explosive Ammo
8. Coin

The board consists of 3 concentric circles with a hex-shaped hole pattern.  Parts of the board are painted light-gray, medium-gray, and dark-gray.

• Oil rigs placed in light-gray recieve one coin per turn.
• Oil rigs placed in medium-gray receive two coin per turn.
• Oil rigs placed in dark-gray receive four coin per turn

You can only spend coin once it has been transported back to your base in a supply truck.  Each piece costs coin to purchase.  You have a limited number of moves per supply-truck and per weapon each turn.

Stratahex is a very comprehensive, but still simple, game to play.  The older kids, 8 and 10, have no trouble.  There is a lot of figuring, math, counting, and changing coin.

Gameplay is predictable, like chess, in that you can determine the possible moves of the opponent before his next turn.  There is no luck involved.

Plenty of fun!

# Home School Software: Inventory Tracking

Ever lose track of how much flour, sugar, milk, eggs, soap, razors, spices, motor oil, filters, nails, screws, glue, paper, staples, etc… you have on hand?

Ever shop based on hunger, rather than diciplined restocking?

Ever want to have an inventory of groceries at your house, so you don’t have to run to the store for everything?

Our objective is to keep consistent inventory levels of common products around the house, so we never run out of things that we should have on hand.  To that end, I’ve been adding some basic household inventory tracking data to the Home School software.  It has (or will have) the following features:

• Areas — places that you store products (freezer, basement shelves, kitchen, bathroom, etc…)
• Items — each “thing” that you want to keep in a given area (flour, sugar, toothpaste, etc)
• Units — lbs, each, bottle, roll, case, etc…
• Min Quantity — minimum amount to have on hand
• Max Quantity — max quantity to have on hand
• Check every [] days — how often should this item be checked?

From that data, you will be able to pull the following information out of the system:

• All items and current inventory levels
• Graphs of inventory levels over time
• Which items (sorted by area) need checked now
• What needs refilled (eg, the container of sugar in the kitchen, from the big bags in the basement)
• What needs purchased (40 eggs, 24 rolls of TP, 10 lbs of sugar)

You will be able to pop on, print out a list of items that need inventoried, and hand it to the kids with a pencil and clipboard to go about filling out how much there actually is.  Excellent math practice, especially if you deal in raw units like oz, lbs, quarts, etc… — the students will have to add/multiply/convert the units that are on the items they are counting.

For Example, you could specify that you want 4 bottles of dish soap around, or 96 ounces of dish soap around.  The difference is that they will need to multiply 24 oz per bottle * 3 bottles on hand = 72 ounces — time to buy another bottle.

Here are some early screen shots:

# A big measuring tape and a right triangle

Just a good picture of Eli (with Ezra and Anna in the background) holding a 300′ measuring tape.

We needed to make a right angle, so I explained to them that a triangle with the sides

3, 4, 5

will be a 100% right triangle.  So we measured 15′, 20′, and 25′ around three stakes, and that is how we got our right angle :)

For anyone who has been out of high-school long enough to forget, the equation is:

A*A + B*B = C*C

That is…

3*3 + 4*4 = 5*5
9 + 16 = 25
25=25
True

See more about Pythagoras and his Pythagorean Theorem at Wikipedia…

# Interesting Numeric Pattern

```>>> for i in range(1,31):
...     print('  ' + '1'*30)
...     print('x ' + '1'*i)
...     print('= ' + str(int('1'*30) * int('1'*i)))
...     print()
...
111111111111111111111111111111
x 1
= 111111111111111111111111111111

111111111111111111111111111111
x 11
= 1222222222222222222222222222221

111111111111111111111111111111
x 111
= 12333333333333333333333333333321

111111111111111111111111111111
x 1111
= 123444444444444444444444444444321

111111111111111111111111111111
x 11111
= 1234555555555555555555555555554321

111111111111111111111111111111
x 111111
= 12345666666666666666666666666654321

111111111111111111111111111111
x 1111111
= 123456777777777777777777777777654321

111111111111111111111111111111
x 11111111
= 1234567888888888888888888888887654321

111111111111111111111111111111
x 111111111
= 12345678999999999999999999999987654321

111111111111111111111111111111
x 1111111111
= 123456790111111111111111111110987654321

111111111111111111111111111111
x 11111111111
= 1234567901222222222222222222220987654321

111111111111111111111111111111
x 111111111111
= 12345679012333333333333333333320987654321

111111111111111111111111111111
x 1111111111111
= 123456790123444444444444444444320987654321

111111111111111111111111111111
x 11111111111111
= 1234567901234555555555555555554320987654321

111111111111111111111111111111
x 111111111111111
= 12345679012345666666666666666654320987654321

111111111111111111111111111111
x 1111111111111111
= 123456790123456777777777777777654320987654321

111111111111111111111111111111
x 11111111111111111
= 1234567901234567888888888888887654320987654321

111111111111111111111111111111
x 111111111111111111
= 12345679012345678999999999999987654320987654321

111111111111111111111111111111
x 1111111111111111111
= 123456790123456790111111111110987654320987654321

111111111111111111111111111111
x 11111111111111111111
= 1234567901234567901222222222220987654320987654321

111111111111111111111111111111
x 111111111111111111111
= 12345679012345679012333333333320987654320987654321

111111111111111111111111111111
x 1111111111111111111111
= 123456790123456790123444444444320987654320987654321

111111111111111111111111111111
x 11111111111111111111111
= 1234567901234567901234555555554320987654320987654321

111111111111111111111111111111
x 111111111111111111111111
= 12345679012345679012345666666654320987654320987654321

111111111111111111111111111111
x 1111111111111111111111111
= 123456790123456790123456777777654320987654320987654321

111111111111111111111111111111
x 11111111111111111111111111
= 1234567901234567901234567888887654320987654320987654321

111111111111111111111111111111
x 111111111111111111111111111
= 12345679012345679012345678999987654320987654320987654321

111111111111111111111111111111
x 1111111111111111111111111111
= 123456790123456790123456790110987654320987654320987654321

111111111111111111111111111111
x 11111111111111111111111111111
= 1234567901234567901234567901220987654320987654320987654321

111111111111111111111111111111
x 111111111111111111111111111111
= 12345679012345679012345679012320987654320987654320987654321```