xxxxxxxxxx
270
class punkt{
constructor(x,y){
this.x=x
this.y=y
}
tegn(){
strokeWeight(5)
point(xCoord(this.x),yCoord(this.y))
strokeWeight(1)
}
}
class funktion{
constructor(a,b){
this.a=a
this.b=b
}
værdi(x){
if(e==0){
return this.a*x+this.b
} else if(e==1){
return this.b*pow(this.a,x)
}
}
graf(){
if(e==0){
line(0,yCoord(this.værdi(-O[0]/xZoom)),w,yCoord(this.værdi((w-O[0])/xZoom)))
} else if(e==1){
let j=0
while(yCoord(this.værdi((j+1-O[0])/xZoom))>0){
line(j,yCoord(this.værdi((j-O[0])/xZoom)),j+1,yCoord(this.værdi((j+1-O[0])/xZoom)))
j++
}
}
}
regina(a) {
if (a.length > 1) {
let sX = 0;
let sY = 0;
let sXY = 0;
let sXX = 0;
for (let i = 0; i < a.length; i++) {
sX += a[i].x;
sY += a[i].y;
sXY += a[i].x * a[i].y;
sXX += sq(a[i].x);
}
this.a = (a.length * sXY - sX * sY) / (a.length * sXX - sq(sX));
this.b = (sXX * sY - sX * sXY) / (a.length * sXX - sq(sX));
if(e==1){
this.a=exp(this.a)
this.b=exp(this.b)
}
}
}
res(a) {
let b = [];
for (let i = 0; i < a.length; i++) {
b.push(a[i].y - this.værdi(a[i].x));
}
return b;
}
rSq(a) {
let Sr = 0;
let Sy = 0;
let My = 0;
for (let i = 0; i < a.length; i++) {
Sy += a[i].y;
}
My = Sy / a.length;
Sy = 0;
for (let i = 0; i < a.length; i++) {
Sr += sq(this.res(a)[i]);
Sy += sq(a[i].y - My);
}
return 1 - Sr / Sy;
}
}
let xIn;
let yIn;
let uXin;
let uYin;
let sXm = 0;
let sYm = 0;
let e = 0;
let p = 0;
let nE = 0;
let nP = 0;
let tX = 40;
let tY = 0;
let t1w = 50;
let t2w = 40;
let m = 20;
let PmiX;
let PmaX;
let rP=200
let w=900
let h=550
let P=[[],[]]
let eP=[]
let xZoom=20
let yZoom=20
let O=[w/2,h/2]
let f=new funktion(4,9)
function setup() {
createCanvas(w, h+rP);
xIn = createInput("");
xIn.position(tX, tY + 2 * m);
xIn.size(t1w);
yIn = createInput("");
yIn.position(tX, tY + 20 + 2 * m);
yIn.size(t1w);
uXin = createInput("");
uXin.position(tX + t1w, tY + 2 * m);
uXin.size(t2w);
uYin = createInput("");
uYin.position(tX + t1w, tY + 20 + 2 * m);
uYin.size(t2w);
}
function draw() {
background(220);
textSize(20);
text("Funktionsforskfriften er", tX + t1w + t2w + 10, tY + 20 + 2 * m);
//if(keyIsDown(69)){e=1}
if(keyIsDown(76)){e=0}
if(keyIsDown(69)){e=1}
if (keyIsDown(13) && xIn.value() != "" && yIn.value() != "") {
P[0].push(new punkt(float(xIn.value()), float(yIn.value())));
P[1].push(new punkt(float(xIn.value()), float(log(yIn.value()))));
PmiX = min(PmiX, float(xIn.value()));
PmaX = max(PmaX, float(xIn.value()));
xIn.value("");
yIn.value("");
}
f.regina(P[e])
if (keyIsDown(RIGHT_ARROW)) {
if (keyIsDown(16)) {
xZoom = xZoom * 1.01;
O[0]=-w/2*0.01+O[0]*1.01
} else {
O[0] += 1;
}
}
if (keyIsDown(LEFT_ARROW)) {
if (keyIsDown(16)) {
xZoom = xZoom / 1.01;
O[0]=w/2*0.01/1.01+O[0]/1.01
} else {
O[0] -= 1;
}
}
if (keyIsDown(UP_ARROW)) {
if (keyIsDown(16)) {
yZoom = yZoom * 1.01;
O[1]=-h/2*0.01+O[1]*1.01
} else {
O[1] -= 1;
}
}
if (keyIsDown(DOWN_ARROW)) {
if (keyIsDown(16)) {
yZoom = yZoom *0.99;
O[1]=h/2*0.01/1.01+O[1]/1.01//(O[1]-C)*0.99+C
} else {
O[1] += 1;
}
}
for(let i=0;i<P[e].length;i++){
P[0][i].tegn()
text("(" + P[e][i].x + "," + P[e][i].y + ")", 2 * m, 2 * m + 20 * (i + 3));
strokeWeight(5);
point(
m + ((w - 2 * m) / (PmaX - PmiX)) * (P[e][i].x - PmiX),
h +
rP / 2 -
(((rP - m) / max(max(f.res(P[0])), -min(f.res(P[0])))) * f.res(P[0])[i]) / 2
);
strokeWeight(1);
}
if(e==0){
text(
"f(x)=" + round(f.a, 2) + "x+" + round(f.b, 2),
tX + t1w + t2w + 10,
tY + 40 + 2 * m
);
} else {
text(
"f(x)=" + round(f.b, 2) + "*" + round(f.a, 2)+"^x",
tX + t1w + t2w + 10,
tY + 40 + 2 * m
);
}
text("r^2=" + f.rSq(P[0]), tX + t1w + t2w + 10, tY + 60 + 2 * m);
line(0, h + rP / 2, w, h + rP / 2);
strokeWeight(1);
text(
"(" +
round((m - O[0]) / xZoom, 1) +
"," +
round((O[1] - h + 20) / yZoom, 1) +
")",
30,
h - 30
);
f.graf()
if (O[0] < m) {
line(m, 0, m, h);
stroke(0);
text(uYin.value(), m + 10, m + 5);
} else if (O[0] > w - m) {
line(w - m, 0, w - m, h);
stroke(0);
textAlign(RIGHT);
text(uYin.value(), w - 3 * m, m + 5);
textAlign(LEFT);
} else {
stroke(0);
line(O[0], 0, O[0], h);
text(uYin.value(), O[0] + 5, m + 5);
}
stroke(150);
if (O[1] > h - m) {
line(0, h - m, w, h - m);
stroke(0);
textAlign(RIGHT);
text(uXin.value(), w - m + 5, h - 3 * m);
textAlign(LEFT);
} else if (O[1] < m) {
line(0, m, w, m);
stroke(0);
textAlign(RIGHT);
text(uXin.value(), w - m + 5, 2 * m + 10);
textAlign(LEFT);
} else {
stroke(0);
line(0, O[1], w, O[1]);
textAlign(RIGHT);
text(uXin.value(), w - m + 5, O[1] - m * 0.5);
textAlign(LEFT);
}
stroke(150);
line(m, 0, m, h);
line(w - m, 0, w - m, h);
line(0, m, w, m);
line(0, h - m, w, h - m);
stroke(0);
}
function mousePressed() {
mX = mouseX;
mY = mouseY;
}
function mouseReleased() {
dX = mouseX - mX;
dY = mY - mouseY;
if (keyIsDown(16)) {
xZoom = xZoom * pow(1.01, dX);
O[0] = (w / 2) * (1 - pow(1.01, dX)) + O[0] * pow(1.01, dX);
yZoom = yZoom * pow(1.01, dY);
O[1] = (h / 2) * (1 - pow(1.01, dY)) + O[1] * pow(1.01, dY);
} else {
O[0] += mouseX - mX;
O[1] += mouseY - mY;
}
}
function xCoord(x){
return x*xZoom+O[0]
}
function yCoord(y){
return O[1]-y*yZoom
}