Polynomien yhteenlasku

Polynomien yhteenlasku


Tämä ohjelma havainnollistaa polynomin yhteenlaskua algebralaattojen avulla. Ohjelman koodi on seuraava. Kopioi tämä koodi kokonaisuudessaan ohjelmointiympäristöön ja suorita koodi. Sinun ei tarvitse tehdä koodiin mitään muutoksia.

// Ohjelman tehnyt e-Oppi Oy
// 19.3.2018
int a1 = 450; 
int b1 = 450; 
int c1 = 450; 
int d1 = 450; 
int e1 = 450; 
int f1 = 450; 
void setup () {
 size(850,750);
 stroke(0); // Musta viivan väri
}

void draw () {
 background(255);
 strokeWeight(2);
 textSize(25);
 fill(255,255,0); // Keltainen täyttöväri
 rect(0,0,849,30); // Piirrä keltainen suorakulmio
 rect(0,30,849,30); // Piirrä keltainen suorakulmio
 rect(0,60,849,30); // Piirrä keltainen suorakulmio
 rect(0,90,849,30); // Piirrä keltainen suorakulmio
 rect(0,120,849,30); // Piirrä keltainen suorakulmio
 rect(0,150,849,30); // Piirrä keltainen suorakulmio
 
 fill(255,100,100); // Punainen täyttöväri
 rect(a1,0,50,30); // Piirrä punainen neliö
 rect(b1,30,50,30); // Piirrä punainen neliö
 rect(c1,60,50,30); // Piirrä punainen neliö
 rect(d1,90,50,30); // Piirrä punainen neliö
 rect(e1,120,50,30); // Piirrä punainen neliö
 rect(f1,150,50,30); // Piirrä punainen neliö
 
 stroke(0);
 int x1 = mouseX; // Selvitä hiiren vaakakoordinaatti
 int y1 = mouseY;
 if (mousePressed == true) {
 if ((y1 > 0) && (y1 < 30)) {
 if ((x1>a1) && (a1 < 800)) { // Jos hiiri on oikealla puolen
 a1++; // niin kasvata laskuria
 }

 if ((x1<a1) && (a1 > 0)) { // Jos hiiri on vasemmalla puolen
 a1--; // niin pienennä laskuria
 }
 }
 
 if ((y1 > 30) && (y1 < 60)) {
 if ((x1>b1) && (b1 < 800)) { // Jos hiiri on oikealla puolen
 b1++; // niin kasvata laskuria
 }

 if ((x1<b1) && (b1 > 0)) { // Jos hiiri on vasemmalla puolen
 b1--; // niin pienennä laskuria
 }
 }
 if ((y1 > 60) && (y1 < 90)) {
 if ((x1>c1) && (c1 < 800)) { // Jos hiiri on oikealla puolen
 c1++; // niin kasvata laskuria
 }

 if ((x1<c1) && (c1 > 0)) { // Jos hiiri on vasemmalla puolen
 c1--; // niin pienennä laskuria
 }
 }
 if ((y1 > 90) && (y1 < 120)) {
 if ((x1>d1) && (d1 < 800)) { // Jos hiiri on oikealla puolen
 d1++; // niin kasvata laskuria
 }

 if ((x1<d1) && (d1 > 0)) { // Jos hiiri on vasemmalla puolen
 d1--; // niin pienennä laskuria
 }
 }
 if ((y1 > 120) && (y1 < 150)) {
 if ((x1>e1) && (e1 < 800)) { // Jos hiiri on oikealla puolen
 e1++; // niin kasvata laskuria
 }

 if ((x1<e1) && (e1 > 0)) { // Jos hiiri on vasemmalla puolen
 e1--; // niin pienennä laskuria
 }
 }
 if ((y1 > 150) && (y1 < 180)) {
 if ((x1>f1) && (f1 < 800)) { // Jos hiiri on oikealla puolen
 f1++; // niin kasvata laskuria
 }

 if ((x1<f1) && (f1 > 0)) { // Jos hiiri on vasemmalla puolen
 f1--; // niin pienennä laskuria
 }
 }
 }

 fill(0); // Musta tekstin väri
 strokeWeight(2);
 int a2 = round((map(a1,0,800,-9,9)));
 int b2 = round((map(b1,0,800,-9,9)));
 int c2 = round((map(c1,0,800,-19,19)));
 int d2 = round((map(d1,0,800,-9,9)));
 int e2 = round((map(e1,0,800,-9,9)));
 int f2 = round((map(f1,0,800,-19,19)));
 
 text(""+a2,a1+4,23);
 text(""+b2,b1+4,53);
 text(""+c2,c1+4,83);
 text(""+d2,d1+4,113);
 text(""+e2,e1+4,143);
 text(""+f2,f1+4,173);
 pushMatrix();
 translate(0,100);
 textSize(30);
 text("(",40,120);
 text(") + (",230,120);
 alaatat(a2,b2,c2);
 translate(250,0);
  textSize(30);
 text(") =",240,120);
 alaatat(d2,e2,f2);
 translate(250,0);
 alaatat(a2+d2,b2+e2,c2+f2);
 popMatrix();
}

void alaatat(int a, int b, int c) {
 textSize(30);

 if (a != 0) {
  if ((b > 0) && (c > 0)) {
   if (( (a > 1) || (a < -1)) && (b > 1)) {
     text(a+"x\u00B2+"+b+"x+"+c,50,120);
    }
    if ((a == 1) && (b > 1)) {
     text("x\u00B2+"+b+"x+"+c,50,120);
    }
    if ((a == -1) && (b > 1)) {
     text("-x\u00B2+"+b+"x+"+c,50,120);
    }
    if ((a > 1) && (b == 1)) {
     text(a+"x\u00B2+x+"+c,50,120);
    }
    if ((a < -1) && (b == 1)) {
     text(a+"x\u00B2+x+"+c,50,120);
    }
    if ((a == 1) && (b == 1)) {
     text("x\u00B2+x+"+c,50,120);
    }
   if ((a == -1) && (b == 1)) {  
   text("-x\u00B2+x+"+c,50,120);
    }
  }
  if ((b == 0) && (c == 0)) {
   if ((a > 1) || (a < -1)) {
    text(a+"x\u00B2",50,120);
   }
   if (a == 1) {
    text("x\u00B2",50,120);
   }
   if (a == -1) {
    text("-x\u00B2",50,120);
   }
  }
  if ((b > 0) && (c == 0)) {
   if (((a > 1) || (a < -1)) && (b > 1)) {
     text(a+"x\u00B2+"+b+"x",50,120);
   }
   if ((a == 1) && (b > 1)) {
    text("x\u00B2+"+b+"x",50,120);
   }
   if ((a == -1) && (b > 1)) {
    text("-x\u00B2+"+b+"x",50,120);
   }
   if ((a > 1) && (b == 1)) {
    text(a+"x\u00B2+x",50,120);
   }
    if ((a < -1) && (b == 1)) {
    text(a+"x\u00B2+x",50,120);
   }
   if ((a == 1) && (b == 1)) {
    text("x\u00B2+x",50,120);
   }
   if ((a == -1) && (b == 1)) {
    text("-x\u00B2+x",50,120);
   }
  }
  if ((b == 0) && (c > 0)) {
   if ((a > 1) || (a < -1)) {
    text(a+"x\u00B2+"+c,50,120);
   }
   if (a == 1) {
    text("x\u00B2+"+c,50,120);
   }
   if (a == -1) {
    text("-x\u00B2+"+c,50,120);
   }
  }
  if ((b < 0) && (c > 0)) {
  if (((a > 1) || (a < -1)) && (b < -1)) {
     text(a+"x\u00B2"+b+"x+"+c,50,120);
   }
   if (((a > 1) || (a < -1)) && (b == -1)) {
     text(a+"x\u00B2-x+"+c,50,120);
   }
   if ((a == 1) && (b < -1)) {
     text("x\u00B2"+b+"x+"+c,50,120);
   }
   if ((a == 1) && (b == -1)) {
     text("x\u00B2-x+"+c,50,120);
   }
   if ((a == -1) && (b < -1)) {
     text("-x\u00B2"+b+"x+"+c,50,120);
   }
   if ((a == -1) && (b == -1)) {
     text("-x\u00B2-x+"+c,50,120);
   }
  }
  if ((b < 0) && (c == 0)) {
   if (((a > 1) || (a < -1)) && (b < -1)) {
      text(a+"x\u00B2"+b+"x",50,120);
    }
    if (((a > 1) || (a < -1)) && (b == -1)) {
      text(a+"x\u00B2-x",50,120);
    }
    if ((a == 1) && (b < -1)) {
      text("x\u00B2"+b+"x",50,120);
    }
     if ((a == 1) && (b == -1)) {
      text("x\u00B2-x",50,120);
    }
     if ((a == -1) && (b < -1)) {
      text("-x\u00B2"+b+"x",50,120);
    }
     if ((a == -1) && (b == -1)) {
      text("-x\u00B2-x",50,120);
    }
  }
  if ((b > 0) && (c < 0)) {
     if (((a > 1) || (a < -1)) && (b > 1)) {
        text(a+"x\u00B2+"+b+"x"+c,50,120);
     }
     if (((a > 1) || (a < -1)) && (b == 1)) {
        text(a+"x\u00B2+x"+c,50,120);
     }
     if ((a == 1) && (b > 1)) {
        text("x\u00B2+"+b+"x"+c,50,120);
     }
     if ((a == 1) && (b == 1)) {
        text("x\u00B2+x"+c,50,120);
     }
     if ((a == -1) && (b > 1)) {
        text("-x\u00B2+"+b+"x"+c,50,120);
     }
     if ((a == -1) && (b == 1)) {
        text("-x\u00B2+x"+c,50,120);
     }
  }
  if ((b == 0) && (c < 0)) {
    if ((a > 1) || (a < -1)) {
     text(a+"x\u00B2"+c,50,120);
    }
     if (a == 1) {
     text("x\u00B2"+c,50,120);
    }
    if (a == -1) {
     text("-x\u00B2"+c,50,120);
    }
  }
  if ((b < 0) && (c < 0)) {
    if (((a > 1) || (a < -1)) && (b < -1)) {
     text(a+"x\u00B2"+b+"x"+c,50,120);
    }
    if (((a > 1) || (a < -1)) && (b == -1)) {
     text(a+"x\u00B2-x"+c,50,120);
    }
    if ((a == 1) && (b < -1)) {
     text("x\u00B2"+b+"x"+c,50,120);
    }
    if ((a == 1) && (b == -1)) {
     text("x\u00B2-x"+c,50,120);
    }
    if ((a == -1) && (b < -1)) {
     text("-x\u00B2"+b+"x"+c,50,120);
    }
    if ((a == -1) && (b == -1)) {
     text("-x\u00B2-x"+c,50,120);
    }
  }
 } else {
  if ((b > 0) && (c > 0)) {
    if (b == 1) {
      text("x+"+c,50,120);
    } else {
     text(b+"x+"+c,50,120);
    }
  }
 if ((b > 0) && (c == 0)) {
    if (b == 1) {
      text("x",50,120);
    } else {
     text(b+"x",50,120);
    }
 }
 if ((b == 0) && (c > 0)) {
   text(c,50,120);
 }
 if ((b < 0) && (c > 0)) {
     if (b == -1) {
      text("-x+"+c,50,120);
    } else {
      text(b+"x+"+c,50,120);
    }
 }
 if ((b < 0) && (c == 0)) {
    if (b == -1) {
      text("-x",50,120);
    } else {
     text(b+"x",50,120);
    }
 }
 if ((b > 0) && (c < 0)) {
    if (b == 1) {
      text("x"+c,50,120);
    } else {
      text(b+"x"+c,50,120);
    }
 }
 if ((b == 0) && (c < 0)) {
   text(c,50,120);
 }
 if ((b < 0) && (c < 0)) {
     if (b == -1) {
      text("-x"+c,50,120);
    } else {
      text(b+"x"+c,50,120);
    }
 }
 }
 textSize(14);
 if (a > 0){
 for (int x1 =1; x1<= a; x1++) {
 fill(255,100,100);
 rect(50,70+60*x1,60,60);
 fill(0);
 text("x\u00B2",75,105+60*x1);
 }
 }
 if (a < 0) {
 for (int x1 =-1; x1>= a; x1--) {
 fill(100,100,255);
 rect(50,70-60*x1,60,60);
 fill(0);
 text("-x\u00B2",70,105-60*x1);
 }
 }
 if (b > 0) {
 for (int x1 =1; x1<= b; x1++) {
 fill(255,100,100);
 rect(110,70+60*x1,20,60);
 fill(0);
 text("x",117,105+60*x1);
 }
 }
 if (b < 0) {
 for (int x1 =-1; x1>= b; x1--) {
 fill(100,100,255);
 rect(110,70-60*x1,20,60);
 fill(0);
 text("-x",113,105-60*x1);
 }
 }
 if (c > 0) {
 for (int x1 =1; x1<= c; x1++) {
 fill(255,100,100);
 rect(130,110+20*x1,20,20);
 fill(0);
 text("1",137,125+20*x1);
 }
 }
 if (c < 0) {
 for (int x1 =-1; x1>= c; x1--) {
 fill(100,100,255);
 rect(130,110-20*x1,20,20);
 fill(0);
 text("-1",133,125-20*x1);
 }
 }
 
}