Murtolukujen summa

Murtolukujen summa



Tämä simulaatio havainnollistaa kahden murtoluvun välistä yhteenlaskua. Ohjelmalla voidaan lisäksi havainnollistaa laventamista ja supistamista.  Älä tee koodiin muutoksia.

// Ohjelman tehnyt e-Oppi Oy
// 30.8.2017
int a = 200; // Laskurin alkuarvo (ikkunan keskellä)
int b = 200; // Laskurin alkuarvo (ikkunan keskellä)
int c = 200; // Laskurin alkuarvo (ikkunan keskellä)
int d = 200; // Laskurin alkuarvo (ikkunan keskellä)

void setup () {
     size(850,650); // ikkunan koko
     stroke(0); // Musta viivan väri
     strokeWeight(3);
     

}

void draw() {
     background(255); // Tyhjennä tausta
     strokeWeight(2);
     textSize(30);
     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
     rect(0,120,849,40); // Piirrä keltainen suorakulmio
     fill(255,100,100); // Punainen täyttöväri
     rect(a,0,50,40); // Piirrä punainen neliö
     rect(b,40,50,40); // Piirrä punainen neliö
     rect(c,80,50,40); // Piirrä punainen neliö
     rect(d,120,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>a) && (a < 800)) { // Jos hiiri on oikealla puolen
         a++; // niin kasvata laskuria
     }

     if ((x1<a) && (a > 0)) { // Jos hiiri on vasemmalla puolen
         a--; // niin pienennä laskuria
     }
     }
    
     if ((y1 > 40) && (y1 < 80)) {
     if ((x1>b) && (b < 800)) { // Jos hiiri on oikealla puolen
         b++; // niin kasvata laskuria
     }

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

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

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

     fill(0); // Musta tekstin väri
     strokeWeight(1);
     int a2 = round((map(a,0,800,1,10)));
     int b2 = round((map(b,0,800,0,a2)));
     int c2 = round((map(c,0,800,1,10)));
     int d2 = round((map(d,0,800,0,c2)));
    
          text(""+a2,a+4,33);
        
          text(""+b2,b+4,73);
           text(""+c2,c+4,113);
        
          text(""+d2,d+4,153);
          textSize(24);
    fill(100,200,255);
    rect(50,600,50,-300);
    rect(150,600,50,-300);
 
    fill(255,200,100);
    float g = 300.0/a2;
    rect(50,600,50,-g*b2);
    float h = 300.0/c2;
    rect(150,600,50,-h*d2);
    for (int s=1; s <=a2; s++) {
      float e = 300.0/a2;
      line(50,600-e*s,100,600-e*s);
    }
    for (int s=1; s <=c2; s++) {
      float f = 300.0/c2;
      line(150,600-f*s,200,600-f*s);
    }
   if (a2 == c2) {
      fill(100,200,255);
      rect(300,600,50,-300);
      rect(400,600,50,-300);
        fill(255,200,100);
        float g2 = 300.0/a2;
        rect(300,600,50,-g2*b2);
        float h2 = 300.0/c2;
        rect(400,600,50,-h2*d2);
          for (int s=1; s <=a2; s++) {
          float e = 300.0/a2;
          line(300,600-e*s,350,600-e*s);
          }
         for (int s=1; s <=c2; s++) {
          float f = 300.0/c2;
          line(400,600-f*s,450,600-f*s);
        }
        if (((b2+d2)*10.0)/(a2*10.0) <= 1) {
           fill(100,200,255);
           rect(550,600,50,-300);
           fill(255,200,100);
           float g3 = 300.0/a2;
           rect(550,600,50,-g3*(b2+d2));
            for (int s=1; s <=a2; s++) {
             float e = 300.0/a2;
             line(550,600-e*s,600,600-e*s);
            }
      
        } else {
           fill(100,200,255);
           rect(550,600,50,-300);
           fill(255,200,100);
           rect(550,600,50,-300);
           fill(100,200,255);
           rect(650,600,50,-300);
           fill(255,200,100);
           float g3 = 300.0/a2;
           rect(650,600,50,-g3*(b2+d2-a2));
            for (int s=1; s <=a2; s++) {
             float e = 300.0/a2;
             line(550,600-e*s,600,600-e*s);
            }
            for (int s=1; s <=c2; s++) {
             float f = 300.0/c2;
             line(650,600-f*s,700,600-f*s);
           }
        }
        fill(0);
        textSize(30);
        text(a2,65,280);
        text(c2,165,280);
        text(b2,65,250);
        text(d2,165,250);
        text(a2,365,280);
        text(a2,565,280);
        text("+",120,260);
        text("=",240,260);
        text("=",490,260);
        text(b2+"+"+d2,345,250);
        text(b2+d2,565,250);
        if ((b2+d2) == a2) {
          text("= 1",620,260);
         
        }
         if (((b2+d2) > a2) && ((b2+d2) != 2*a2)) {
          text("= 1",620,260);
          strokeWeight(2);
          line(675,255,715,255);
          text(((b2+d2)-a2),680,250);
           text(a2,680,280);
              noFill();
           rect(525,290,200,320);
           fill(0);
         
        }
         if ((b2+d2) == 2*a2) {
          text("= 2",620,260);
           noFill();
           strokeWeight(2);
           rect(525,290,200,320);
           fill(0);
         
        }
        strokeWeight(2);
        line(60,255,90,255);
        line(160,255,190,255);
        line(340,255,410,255);
        line(560,255,590,255);
        strokeWeight(1);
        
        if (abs(b2+d2) != a2) {
        if (((b2+d2) > 0) && ((b2+d2) < a2)) {
          int jako=1;
          if (((a2%2)==0) && ((b2+d2)%2==0)) { jako=2; }
          if (((a2%3)==0) && ((b2+d2)%3==0)) { jako=3; }
          if (((a2%4)==0) && ((b2+d2)%4==0)) { jako=4; }
          if (((a2%5)==0) && ((b2+d2)%5==0)) { jako=5; }
          if (jako != 1) {
            
            text("=",640,260);
            text(a2/jako,680,280);
            text((b2+d2)/jako,680,250);
            textSize(20);
            text(jako,590,215);
            textSize(30);
            strokeWeight(2);
            noFill();
            arc(600,210,30,30,radians(90),radians(180));
            fill(0);
            line(675,255,715,255);
            strokeWeight(1);
            
             fill(100,200,255);
             rect(680,600,50,-300);
            
             float g3 = 300.0/(a2/jako);
             if ((b2+d2) > 0) {
                fill(255,200,100);
                rect(680,600,50,-g3*((b2+d2)/jako));
             }
             
            
            for (int s=1; s <=(a2/jako); s++) {
             float e = 300.0/(a2/jako);
             line(680,600-e*s,730,600-e*s);
            }
            
          }
        }
       }
        
        
   } else {
      fill(100,200,255);
     rect(300,600,50,-300);
     rect(400,600,50,-300);
     
      fill(255,200,100);
        float g2 = 300.0/(a2*c2);
        rect(300,600,50,-g2*(c2*b2));
        float h2 = 300.0/(a2*c2);
        rect(400,600,50,-h2*(a2*d2));
          for (int s=1; s <= (c2*a2); s++) {
          float e = 300.0/(a2*c2);
          line(300,600-e*s,350,600-e*s);
          }
         for (int s=1; s <= (a2*c2); s++) {
          float f = 300.0/(a2*c2);
          line(400,600-f*s,450,600-f*s);
        }
        
        if (((c2*b2+a2*d2)*10.0)/(a2*c2*10.0) <= 1) {
           fill(100,200,255);
           rect(550,600,50,-300);
           fill(255,200,100);
           float g3 = 300.0/(a2*c2);
           rect(550,600,50,-g3*(c2*b2+a2*d2));
            for (int s=1; s <=(a2*c2); s++) {
             float e = 300.0/(a2*c2);
             line(550,600-e*s,600,600-e*s);
            }
      
        } else {
           fill(100,200,255);
           rect(550,600,50,-300);
           fill(255,200,100);
           rect(550,600,50,-300);
           fill(100,200,255);
           rect(650,600,50,-300);
           fill(255,200,100);
           float g3 = 300.0/(a2*c2);
           rect(650,600,50,-g3*(c2*b2+a2*d2-a2*c2));
            for (int s=1; s <=(a2*c2); s++) {
             float e = 300.0/(a2*c2);
             line(550,600-e*s,600,600-e*s);
            }
            for (int s=1; s <=(a2*c2); s++) {
             float f = 300.0/(a2*c2);
             line(650,600-f*s,700,600-f*s);
           }
        }
        noFill();
         strokeWeight(2);
         arc(160,210,30,30,radians(0),radians(90));
         arc(60,210,30,30,radians(0),radians(90));
         fill(0);
         textSize(20);
         text(a2,155,220);
        text(c2,55,220);
        textSize(30);
        text(a2,65,280);
        text(c2,165,280);
        text(b2,65,250);
        text(d2,165,250);
        text(a2*c2,365,280);
        text(a2*c2,565,280);
        text("+",120,260);
        text("=",240,260);
        text("=",490,260);
        text(c2*b2+"+"+a2*d2,345,250);
        text(c2*b2+a2*d2,565,250);
        
        if ((c2*b2+a2*d2) == c2*a2) {
          text("= 1",645,260);
          fill(255,200,100);
          strokeWeight(1);
          rect(680,600,50,-300);
         
        }
         if (((c2*b2+a2*d2) > c2*a2) && ((c2*b2+a2*d2) != 2*a2*c2)) {
          text("= 1",625,260);
          strokeWeight(2);
          line(680,255,735,255);
          text(((c2*b2+a2*d2)-a2*c2),685,250);
           text(a2*c2,685,280);
           noFill();
           rect(525,290,200,320);
           fill(0);
         
        }
         if ((c2*b2+a2*d2) == 2*a2*c2) {
          text("= 2",625,260);
           noFill();
           rect(525,290,200,320);
           fill(0);
         
        }
        strokeWeight(2);
        line(60,255,90,255);
        line(160,255,190,255);
        line(340,255,430,255);
        line(560,255,620,255);
        strokeWeight(1);
        
             if (abs(c2*b2+a2*d2) != a2*c2) {
        if (((c2*b2+a2*d2) > 0) && ((c2*b2+a2*d2) < a2*c2)) {
          int jako=1;
          if ((((a2*c2)%2)==0) && ((c2*b2+a2*d2)%2==0)) { jako=2; }
          if ((((a2*c2)%3)==0) && ((c2*b2+a2*d2)%3==0)) { jako=3; }
          if ((((a2*c2)%4)==0) && ((c2*b2+a2*d2)%4==0)) { jako=4; }
          if ((((a2*c2)%5)==0) && ((c2*b2+a2*d2)%5==0)) { jako=5; }
          if ((((a2*c2)%6)==0) && ((c2*b2+a2*d2)%6==0)) { jako=6; }
          if ((((a2*c2)%7)==0) && ((c2*b2+a2*d2)%7==0)) { jako=7; }
          if ((((a2*c2)%8)==0) && ((c2*b2+a2*d2)%8==0)) { jako=8; }
          if ((((a2*c2)%9)==0) && ((c2*b2+a2*d2)%9==0)) { jako=9; }
          if ((((a2*c2)%10)==0) && ((c2*b2+a2*d2)%10==0)) { jako=10; }
          if ((((a2*c2)%11)==0) && ((c2*b2+a2*d2)%11==0)) { jako=11; }
          if ((((a2*c2)%12)==0) && ((c2*b2+a2*d2)%12==0)) { jako=12; }
          if ((((a2*c2)%13)==0) && ((c2*b2+a2*d2)%13==0)) { jako=13; }
          if ((((a2*c2)%14)==0) && ((c2*b2+a2*d2)%14==0)) { jako=14; }
          if ((((a2*c2)%15)==0) && ((c2*b2+a2*d2)%15==0)) { jako=15; }
          if ((((a2*c2)%16)==0) && ((c2*b2+a2*d2)%16==0)) { jako=16; }
          if ((((a2*c2)%17)==0) && ((c2*b2+a2*d2)%17==0)) { jako=17; }
          if ((((a2*c2)%18)==0) && ((c2*b2+a2*d2)%18==0)) { jako=18; }
          if ((((a2*c2)%19)==0) && ((c2*b2+a2*d2)%19==0)) { jako=19; }
          if ((((a2*c2)%20)==0) && ((c2*b2+a2*d2)%20==0)) { jako=20; }
          if ((((a2*c2)%21)==0) && ((c2*b2+a2*d2)%21==0)) { jako=21; }
          if ((((a2*c2)%22)==0) && ((c2*b2+a2*d2)%22==0)) { jako=22; }
          if ((((a2*c2)%23)==0) && ((c2*b2+a2*d2)%23==0)) { jako=23; }
          if ((((a2*c2)%24)==0) && ((c2*b2+a2*d2)%24==0)) { jako=24; }
          if ((((a2*c2)%25)==0) && ((c2*b2+a2*d2)%25==0)) { jako=25; }
          if ((((a2*c2)%26)==0) && ((c2*b2+a2*d2)%26==0)) { jako=26; }
          if ((((a2*c2)%27)==0) && ((c2*b2+a2*d2)%27==0)) { jako=27; }
          if ((((a2*c2)%28)==0) && ((c2*b2+a2*d2)%28==0)) { jako=28; }
          if ((((a2*c2)%29)==0) && ((c2*b2+a2*d2)%29==0)) { jako=29; }
          if ((((a2*c2)%30)==0) && ((c2*b2+a2*d2)%30==0)) { jako=30; }
          if (jako != 1) {
            
            text("=",640,260);
            text((a2*c2)/jako,680,280);
            text((c2*b2+a2*d2)/jako,680,250);
            textSize(20);
            text(jako,590,215);
            textSize(30);
            strokeWeight(2);
            noFill();
            arc(600,210,30,30,radians(90),radians(180));
            fill(0);
            line(675,255,715,255);
            strokeWeight(1);
            
             fill(100,200,255);
             rect(680,600,50,-300);
            
             float g3 = 300.0/((c2*a2)/jako);
             if ((c2*b2+a2*d2) > 0) {
                fill(255,200,100);
                rect(680,600,50,-g3*((c2*b2+a2*d2)/jako));
             }
             
            
            for (int s=1; s <=((a2*c2)/jako); s++) {
             float e = 300.0/((a2*c2)/jako);
             line(680,600-e*s,730,600-e*s);
            }
            
          }
        }
       }
     
   }     
     
}