xxxxxxxxxx
164
let reset;
let randoms = [0, 1, 2];
let points = 0;
let punkter = [];
let numb = [0, 1, 2];
let func = numb;
let tempn2 = [0, 1, 2];
let inputs = [];
let s = 1;
let O = [0, 0];
let a = [0, 0, 0, 0, 0, 0, 0];
let b = 0;
let r = [];
let rm = 0;
let tt = 0;
let rt = 0;
let rv = 0;
function setup() {
createCanvas(1190, 700);
s = height / 20;
O = [width / 2, height / 2];
reset = createButton("Gæt");
reset.mousePressed(resets);
punkter.push(new vector([0, 0,0]));
punkter.push(new vector([0, 0,0]));
punkter.push(new vector([0, 0,0]));
for (let i = 0; i < 7; i++) {
inputs.push(createInput(""));
}
rand();
inputs[0].style("background-color", "blue");
inputs[0].style("color", "white");
inputs[3].style("background-color", "red");
inputs[3].style("color", "white");
inputs[1].style("background-color", "red");
inputs[1].style("color", "white");
inputs[4].style("background-color", "green");
inputs[4].style("color", "white");
inputs[2].style("background-color", "green");
inputs[2].style("color", "white");
inputs[5].style("background-color", "blue");
inputs[5].style("color", "white");
inputs[6].style("background-color", "purple");
inputs[6].style("color", "white");
for (let i = 0; i < 7; i++) {
inputs[i].size(150);
}
}
function draw() {
background(220);
stroke("grey");
for (let i = 1; i < width / s; i++) {
line(i * s, 0, i * s, height);
}
for (let i = 1; i < height / s; i++) {
line(0, i * s, width, i * s);
}
strokeWeight(2);
stroke(0);
line(width / 2, 0, width / 2, height);
line(0, height / 2, width, height / 2);
strokeWeight(1);
fill("purple")
beginShape()
vertex(O[0],O[1])
vertex(O[0]+punkter[0].mag()*s, O[1])
vertex(O[0]+punkter[0].mag()*s+s*punkter[1].mag()*cos(ang(punkter[0],punkter[1])),O[1]-s*punkter[1].mag()*sin(ang(punkter[0],punkter[1])))
vertex(O[0]+s*punkter[1].mag()*cos(ang(punkter[0],punkter[1])),O[1]-s*punkter[1].mag()*sin(ang(punkter[0],punkter[1])))
vertex(O[0],O[1])
endShape()
text("Point:" + points, 10, 20);
strokeWeight(3);
noFill();
stroke("blue");
line(O[0],O[1],O[0]+punkter[0].mag()*s, O[1]);
stroke("red");
line(O[0],O[1],O[0]+s*punkter[1].mag()*cos(ang(punkter[0],punkter[1])),O[1]-s*punkter[1].mag()*sin(ang(punkter[0],punkter[1])));
stroke('green')
arc(
O[0],O[1],50,50,
-ang(punkter[0],punkter[1]),
0
);
strokeWeight(1);
}
function resets() {
if (
inputs[2].value() == str(punkter[0].dot(punkter[1])) &&
inputs[3].value() == str(punkter[2].x[0]) &&
inputs[4].value() == str(punkter[2].x[1])&&
inputs[5].value() == str(punkter[2].x[2])&&
abs(float(inputs[6].value())-punkter[2].mag())<0.1
) {
points++;
} else {
points--;
}
rand();
}
function coord(x) {
return [x.x[0] * s + O[0], O[1] - x.x[1] * s];
}
class vector {
constructor(x) {
this.x = x;
this.a = 0;
}
add(v) {
return new vector([this.x[0] + v.x[0], this.x[1] + v.x[1],this.x[2]+v.x[2]]);
}
mult(k) {
return new vector([this.x[0] * k, this.x[1] * k,this.x[2]*k]);
}
sub(v) {
return this.add(v.mult(-1));
}
co() {
return new vector([-this.x[1], this.x[0]]);
}
mag() {
return sqrt(sq(this.x[0]) + sq(this.x[1])+sq(this.x[2]));
}
outside() {
return (
this.x[0] < 0 || this.x[0] > width || this.x[1] < 0 || this.x[1] > height
);
}
dot(v) {
return this.x[0] * v.x[0] + this.x[1] * v.x[1]+this.x[2]*v.x[2];
}
det(v) {
return this.dot(v.co());
}
}
function ang(v, w) {
return acos(v.dot(w)/v.mag()/w.mag())
}
function cross(v,w){
return new vector([v.x[1]*w.x[2]-v.x[2]*w.x[1],v.x[2]*w.x[0]-v.x[0]*w.x[2],v.x[0]*w.x[1]-v.x[1]*w.x[0]])
}
function linje(v, w) {
line(coord(v)[0], coord(v)[1], coord(w)[0], coord(w)[1]);
}
function dis(v, w) {
return sqrt(sq(v.x[0] - w.x[0]) + sq(v.x[1] - w.x[1]));
}
function sign(x) {
return x / abs(x);
}
function rand() {
r = random(randoms);
punkter[0].x = [round(32 * (random() - 0.5)), round(20 * (random() - 0.5)),round(20 * (random() - 0.5))];
punkter[1].x = [round(32 * (random() - 0.5)), round(20 * (random() - 0.5)),round(20 * (random() - 0.5))];
punkter[2].x=cross(punkter[0],punkter[1]).x
a[0] = punkter[0].dot(punkter[1]);
rt = random([0, 1, 2]);
rv = random([3, 4, 5]);
for (let i = 0; i < 7; i++) {
inputs[i].value("");
}
inputs[0].value("("+punkter[0].x[0]+","+punkter[0].x[1]+","+punkter[0].x[2]+")");
inputs[1].value("("+punkter[1].x[0]+","+punkter[1].x[1]+","+punkter[1].x[2]+")");
}