xxxxxxxxxx
111
class Board {
constructor(spacing) {
this.frames = 0;
this.spacing = spacing;
this.w = int(width/this.spacing)-1;
this.h = int(height/this.spacing)-2;
this.dotColors = [color(0,0,0),color(0,135,0),
color(255,0,255)];
this.makeLattice(this.w,this.h);
this.polygon = new Polygon(this.w,this.h,4);
}
getMouse() {
let x = int(mouseX/this.spacing+0.5)-1;
let y = int(mouseY/this.spacing+0.5)-3;
return createVector(x,y); // mouse mapped to grid
}
mousePressed() {
this.frames = 0;
this.polygon.clicked(this.getMouse());
}
mouseReleased() {
this.frames = 0;
this.polygon.released();
}
doubleClicked() {
let m = this.getMouse();
let vs = this.polygon.vertices
for (let i in vs){
if (vs[i].x == m.x && vs[i].y == m.y && vs.length > 3) {
this.polygon.vertices.splice(i,1);
return;
}
}
}
makeLattice (w,h){
this.lattice = [];
for (let i=0;i<w;i++){
for (let j=0;j<h;j++){
this.lattice.push(new Point(i,j,color(0,0,0),8));
}
}
}
showLattice() {
for (let p of this.lattice) {
noStroke();
if (this.polygon.isVertex(p.x,p.y)) {
stroke(0);
strokeWeight(2);
}
p.show(this.spacing);
}
}
update() {
this.boundary = 0;
this.interior = 0;
for (let p of this.lattice){
if (this.polygon.isBoundary(p.x,p.y)) {
p.color = color(215,0,215);
p.size = 25;
this.boundary++;
} else if (this.polygon.isInterior(p.x,p.y)) {
p.color = color(0,130,0);
p.size = 18;
this.interior++;
} else {
p.color = color(0);
p.size = 8;
}
}
this.polygon.update(this.getMouse());
}
displayMenu() {
fill(200);
stroke(0);
strokeWeight(1);
rect(0,0,width,120);
textAlign(CENTER);
textSize(50);
fill(0);
noStroke();
text("Pick's Polygons",width/2,65);
textSize(14);
text('boundary points',85,110);
textSize(50);
fill(215,0,215);
if (this.boundary) text(this.boundary,85,70);
fill(0);
textSize(14);
text('interior points',width-85,110);
textSize(50);
fill(0,130,0);
text(this.interior,width-85,70);
}
show() {
this.displayMenu();
translate(this.spacing,this.spacing*3);
this.polygon.show(this.spacing);
this.showLattice();
}
}