Rectangles¶
All the interesting stuff is in the JavaScript, so open up a JavaScript console and look at the console.log output. Here is the source code:
class Rect {
// construct a rectangle from two diagonal corners, each
// represented as a dictionary: {x, y}
constructor(c1,c2) {
// UL is upper left, LR is lower right
this.ulx = Math.min(c1.x, c2.x);
this.uly = Math.max(c1.y, c2.y);
this.lrx = Math.max(c1.x, c2.x);
this.lry = Math.min(c1.y, c2.y);
}
// the area method
area () {
const width = this.lrx - this.ulx;
const height = this.uly - this.lry;
return width*height;
}
}
// some testing code
var origin = {x: 0, y: 0};
var p1 = {x: 10, y: 20};
var r1 = new Rect(origin, p1);
console.log(r1);
console.log("area is "+r1.area());