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.
Learn more about bidirectional Unicode characters
// D3.select allows for DOM manipulation | |
// Here we get the SVG in our index.html and then get it's width and height | |
var svg = d3.select("svg"), | |
width = svg.attr("width"), | |
height = svg.attr("height"); | |
// Here we create a the hover over 'tooltip' - note it is an element of the body and not the SVG | |
var div = d3.select("body") | |
.append("div") | |
.attr("class", "tooltip") |
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.
Learn more about bidirectional Unicode characters
//EXAMPLE 1: A Closure - A function within a function (nested) | |
function closureOne(config) { | |
return function() { | |
console.log(config.name); | |
} | |
} | |
config = { | |
'name': 'Joe Blogs' | |
} |
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.
Learn more about bidirectional Unicode characters
import networkx as nx | |
#Create the empty directed graph | |
G = nx.DiGraph() | |
#Add the edges with a weight proportional to their percentage expressed as a decimal | |
#For consistency we're using the ISO-Codes | |
for country in countries: | |
for partner in countries[country].exporting_partners: | |
G.add_edge(names_to_code[countries[country].name], names_to_code[partner], weight=countries[country].exporting_partners[partner]/100) |
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.
Learn more about bidirectional Unicode characters
countries = {} | |
#For all of the country codes | |
for code in code_to_continent: | |
#Get the JSON Data | |
url = 'https://raw.githubusercontent.com/factbook/factbook.json/master/{continent}/{code}.json'.format(continent=code_to_continent[code], code=code) | |
json = requests.get(url).json() | |
if 'Country name' in json['Government']: | |
#We might miss a few if they don't have this | |
if 'conventional short form' in json['Government']['Country name']: |
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.
Learn more about bidirectional Unicode characters
#A simple country class to store the data relating to the country | |
class Country: | |
def __init__(self, name, code): | |
self.name = name.lower() | |
self.code = code | |
#The country being exported to and the percentage | |
self.exporting_partners = {} | |
def add_partner(self, p, v): | |
self.exporting_partners[p] = v | |
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.
Learn more about bidirectional Unicode characters
import requests | |
from bs4 import BeautifulSoup | |
import json | |
#Accessing the Webpage | |
result = requests.get('https://github.com/factbook/factbook.json') | |
#Getting the content of the webpage | |
content = result.content |
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.
Learn more about bidirectional Unicode characters
//Our global variables | |
let earth; | |
let moon; | |
let r = 225; | |
function setup() { | |
createCanvas(640, 640); | |
//Creating our Earth and Moon | |
earth = new Planet(createVector(width/2, height/2), 1000, 100, 'blue'); | |
//Notice we're starting the moon directly 'east' to our Earth, so the initial velocity is straight upwards. |
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.
Learn more about bidirectional Unicode characters
function centripetalForce(satellite, centre) { | |
//Get the direction of the force | |
let force = p5.Vector.sub(centre, satellite.position).normalize(); | |
//Get the distance to centre of rotation from our satellite | |
let radius = p5.Vector.sub(centre, satellite.position).mag(); | |
//The velocity squared | |
let velocitySquared = p5.Vector.dot(satellite.velocity, satellite.velocity); | |
//Pull it all together with the f = (m * v^2) / r equation | |
force.mult(velocitySquared); | |
force.mult(satellite.mass); |
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.
Learn more about bidirectional Unicode characters
class Satellite extends Planet { | |
constructor(position, mass, radius, color, vel) { | |
//Telling Planet to build a base planet for us | |
super(position, mass, radius, color); | |
//Redefining the velocity to be something we pass it (shadowing) | |
this.velocity = vel; | |
//Our new variable acceleration | |
this.acceleration = createVector(0, 0); | |
} | |
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.
Learn more about bidirectional Unicode characters
class Planet { | |
//The 'build' method | |
constructor(position, mass, radius, color) { | |
this.position = position; | |
//A p5 Vector | |
this.velocity = createVector(0, 0); | |
this.mass = mass; | |
this.radius = radius; | |
this.color = color; | |
} |