Lecture Notes 02
This lecture notes is still work in progress. Not all programs covered in the class are added here yet. The remaining ones will be added soon!
Introduction
Why should you learn programming?
A powerful programming language is more than just a means for instructing a computer to perform tasks. The language also serves as a framework within which we organize our ideas about processes.
– Structure and Interpretation of Computer Programs
Every powerful language has three mechanisms for accomplishing this:
- primitive expressions, which represent the simplest entities the language is concerned with,
- means of combination, by which compound elements are built from simpler ones, and
- means of abstraction, by which compound elements can be named and manipulated as units.
– Structure and Interpretation of Computer Programs
Diving in
A Circle
#include <sketch.h>
int main()
{
// draw a circle with center as (0, 0) and radius 100
(0, 0, 100);
draw_circle
// save all the shapes draws to lesson2.svg
("lesson2.svg");
save_sketch}
Two Circles
How do we draw two circles instead of just one?
#include <sketch.h>
int main()
{
(-100, 0, 100);
draw_circle(100, 0, 100);
draw_circle
("lesson2.svg");
save_sketch}
Concentric Circles
How about drawing two concentic circles?
#include <sketch.h>
int main()
{
(0, 0, 200);
draw_circle(0, 0, 100);
draw_circle
("lesson2.svg");
save_sketch}
What if we want three circles?
#include <sketch.h>
int main()
{
float r = 150;
(0, 0, r / 3);
draw_circle(0, 0, 2 * r / 3);
draw_circle(0, 0, r);
draw_circle
("lesson2.svg");
save_sketch}
Candle Lights
#include <sketch.h>
void bottle_circle(float bx, float by, float r)
{
float cx = bx;
float cy = by + r;
(cx, cy, r);
draw_circle}
void draw_candle_light(float x, float y, float r)
{
(x, y, r / 3);
bottle_circle(x, y, 2 * r / 3);
bottle_circle(x, y, r);
bottle_circle}
int main()
{
(-100, 0, 100);
draw_candle_light(100, 0, 100);
draw_candle_light
("lesson2.svg");
save_sketch}
Concentric Circles
#include <sketch.h>
void concentric_circles(float x, float y, float r, float n)
{
for (int i = 1; i <= n; i++)
{
(x, y, i * r / n);
draw_circle}
}
int main()
{
(0, 0, 250, 10);
concentric_circles
("lesson2.svg");
save_sketch}
Random Concentric Circles
#include <sketch.h>
#include <stdlib.h>
#include <time.h>
void random_concentric_circles(float x, float y, int r, float n)
{
for (int i = 1; i <= n; i++)
{
// pick a random number from 1 to r
float r1 = 1 + rand() % r;
(x, y, r1);
draw_circle}
}
int main()
{
// initialize the random number generator
(time(NULL));
srand
(0, 0, 250, 10);
random_concentric_circles
("lesson2.svg");
save_sketch}
Try running this program again and you’ll get completely different circles!
String Art
Let’s add a bit more complexity. How do you find N points equidistant on a circle?
Yes, trigonometry!
#include <sketch.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#define N 36
float X[N];
float Y[N];
// initialze the X and Y for N points
// on circle of radius r
void make_circle(float r)
{
for (int i = 0; i < N; i++)
{
float theta = 2 * M_PI / N * i;
[i] = r * cos(theta);
X[i] = r * sin(theta);
Y(X[i], Y[i], 2);
draw_circle}
}
int main()
{
(250);
make_circle
("lesson2.svg");
save_sketch}
Connecting Points
What if we connect some points?
#include <sketch.h>
#include <math.h>
#define N 36
float X[N];
float Y[N];
// initialze the X and Y for N points
// on circle of radius r
void make_circle(float r)
{
for (int i = 0; i < N; i++)
{
float theta = 2 * M_PI / N * i;
[i] = r * cos(theta);
X[i] = r * sin(theta);
Y(X[i], Y[i], 2);
draw_circle}
}
void connect(int i, int j)
{
= i % N;
i = j % N;
j (X[i], Y[i], X[j], Y[j]);
draw_line}
int main()
{
(250);
make_circle
(0, 4);
connect(1, 5);
connect
("lesson2.svg");
save_sketch}
Connecting All Points
Let’s go one step further and connect all the points.
#include <sketch.h>
#include <math.h>
#define N 36
float X[N];
float Y[N];
// initialze the X and Y for N points
// on circle of radius r
void make_circle(float r)
{
for (int i = 0; i < N; i++)
{
float theta = 2 * M_PI / N * i;
[i] = r * cos(theta);
X[i] = r * sin(theta);
Y(X[i], Y[i], 2);
draw_circle}
}
void connect(int i, int j)
{
= i % N;
i = j % N;
j (X[i], Y[i], X[j], Y[j]);
draw_line}
void connect_all(int delta)
{
for (int i = 0; i < N; i++)
{
(i, i + delta);
connect}
}
int main()
{
(250);
make_circle
(10);
connect_all
("lesson2.svg");
save_sketch}
A Pattern
We could use connect_all
multiple times to generate very interesting pattern.
#include <sketch.h>
#include <math.h>
#define N 36
float X[N];
float Y[N];
// initialze the X and Y for N points
// on circle of radius r
void make_circle(float r)
{
for (int i = 0; i < N; i++)
{
float theta = 2 * M_PI / N * i;
[i] = r * cos(theta);
X[i] = r * sin(theta);
Y// draw_circle(X[i], Y[i], 2);
}
}
void connect(int i, int j)
{
= i % N;
i = j % N;
j (X[i], Y[i], X[j], Y[j]);
draw_line}
void connect_all(int delta)
{
for (int i = 0; i < N; i++)
{
(i, i + delta);
connect}
}
int main()
{
(250);
make_circle
(16);
connect_all(12);
connect_all(7);
connect_all
("lesson2.svg");
save_sketch}
Cardoid
#include <sketch.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#define N 72
float X[N];
float Y[N];
// initialze the X and Y for N points
// on circle of radius r
void make_circle(float r)
{
for (int i = 0; i < N; i++)
{
float theta = 2 * M_PI / N * i;
[i] = r * cos(theta);
X[i] = r * sin(theta);
Y// draw_circle(X[i], Y[i], 2);
}
}
void connect(int i, int j)
{
= i % N;
i = j % N;
j (X[i], Y[i], X[j], Y[j]);
draw_line}
void connect_all(int delta)
{
for (int i = 0; i < N; i++)
{
(i, i + delta);
connect}
}
int main()
{
// initialize the random number generator
(time(NULL));
srand
(250);
make_circle
for (int i = 0; i < N; i++)
{
(i, 2 * i);
connect}
("lesson2.svg");
save_sketch}
Final Touch
#include <sketch.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#define N 720
float X[N];
float Y[N];
// initialze the X and Y for N points
// on circle of radius r
void make_circle(float r)
{
for (int i = 0; i < N; i++)
{
float theta = 2 * M_PI / N * i;
[i] = r * cos(theta);
X[i] = r * sin(theta);
Y// draw_circle(X[i], Y[i], 2);
}
}
void connect(int i, int j)
{
= i % N;
i = j % N;
j (X[i], Y[i], X[j], Y[j]);
draw_line}
void connect_all(int delta)
{
for (int i = 0; i < N; i++)
{
(i, i + delta);
connect}
}
int main()
{
// initialize the random number generator
(time(NULL));
srand
(0.5);
set_stroke_width("#44444480");
set_stroke
(250);
make_circle
// connect_all(16);
// connect_all(12);
// connect_all(7);
for (int i = 0; i < N; i++)
{
int d = N / 6;
(i, (i * i) % d);
connect}
("lesson2.svg");
save_sketch}
References
- Structure and Interpretation of Computer Programs
- Curve stitching: the art of sewing beautiful mathematical patterns, Jon Millington