Polynomi
Polynomi

Tämä ohjelma havainnollistaa polynomin termejä 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; // Laskurin alkuarvo (ikkunan keskellä)
int b1 = 450; // Laskurin alkuarvo (ikkunan keskellä)
int c1 = 450; // Laskurin alkuarvo (ikkunan keskellä)
void setup () {
size(850,673);
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,40); // Piirrä keltainen suorakulmio
rect(0,40,849,40); // Piirrä keltainen suorakulmio
rect(0,80,849,40); // Piirrä keltainen suorakulmio
fill(255,100,100); // Punainen täyttöväri
rect(a1,0,50,40); // Piirrä punainen neliö
rect(b1,40,50,40); // Piirrä punainen neliö
rect(c1,80,50,40); // Piirrä punainen neliö
stroke(0);
int x1 = mouseX; // Selvitä hiiren vaakakoordinaatti
int y1 = mouseY;
if (mousePressed == true) {
if ((y1 > 0) && (y1 < 40)) {
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 > 40) && (y1 < 80)) {
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 > 80) && (y1 < 120)) {
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
}
}
}
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,-27,27)));
text(""+a2,a1+4,33);
text(""+b2,b1+4,73);
text(""+c2,c1+4,113);
alaatat(a2,b2,c2);
}
void alaatat(int a, int b, int c) {
textSize(30);
text("Polynomi",350,180);
text("Termi",350,300);
text("Kerroin",500,300);
text("Kirjainosa",650,300);
noFill();
rect(340,260,470,270);
line(340,320,810,320);
line(470,260,470,530);
line(630,260,630,530);
if ((a < -1) || ( a > 1)) {
text(a+"x\u00B2",350,370);
}
if (a == -1) {
text("-x\u00B2",350,370);
}
if (a == 1) {
text("x\u00B2",350,370);
}
if (a == 0) {
text("0",350,370);
}
if ((b < -1) || ( b > 1)) {
text(b+"x",350,420);
}
if (b == -1) {
text("-x",350,420);
}
if (b == 1) {
text("x",350,420);
}
if (b == 0) {
text("0",350,420);
}
text(c,350,470);
text(a,500,370);
text(b,500,420);
text(c,500,470);
text("x\u00B2",650,370);
text("x\u00B9 = x",650,420);
text("x\u2070 = 1, ei",650,470);
if (a != 0) {
if ((b > 0) && (c > 0)) {
if (( (a > 1) || (a < -1)) && (b > 1)) {
text(a+"x\u00B2+"+b+"x+"+c,350,230);
}
if ((a == 1) && (b > 1)) {
text("x\u00B2+"+b+"x+"+c,350,230);
}
if ((a == -1) && (b > 1)) {
text("-x\u00B2+"+b+"x+"+c,350,230);
}
if ((a > 1) && (b == 1)) {
text(a+"x\u00B2+x+"+c,350,230);
}
if ((a < -1) && (b == 1)) {
text(a+"x\u00B2+x+"+c,350,230);
}
if ((a == 1) && (b == 1)) {
text("x\u00B2+x+"+c,350,230);
}
if ((a == -1) && (b == 1)) { // Mikä?
text("-x\u00B2+x+"+c,350,230);
}
}
if ((b == 0) && (c == 0)) {
if ((a > 1) || (a < -1)) {
text(a+"x\u00B2",350,230);
}
if (a == 1) {
text("x\u00B2",350,230);
}
if (a == -1) {
text("-x\u00B2",350,230);
}
}
if ((b > 0) && (c == 0)) {
if (((a > 1) || (a < -1)) && (b > 1)) {
text(a+"x\u00B2+"+b+"x",350,230);
}
if ((a == 1) && (b > 1)) {
text("x\u00B2+"+b+"x",350,230);
}
if ((a == -1) && (b > 1)) {
text("-x\u00B2+"+b+"x",350,230);
}
if ((a > 1) && (b == 1)) {
text(a+"x\u00B2+x",350,230);
}
if ((a < -1) && (b == 1)) {
text(a+"x\u00B2+x",350,230);
}
if ((a == 1) && (b == 1)) {
text("x\u00B2+x",350,230);
}
if ((a == -1) && (b == 1)) {
text("-x\u00B2+x",350,230);
}
}
if ((b == 0) && (c > 0)) {
if ((a > 1) || (a < -1)) {
text(a+"x\u00B2+"+c,350,230);
}
if (a == 1) {
text("x\u00B2+"+c,350,230);
}
if (a == -1) {
text("-x\u00B2+"+c,350,230);
}
}
if ((b < 0) && (c > 0)) {
if (((a > 1) || (a < -1)) && (b < -1)) {
text(a+"x\u00B2"+b+"x+"+c,350,230);
}
if (((a > 1) || (a < -1)) && (b == -1)) {
text(a+"x\u00B2-x+"+c,350,230);
}
if ((a == 1) && (b < -1)) {
text("x\u00B2"+b+"x+"+c,350,230);
}
if ((a == 1) && (b == -1)) {
text("x\u00B2-x+"+c,350,230);
}
if ((a == -1) && (b < -1)) {
text("-x\u00B2"+b+"x+"+c,350,230);
}
if ((a == -1) && (b == -1)) {
text("-x\u00B2-x+"+c,350,230);
}
}
if ((b < 0) && (c == 0)) {
if (((a > 1) || (a < -1)) && (b < -1)) {
text(a+"x\u00B2"+b+"x",350,230);
}
if (((a > 1) || (a < -1)) && (b == -1)) {
text(a+"x\u00B2-x",350,230);
}
if ((a == 1) && (b < -1)) {
text("x\u00B2"+b+"x",350,230);
}
if ((a == 1) && (b == -1)) {
text("x\u00B2-x",350,230);
}
if ((a == -1) && (b < -1)) {
text("-x\u00B2"+b+"x",350,230);
}
if ((a == -1) && (b == -1)) {
text("-x\u00B2-x",350,230);
}
}
if ((b > 0) && (c < 0)) {
if (((a > 1) || (a < -1)) && (b > 1)) {
text(a+"x\u00B2+"+b+"x"+c,350,230);
}
if (((a > 1) || (a < -1)) && (b == 1)) {
text(a+"x\u00B2+x"+c,350,230);
}
if ((a == 1) && (b > 1)) {
text("x\u00B2+"+b+"x"+c,350,230);
}
if ((a == 1) && (b == 1)) {
text("x\u00B2+x"+c,350,230);
}
if ((a == -1) && (b > 1)) {
text("-x\u00B2+"+b+"x"+c,350,230);
}
if ((a == -1) && (b == 1)) {
text("-x\u00B2+x"+c,350,230);
}
}
if ((b == 0) && (c < 0)) {
if ((a > 1) || (a < -1)) {
text(a+"x\u00B2"+c,350,230);
}
if (a == 1) {
text("x\u00B2"+c,350,230);
}
if (a == -1) {
text("-x\u00B2"+c,350,230);
}
}
if ((b < 0) && (c < 0)) {
if (((a > 1) || (a < -1)) && (b < -1)) {
text(a+"x\u00B2"+b+"x"+c,350,230);
}
if (((a > 1) || (a < -1)) && (b == -1)) {
text(a+"x\u00B2-x"+c,350,230);
}
if ((a == 1) && (b < -1)) {
text("x\u00B2"+b+"x"+c,350,230);
}
if ((a == 1) && (b == -1)) {
text("x\u00B2-x"+c,350,230);
}
if ((a == -1) && (b < -1)) {
text("-x\u00B2"+b+"x"+c,350,230);
}
if ((a == -1) && (b == -1)) {
text("-x\u00B2-x"+c,350,230);
}
}
} else {
if ((b > 0) && (c > 0)) {
if (b == 1) {
text("x+"+c,350,230);
} else {
text(b+"x+"+c,350,230);
}
}
if ((b > 0) && (c == 0)) {
if (b == 1) {
text("x",350,230);
} else {
text(b+"x",350,230);
}
}
if ((b == 0) && (c > 0)) {
text(c,350,230);
}
if ((b < 0) && (c > 0)) {
if (b == -1) {
text("-x+"+c,350,230);
} else {
text(b+"x+"+c,350,230);
}
}
if ((b < 0) && (c == 0)) {
if (b == -1) {
text("-x",350,230);
} else {
text(b+"x",350,230);
}
}
if ((b > 0) && (c < 0)) {
if (b == 1) {
text("x"+c,350,230);
} else {
text(b+"x"+c,350,230);
}
}
if ((b == 0) && (c < 0)) {
text(c,350,230);
}
if ((b < 0) && (c < 0)) {
if (b == -1) {
text("-x"+c,350,230);
} else {
text(b+"x"+c,350,230);
}
}
}
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);
}
}
}