xxxxxxxxxx
67
class punkt{
constructor(x,y){
this.x=x
this.y=y
}
place(){
strokeWeight(5)
point(O[0]+skalX*this.x,O[1]-skalY*this.y)
}
}
class lin{
constructor(a,b){
this.a=a
this.b=b
}
værdi(x){
return this.a*x+this.b
}
graf(){
strokeWeight(1)
line(0,O[1]-this.værdi(-b/2/skalX)*skalY,b,O[1]-this.værdi(b/2/skalX)*skalY)
}
reg(p){
let sXY=0
let sX=0
let sY=0
let sXX=0
for(let i=0;i<p.length;i++){
sXY+=p[i].x*p[i].y
sX+=p[i].x
sY+=p[i].y
sXX+=sq(p[i].x)
}
this.a=(p.length*sXY-sX*sY)/(p.length*sXX-sq(sX))
this.b=(sXX*sY-sX*sXY)/(p.length*sXX-sq(sX))
}
}
let P=[]
let skalX=50
let skalY=50
let b=800
let h=700
let O=[b/2,h/2]
let f
function setup() {
createCanvas(b, h);
P0=new punkt(1.5,1.1)
P1=new punkt(3.4,2.5)
P2=new punkt(4.2,3.2)
P3=new punkt(5.9,4.4)
P.push(P0)
P.push(P1)
P.push(P2)
P.push(P3)
f=new lin(2,4)
}
function draw() {
background(220);
for(let i=0;i<P.length;i++){
P[i].place()
}
f.graf()
f.reg(P)
}