Murtolukujen erotus
Murtolukujen erotus

Tämä simulaatio havainnollistaa kahden murtoluvun välistä vähennyslaskua. Ohjelmalla voidaan lisäksi havainnollistaa laventamista ja supistamista. Kopioi alla oleva koodi ja aja ohjelma. Älä tee koodiin muutoksia.
// 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);
fill(100,200,255);
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;
fill(200,255,100);
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;
fill(200,255,100);
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) > 0){
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);
}
}
}
if ((b2-d2) == 0){
fill(100,200,255);
rect(550,600,50,-300);
fill(255,200,100);
for (int s=1; s <=a2; s++) {
float e = 300.0/a2;
line(550,600-e*s,600,600-e*s);
}
}
if ((b2-d2) < 0){
fill(100,200,255);
rect(550,600,50,-300);
fill(200,255,100);
float g3 = 300.0/a2;
rect(550,600,50,-g3*(d2-b2));
for (int s=1; s <=a2; s++) {
float e = 300.0/a2;
line(550,600-e*s,600,600-e*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) && ((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);
}
if ((b2-d2) == 2*a2) {
text("= 2",620,260);
}
if ((b2-d2) == a2) {
text("= 1",640,260);
fill(255,200,100);
rect(680,600,50,-300);
}
if ((b2-d2) == -a2) {
text("= -1",640,260);
fill(200,255,100);
rect(680,600,50,-300);
}
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);
}
if ((b2-d2) == -2*a2) {
text("= 2",620,260);
}
if ((b2-d2) == 0) {
text("= 0",620,260);
}
strokeWeight(2);
line(60,255,90,255);
line(160,255,190,255);
line(340,255,410,255);
line(560,255,600,255);
strokeWeight(1);
if (abs(b2-d2) != a2) {
if (((b2-d2) > 0) || ((b2-d2) < 0)) {
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));
}
if ((b2-d2) < 0) {
fill(200,255,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);
fill(200,255,100);
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) > 0) {
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);
}
}
}
if ((c2*b2-a2*d2) == 0) {
fill(100,200,255);
rect(550,600,50,-300);
fill(200,255,100);
for (int s=1; s <=(a2*c2); s++) {
float e = 300.0/(a2*c2);
line(550,600-e*s,600,600-e*s);
}
}
if ((c2*b2-a2*d2) < 0) {
if (((c2*b2-a2*d2)*10.0)/(a2*c2*10.0) >= -1) {
fill(100,200,255);
rect(550,600,50,-300);
fill(200,255,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(200,255,100);
rect(550,600,50,-300);
fill(100,200,255);
rect(650,600,50,-300);
fill(200,255,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);
if (a2 == 10) {
arc(170,210,30,30,radians(0),radians(90));
} else {
arc(160,210,30,30,radians(0),radians(90));
}
if (c2 == 10) {
arc(70,210,30,30,radians(0),radians(90));
} else {
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);
strokeWeight(1);
if ((c2*b2-a2*d2) == c2*a2) {
text("= 1",640,260);
fill(255,200,100);
rect(680,600,50,-300);
}
if ((c2*b2-a2*d2) == -c2*a2) {
text("= -1",640,260);
fill(200,255,100);
rect(680,600,50,-300);
}
strokeWeight(2);
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);
}
if ((c2*b2-a2*d2) == 2*a2*c2) {
text("= 2",625,260);
}
if ((c2*b2-a2*d2) == 0) {
text("= 0",625,260);
}
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) < 0)) {
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 ((((a2*c2)%31)==0) && ((c2*b2-a2*d2)%31==0)) { jako=31; }
if ((((a2*c2)%32)==0) && ((c2*b2-a2*d2)%32==0)) { jako=32; }
if ((((a2*c2)%33)==0) && ((c2*b2-a2*d2)%33==0)) { jako=33; }
if ((((a2*c2)%34)==0) && ((c2*b2-a2*d2)%34==0)) { jako=34; }
if ((((a2*c2)%35)==0) && ((c2*b2-a2*d2)%35==0)) { jako=35; }
if ((((a2*c2)%36)==0) && ((c2*b2-a2*d2)%36==0)) { jako=36; }
if ((((a2*c2)%37)==0) && ((c2*b2-a2*d2)%37==0)) { jako=37; }
if ((((a2*c2)%38)==0) && ((c2*b2-a2*d2)%38==0)) { jako=38; }
if ((((a2*c2)%39)==0) && ((c2*b2-a2*d2)%39==0)) { jako=39; }
if ((((a2*c2)%40)==0) && ((c2*b2-a2*d2)%40==0)) { jako=40; }
if ((((a2*c2)%41)==0) && ((c2*b2-a2*d2)%41==0)) { jako=41; }
if ((((a2*c2)%42)==0) && ((c2*b2-a2*d2)%42==0)) { jako=42; }
if ((((a2*c2)%43)==0) && ((c2*b2-a2*d2)%43==0)) { jako=43; }
if ((((a2*c2)%44)==0) && ((c2*b2-a2*d2)%44==0)) { jako=44; }
if ((((a2*c2)%45)==0) && ((c2*b2-a2*d2)%45==0)) { jako=45; }
if ((((a2*c2)%46)==0) && ((c2*b2-a2*d2)%46==0)) { jako=46; }
if ((((a2*c2)%47)==0) && ((c2*b2-a2*d2)%47==0)) { jako=47; }
if ((((a2*c2)%48)==0) && ((c2*b2-a2*d2)%48==0)) { jako=48; }
if ((((a2*c2)%49)==0) && ((c2*b2-a2*d2)%49==0)) { jako=49; }
if ((((a2*c2)%50)==0) && ((c2*b2-a2*d2)%40==0)) { jako=50; }
if (jako != 1) {
text("=",640,260);
text(a2*c2/jako,680,280);
text((c2*b2-a2*d2)/jako,680,250);
textSize(20);
text(jako,610,215);
textSize(30);
strokeWeight(2);
noFill();
arc(620,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));
}
if ((c2*b2-a2*d2) < 0) {
fill(200,255,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);
}
}
}
}
}
}