3D-geometrian funktiot (Tämä kirjasto sopii iPAD:lle)

Tämä kirjasto sisältää seuraavat komennot
  • Kartio
  • Koordinaatisto
  • Kommentti
  • Lieriö
  • Mittaa-funktiot
  • Pallo
  • Piirtokolmio
  • Piste
  • Pyramidi
  • Särmiö
  • Viivoitin

// Ohjelman tehnyt e-Oppi Oy, 3D-Geometrian funktiot
// Viimeksi päivitetty: 31.8.2017
// Oletus täyttöväri
int R =250; // Oletus punainen
int G = 200; // Oletus vihreä
int B = 0; // Oletus sininen
int A = 100; // Oletus läpinäkyvyys

void setup () {
     size(601,601); // Älä muuta ikkunan kokoa
}
// Kaikki komennot laitetaan void draw ()-lohkoon
void draw () {
      koordinaatisto();
    // Kirjoita tähän kaikki komennot
}

// Tästä alkaa funktiot eli aliohjelmat
// Älä tee näihin muutoksia !!

// Koordinaatisto on suunniteltu 600x600 ikkunaan
void koordinaatisto() {
     fill(255);
     noStroke();
     rect(0,0,600,600);
     stroke(128);
     strokeWeight(1);
     for (int z = 0; z < 21; z++) {
         line(z*30,0,z*30,600); 
         line(0,0+z*30,600,z*30); 
     }
     strokeWeight(3);
     stroke(0,0,100);
     line(300,3,300,600);
     line(0,300,600,300);
     fill(0,0,100);
     triangle(590,290,598,300,590,310);
     triangle(300,3,310,10,290,10);
     text("x",580,285);
     text("y",315,20);
     textSize(20);
     text("1",323,320);
     text("1",280,280);
     translate(300,300);
     scale(1,-1);
     strokeWeight(2); // Oletus paksuus
     fill(R,G,B,A); // Oletusväri
     stroke(0);
}


void piste(float x, float y) {
     float x2 = map(x,-10,10,-300,300);
     float y2 = map(y,-10,10,-300,300);
     strokeWeight(3);
     // fill(255,0,0);
     ellipse(x2,y2,10,10);
     fill(R,G,B,A);
     strokeWeight(2);
     stroke(0);
}

void sarmio(float x, float y, float a, float b, float c) {
      float x1 = map(x,-10,10,-300,300);
      float y1 = map(y,-10,10,-300,300);
      float a1 = map(a,-10,10,-300,300);
      float b1 = map(b,-10,10,-300,300)/2.0;
      float c1 = map(c,-10,10,-300,300);
      float dx = b1*cos(radians(45));
      float dy = b1*sin(radians(45));
      quad(x1,y1,x1+dx,y1+dy,x1+dx,y1+c1+dy,x1,y1+c1);
      quad(x1,y1,x1+dx,y1+dy,x1+a1+dx,y1+dy,x1+a1,y1);
      quad(x1+dx,y1+dy,x1+a1+dx,y1+dy,x1+a1+dx,y1+c1+dy,x1+dx,y1+c1+dy);
      rect(x1,y1,a1,c1);
      quad(x1+a1,y1,x1+a1+dx,y1+dy,x1+a1+dx,y1+c1+dy,x1+a1,y1+c1);
      quad(x1+a1+dx,y1+c1+dy,x1+a1,y1+c1,x1,y1+c1,x1+dx,y1+c1+dy);
}

void kartio(float x, float y, float r, float h) {
      float x1 = map(x,-10,10,-300,300);
      float y1 = map(y,-10,10,-300,300);
      float r1 = map(r,-10,10,-300,300);
      float h1 = map(h,-10,10,-300,300);
      strokeWeight(2);
      stroke(R,G,B,A);
      for (int s= 0; s <= 360; s++) {
        float xx = x1+r1*cos(radians(s));
        float yy = y1+r1/2*sin(radians(s));
        line(x1,y1+h1,xx,yy);
      }
      stroke(0);
      strokeWeight(3);
      ellipse(x1,y1,r1*2,r1);
      noFill();
      triangle(x1,y1,x1+r1,y1,x1,y1+h1);
      triangle(x1,y1,x1-r1,y1,x1,y1+h1);
      fill(R,G,B,A);
}
void lierio(float x, float y, float r, float h) {
      float x1 = map(x,-10,10,-300,300);
      float y1 = map(y,-10,10,-300,300);
      float r1 = map(r,-10,10,-300,300);
      float h1 = map(h,-10,10,-300,300);
  
          fill(R,G,B,A);
     strokeWeight(3);
     stroke(R,G,B,A);
     for (int s= 0; s <= 360; s++) {
       float xx = x1+r1*cos(radians(s));
        float yy = y1+r1/2*sin(radians(s));
       line(xx,yy,xx,yy+h1);
      }
         noFill();
         stroke(0);
       ellipse(x1,y1,r1*2,r1);
      rect(x1-r1,y1,r1,h1);
      rect(x1,y1,r1,h1);
      stroke(0);
      fill(R,G,B,A);
         strokeWeight(3);
      ellipse(x1,y1+h1,r1*2,r1);
}

void pyramidi(float x, float y, float r, float h) {
      float x1 = map(x,-10,10,-300,300);
      float y1 = map(y,-10,10,-300,300);
      float r1 = map(r,-10,10,-300,300);
      float h1 = map(h,-10,10,-300,300);
      float dx = (r1/2.0)*cos(radians(45));
      float dy = (r1/2.0)*sin(radians(45));
      noFill();  
      quad(x1-r1-dx,y1-dy,x1+r1-dx,y1-dy,x1+r1+dx,y1+dy,x1-r1+dx,y1+dy);
      fill(R,G,B,A);
      triangle(x1+r1+dx,y1+dy,x1-r1+dx,y1+dy,x1,y1+h1);
      line(x1,y1,x1,y1+h1); 
      line(x1,y1,x1+r1,y1);
      line(x1,y1+h1,x1+r1,y1);
      triangle(x1-r1-dx,y1-dy,x1-r1+dx,y1+dy,x1,y1+h1);
     
      triangle(x1+r1-dx,y1-dy,x1+r1+dx,y1+dy,x1,y1+h1);
      triangle(x1-r1-dx,y1-dy,x1+r1-dx,y1-dy,x1,y1+h1);
      
}
void pallo(float x, float y, float r) {
      float x1 = map(x,-10,10,-300,300);
      float y1 = map(y,-10,10,-300,300);
      float r1 = map(r,-10,10,-300,300);
      ellipse(x1,y1,2*r1,2*r1);
      noFill();
      ellipse(x1,y1,2*r1,r1);
      line(x1-r1,y1,x1+r1,y1);
      line(x1,y1,x1,y1+r1);
      fill(R,G,B,A);      
}
void sarmiok(float x, float y, float a,float b, float h) {
      float x1 = map(x,-10,10,-300,300);
      float y1 = map(y,-10,10,-300,300);
      float a1 = map(a,-10,10,-300,300);
    //  float b1 = map(b,-10,10,-300,300);
      float h1 = map(h,-10,10,-300,300);
      float kulma = atan(a/b);
      float xx = b*30*cos(kulma);
      float yy =b*30*sin(kulma)/2.0;
      if (h < 0) {
        fill(R,G,B,A);
      } else {
        noFill();
      }
      rect(x1,y1,a1,h1);
      triangle(x1,y1,x1+a1,y1,x1+xx,y1-yy);
        fill(R,G,B,A);
      triangle(x1,y1+h1,x1+a1,y1+h1,x1+xx,y1+h1-yy);
      quad(x1,y1,x1+xx,y1-yy,x1+xx,y1+h1-yy,x1,y1+h1);
      quad(x1+a1,y1,x1+xx,y1-yy,x1+xx,y1+h1-yy,x1+a1,y1+h1);
}


void kommentti(String teksti,float x,float y,float koko) {
      float x2 = map(x,-10,10,-300,300);
      float y2 = map(y,-10,10,-300,300);
      fill(0);
      pushMatrix();
      scale(1,-1);
      fill(0);
      textSize(koko);
      text(teksti,x2,-y2);
      popMatrix();
      textSize(20);
        fill(R,G,B,A); // Oletusväri
}

void mittaakulma(float x1, float y1, float x2, float y2, float x3, float y3) {
float xx1 = map(x1,-10,10,-300,300);
float yy1 = map(y1,-10,10,-300,300);
float xx2 = map(x2,-10,10,-300,300);
float yy2 = map(y2,-10,10,-300,300);
float xx3 = map(x3,-10,10,-300,300);
float yy3 = map(y3,-10,10,-300,300);
float sade1 = sqrt((y1-y2)*(y1-y2)+(x1-x2)*(x1-x2));
float sade2 = sqrt((y3-y2)*(y3-y2)+(x3-x2)*(x3-x2));
float kulma1 = acos(abs(x1-x2)/sade1);
float kulma2 = acos(abs(x3-x2)/sade2);
float summa1 = 0;
float summa2 = 0;
float lisays = 0;
if ( ((y1-y2) == 0) && (x1 >= x2) ) { summa1 = 0; }
if ( ((y2-y1) == 0) && (x2 > x1) ) { summa1 = 180; }
if ( ((x1-x2) == 0) && (y1 >= y2) ) { summa1 = 90; }
if ( ((x2-x1) == 0) && (y2 > y1) ) { summa1 = 270; }

if (abs(x3-x2) == 0) {
lisays = 90-degrees(kulma1);
}

if ( ((x1-x2) > 0) && ((y1 -y2) > 0) ) {
summa1 = degrees(kulma1);
}
if ( ((x1-x2) < 0) && ((y1 -y2) > 0) ) {
summa1 = (90-degrees(kulma1))+90;
}
if ( ((x1-x2) < 0) && ((y1 -y2) < 0) ) {
summa1 = degrees(kulma1)+180;
}
if ( ((x1-x2) >=0) && ((y1 -y2) < 0) ) {
summa1 = 360-degrees(kulma1);
}

if ( ((x3-x2) > 0) && ((y3 -y2) > 0) ) {
summa2 = degrees(kulma2);
if ((summa1 >= 0) && (summa1 < 90)) {
if (summa2 > summa1) {
lisays = summa2-summa1;
} else {
lisays = 360-summa1+summa2;
}
}
if ((summa1 >= 90) && (summa1 < 180)) {
lisays = 360-summa1+summa2;
}
if ((summa1 >= 180) && (summa1 < 270)) {
lisays = 360-summa1+summa2;
}
if ((summa1 >= 270) && (summa1 < 360)) {
lisays = 360-summa1+summa2;
}
}
if ( ((x3-x2) <= 0) && ((y3 -y2) >= 0) ) {
summa2 = (90-degrees(kulma2))+90;
if ((summa1 >= 0) && (summa1 < 90)) {
lisays = summa2-summa1;
}
if ((summa1 >= 90) && (summa1 < 180)) {
if (summa2 > summa1) {
lisays = summa2-summa1;
} else {
lisays = 360-summa1+summa2;
}
}
if ((summa1 >= 180) && (summa1 < 270)) {
lisays = 360-summa1+summa2;
}
if ((summa1 >= 270) && (summa1 < 360)) {
lisays = 360-summa1+summa2;
}
}
if ( ((x3-x2) <= 0) && ((y3 -y2) <= 0) ) {
summa2 = degrees(kulma2)+180;
if ((summa1 >= 0) && (summa1 < 90)) {
lisays = summa2-summa1;
}
if ((summa1 >= 90) && (summa1 < 180)) {
lisays = summa2-summa1;
}
if ((summa1 >= 180) && (summa1 < 270)) {

if (summa2 > summa1) {
lisays = summa2-summa1;
} else {
lisays = 360-summa1+summa2;
}
}
if ((summa1 >= 270) && (summa1 < 360)) {
lisays = 360-summa1+summa2;
}
}
if ( ((x3-x2) >= 0) && ((y3 -y2) <= 0) ) {
summa2 = 360-degrees(kulma2);
if ((summa1 >= 0) && (summa1 < 90)) {
lisays = summa2-summa1;
}
if ((summa1 >= 90) && (summa1 < 180)) {
lisays = summa2-summa1;
}
if ((summa1 >= 180) && (summa1 < 270)) {
lisays = summa2-summa1;

}
if ((summa1 >= 270) && (summa1 < 360)) {
if (summa2 > summa1) {
lisays = summa2-summa1;
} else {
lisays = 360-summa1+summa2;
}
}

}

float summa3 = (summa1+summa1+lisays)/2;
strokeWeight(6);
point(xx1,yy1);
point(xx2,yy2);
point(xx3,yy3);
strokeWeight(3);
line(xx2,yy2,xx1,yy1);
line(xx2,yy2,xx3,yy3);
noFill();
arc(xx2,yy2,35,35,radians(summa1),radians(summa1+lisays),PIE);
float ds = 20*(sade1 + sade2)/2;
float dx = ds*cos(radians(summa3));
float dy = ds*sin(radians(summa3));
fill(0);
pushMatrix();
scale(1,-1);
fill(255,0,0);
text(""+nfc(lisays,1)+"\u00B0",xx2+dx/2,-(yy2+dy/2));
popMatrix();
strokeWeight(2);
fill(R,G,B,A);
} void mittaajana(float x1, float y1, float x2, float y2) { float x12 = map(x1,-10,10,-300,300); float y12 = map(y1,-10,10,-300,300); float x22 = map(x2,-10,10,-300,300); float y22 = map(y2,-10,10,-300,300); float x0 = (x12+x22)/2; float y0 = (y12+y22)/2; float pituus = sqrt((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1)); strokeWeight(5); line(x12,y12,x22,y22); strokeWeight(10); point(x12,y12); point(x22,y22); pushMatrix(); scale(1,-1); fill(255,0,0); text(""+round(10*pituus)/10.0,x0+20,-y0+20); popMatrix(); fill(R,G,B,A); strokeWeight(2); stroke(0); } void mittaasuora(float x1, float y1,float k, float b) { float x12 = map(x1,-10,10,-300,300); float y12 = map(y1,-10,10,-300,300); float b2 = map(b,-10,10,-300,300); float x2 = -300; float y2 = k*x2+b2; float pituus = sqrt((y2-y12)*(y2-y12)+(x2-x12)*(x2-x12)); float x0 = 0; float y0 = 0; for (int s=-3000; s<3000; s++) { float y =k*(s/10.0)+b2; ellipse((s/10.0),y,1,1); float y3 = k*(s/10.0)+b2; float pituus3 = sqrt((y3-y12)*(y3-y12)+((s/10.0)-x12)*((s/10.0)-x12)); if (pituus3 < pituus) { pituus = pituus3; x0 = (s/10); y0 = y3; } } strokeWeight(8); point(x12,y12); point(x0,y0); strokeWeight(3); line(x12,y12,x0,y0); fill(0); pushMatrix(); scale(1,-1); fill(255,0,0); text(""+round(10*(pituus/30.0))/10.0,(x0+x12)/2.0+10,-((y0+y12)/2.0)+25); popMatrix(); strokeWeight(2); fill(R,G,B,A); } void mittaakaari(float x, float y, float sade, float kulma1, float kulma2) { float x2 = map(x,-10,10,-300,300); float y2 = map(y,-10,10,-300,300); float sade2 = map(sade,-10,10,-300,300); noFill(); if (kulma2 < kulma1) { float muuta = kulma1; kulma1 = kulma2; kulma2 = muuta; } arc(x2,y2,2*sade2,2*sade2,radians(kulma1),radians(kulma2)); point(x2,y2); fill(250,200,0,150); // Oletusväri float pituus = ((kulma2-kulma1)/360)*2*PI*sade; float dx = 35*sade*cos(radians((kulma2+kulma1)/2.0)); float dy = 35*sade*sin(radians((kulma2+kulma1)/2.0)); pushMatrix(); scale(1,-1); fill(255,0,0); text(""+round(10*pituus)/10.0,x2+dx,-(y2+dy)); popMatrix(); strokeWeight(2); fill(R,G,B,A); } void mittaahypotenuusa(float x, float y, float a, float b) { float x2 = map(x,-10,10,-300,300); float y2 = map(y,-10,10,-300,300); float a2 = map(a,-10,10,-300,300); float b2 = map(b,-10,10,-300,300); triangle(x2,y2,x2+a2,y2,x2,y2+b2); float pituus = sqrt(a*a+b*b); pushMatrix(); scale(1,-1); fill(255,0,0); text(""+round(10*pituus)/10.0,(x2+(x2+a2))/2,-(y2+(y2+b2))/2); popMatrix(); strokeWeight(2); fill(R,G,B,A); } void mittaakateetti(float x, float y, float a, float c) { float x2 = map(x,-10,10,-300,300); float y2 = map(y,-10,10,-300,300); float a2 = map(a,-10,10,-300,300); float b = sqrt(c*c-a*a); float b2 = map(b,-10,10,-300,300); triangle(x2,y2,x2+a2,y2,x2,y2+b2); float pituus = sqrt(c*c-a*a); pushMatrix(); scale(1,-1); fill(255,0,0); text(""+round(10*pituus)/10.0,x2-35,-(y2+(y2+b2))/2); popMatrix(); strokeWeight(2); fill(R,G,B,A); } void piirtokolmio(float x, float y, float kulma) { fill(0,100,255,100); noStroke(); float h = 150; float x2 = map(x,-10,10,-300,300); float y2 = map(y,-10,10,-300,300); pushMatrix(); translate(x2,y2); rotate(radians(kulma)); triangle(-h,0,h,0,0,h); fill(0); ellipse(0,0,5,5); for (int s = 0; s <= 180; s = s + 10) { float a1 = 100*cos(radians(s)); float b1 = 100*sin(radians(s)); float a2 = 50*cos(radians(s)); float b2 = 50*sin(radians(s)); stroke(0,0,255); strokeWeight(1); line(a1,b1,a2,b2); } for (int s = 0; s <= 180; s = s + 5) { float a1 = 100*cos(radians(s)); float b1 = 100*sin(radians(s)); float a2 = 90*cos(radians(s)); float b2 = 90*sin(radians(s)); stroke(0); strokeWeight(1); line(a1,b1,a2,b2); } for (int s = 0; s <= 180; s = s + 45) { float a1 = 100*cos(radians(s)); float b1 = 100*sin(radians(s)); float a2 = 20*cos(radians(s)); float b2 = 20*sin(radians(s)); stroke(255,0,0); strokeWeight(1); line(a1,b1,a2,b2); } popMatrix(); stroke(0); fill(R,G,B,A); strokeWeight(2); // Oletus paksuus } void viivoitin(float x, float y, float kulma) { fill(100,255,100,100); noStroke(); float x2 = map(x,-10,10,-300,300); float y2 = map(y,-10,10,-300,300); pushMatrix(); translate(x2,y2); rotate(radians(kulma)); rect(0,0,300,30); fill(0); ellipse(0,0,5,5); for (int s=0; s <= 20; s=s+1) { stroke(0); strokeWeight(1); line(s*15,0,s*15,10); } for (int s=0; s <= 10; s=s+1) { stroke(0,0,255); strokeWeight(1); line(s*30,0,s*30,20); } for (int s=0; s <= 100; s=s+1) { stroke(255,0,255); strokeWeight(1); line(s*3,0,s*3,5); } popMatrix(); stroke(0); fill(R,G,B,A); strokeWeight(2); // Oletus paksuus } void kartiomoni(float x, float y, float sade, float h, float lukumaara, float alkukulma) { if (lukumaara > 0) { float x2 = map(x,-10,10,-300,300); float y2 = map(y,-10,10,-300,300); float h2 = map(h,-10,10,-300,300); if (lukumaara == 1) { ellipse(x2,y2,5,5); } else { float lisays = 360.0/lukumaara; float kulma0 = alkukulma; float x1 = sade*30*cos(radians(kulma0)); float y1 = sade*30*sin(radians(kulma0))/2.0; line(x2,y2,x2+x1,y2+y1); float kulma = alkukulma+lisays; beginShape(); for (int s = 0; s < lukumaara; s++) { x1 = sade*30*cos(radians(kulma)); y1 = sade*30*sin(radians(kulma))/2.0; vertex(x2+x1,y2+y1); kulma = kulma+lisays; float x3 = sade*30*cos(radians(kulma)); float y3 = sade*30*sin(radians(kulma))/2.0; triangle(x2+x1,y2+y1,x2,y2+h2,x2+x3,y2+y3); } endShape(); line(x2,y2,x2,y2+h2); } } } void lieriomoni(float x, float y, float sade, float h, float lukumaara, float alkukulma) { if (lukumaara > 0) { float x2 = map(x,-10,10,-300,300); float y2 = map(y,-10,10,-300,300); float h2 = map(h,-10,10,-300,300); if (lukumaara == 1) { ellipse(x2,y2,5,5); } else { float lisays = 360.0/lukumaara; float kulma0 = alkukulma; float x1 = sade*30*cos(radians(kulma0)); float y1 = sade*30*sin(radians(kulma0))/2.0; line(x2,y2,x2+x1,y2+y1); line(x2,y2+h2,x2+x1,y2+y1+h2); float kulma = alkukulma+lisays; beginShape(); for (int s = 0; s < lukumaara; s++) { x1 = sade*30*cos(radians(kulma)); y1 = sade*30*sin(radians(kulma))/2.0; vertex(x2+x1,y2+y1); kulma = kulma+lisays; float x3 = sade*30*cos(radians(kulma)); float y3 = sade*30*sin(radians(kulma))/2.0; quad(x2+x1,y2+y1,x2+x3,y2+y3,x2+x3,y2+y3+h2,x2+x1,y2+y1+h2); } endShape(); line(x2,y2,x2,y2+h2); } } }