Given:
Compute: updated configuration
How?
Pick a random number:
Draw rules:
Simulate execution:
Numerical values are Number
s
Example: 4 == 4.0
?
So 4 / 2
is the same is 4.0 / 2
What about 5 / 2
?
Math.floor()
Math.floor(5 / 2)
gives 2
HTML + CSS + JavaScript
This is all good for text-based documents
Course Goal: Visualizations of algorithmic processes
Connection: visualize the states of objects as computation progresses
Scalable Vector Graphics
png
, jpg
, tiff
).svg
or embedded in HTMLCreate an SVG element with <svg>
tag:
<svg width="600" height="400" xmlns="http://www.w3.org/2000/svg">
...
</svg>
Must specify width
, height
, and xmlns
xmlns
is “xml namespace”, used to avoid naming conflicts with other types of XML (e.g., HTML)<rect width="100%" height="100%"
fill="white" stroke="black" stroke-width="5"/>
<line x1="100" y1="100" x2="500" y2="300"
stroke="black" stroke-width="5"/>
<circle cx="300" cy="200" r="100" stroke="black"
fill="pink" stroke-width="5"/>
<polygon points="100 100 300 100 400 200 500 300 200 350"
stroke="black" stroke-width="5" fill="pink"/>
<path d="M120,230 Q220,0 400,100 T300,300"
stroke="black" stroke-width="5" fill="transparent"/>
Draw This (or something better)
All circle
s have same radius
, stroke
, stroke-width
, fill
circle {
r: "25";
fill: "pink";
stroke: "black";
stroke-width: "5";
}
Now must only specify the location of each circle!
SVG elements can be given class
and id
just like HTML elements!
<circle cx="300" cy="200" class="dot" id="special-dot"/>
<circle cx="120" cy="300" class="dot"/>
<circle cx="40" cy="40" class="dot"/>
<circle cx="260" cy="260" class="dot"/>
<circle cx="350" cy="85" class="dot"/>
.dot {
r: 25;
fill: pink;
stroke: black;
stroke-width: 5;
}
#special-dot {
fill: red;
}
SVG can be accessed and modified with JavaScript!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Dots!</title>
<link rel="stylesheet" href="style.css">
<script src="dots.js" defer></script>
</head>
<body>
<div id="root">
<h1>Dots!</h1>
<svg id="dots"
width="600" height="400"
xmlns="http://www.w3.org/2000/svg">
<rect id="dot-background" width="100%" height="100%"/>
</svg>
<button onclick="moveDots()">Move Dots!</button>
</div>
</body>
</html>
HTML contains:
<svg id="dots"
width="600" height="400"
xmlns="http://www.w3.org/2000/svg">
<rect id="dot-background" width="100%" height="100%"/>
</svg>
Slightly different that html elements
const ns = 'http://www.w3.org/2000/svg';
const svg = document.querySelector('#dots');
let circle = document.createElementNS(ns, 'circle');
svg.appendChild(circle);
Again, different from HTML
let circle = document.createElementNS(ns, 'circle');
circle.setAttributeNS(null, 'cx', this.cx);
circle.setAttributeNS(null, 'cy', this.cy);
circle.setAttributeNS(null, 'class', 'dot');
svg.appendChild(circle);
.dot {
r: 10px;
fill: rgb(50, 120, 255);
stroke: black;
stroke-width: 2;
}
Collection of
Example dot
class
cx
x position of centercy
y position of centerupdateLocation(cx, cy)
moves dot to a new locationIn JS, object types can be defined by defining a constructor
this
defines attributes and methodsBy convention, constructor names are Capitalized:
function Dot(cx, cy) {
this.cx = cx;
this.cy = cy;
this.circle = document.createElementNS(ns, 'circle');
this.circle.setAttributeNS(null, 'cx', this.cx);
this.circle.setAttributeNS(null, 'cy', this.cy);
this.circle.setAttributeNS(null, 'class', 'dot');
svg.appendChild(this.circle);
}
let someDot = new Dot(100,100);
let anotherDot = new Dot(200,200);
dots = []; // an array of dots
function makeDots() {
for(let i = 0; i < 10; i++) {
let x = Math.floor(600 * Math.random());
let y = Math.floor(400 * Math.random());
dots.push(new Dot(x, y));
}
}
You can include method definitions in the constructor as well!
function Dot(cx, cy) {
...
this.updateLocation = function (cx, cy) {
this.cx = cx;
this.cy = cy;
this.circle.setAttributeNS(null, 'cx', this.cx);
this.circle.setAttributeNS(null, 'cy', this.cy);
};
}
dot
s arounddots = [];
//...create dots...
function moveDots() {
for(let i = 0; i < 10; i++) {
let x = Math.floor(600 * Math.random());
let y = Math.floor(400 * Math.random());
dots[i].updateLocation(x, y);
}
}