Advertisement
Guest User

Processing snippet for answer to math.se q 3307156

a guest
Jul 30th, 2019
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1. import java.util.Collections;
  2.  
  3. /* Quadratic Mode:
  4.      true:  a*a*x + b*x + c
  5.      false: a*(x-b)*(x-b) + c
  6. */
  7. boolean mode = false;
  8.  
  9. float N = 4; // size of parameter space
  10. float S = 0.5; // resolution of parameter space
  11. float W = .1; // width to draw, as percentage of N
  12.  
  13. float w = N*W;
  14. float scale = 40;
  15.  
  16. int two = 0;
  17. int one = 0;
  18. int non = 0;
  19.  
  20. float quadratic(float a, float b, float c, float x) {
  21.   if(mode) {
  22.     return a*x*x + b*x + c;
  23.   } else {
  24.     return a*(x-b)*(x-b) + c;
  25.   }
  26. }
  27. float qvertex(float a, float b, float c) {
  28.   if(mode) {
  29.     return a == 0 ? 0 : -b/(2*a);
  30.   } else {
  31.     return b;
  32.   }
  33. }
  34. int qzeroes(float a, float b, float c) {
  35.   if(mode) {
  36.     return b*b == 4*a*c ? 1 : b*b > 4*a*c ? 2 : 0;
  37.   } else {
  38.     return c == 0 ? 1 : a*c <= 0 ? 2 : 0;
  39.   }
  40. }
  41.  
  42. void setup() {
  43.   size(800,1000);
  44.   noLoop();
  45. }
  46.  
  47. void draw() {
  48.   background(255);
  49.   translate(width/2,height/2);
  50.   scale(scale);
  51.   stroke(0);
  52.   strokeWeight(0);
  53.   rect(0,-100,100,100);
  54.   rect(-100,0,100,100);
  55.   strokeWeight(1/scale);
  56.  
  57.   ArrayList<float[]> quads = new ArrayList<float[]>();
  58.   for(float a = -N; a <= N; a += S) {
  59.     for(float b = -N; b <= N; b += S) {
  60.       for(float c = -N; c <= N; c += S) {
  61.         float[] quad = {a, b, c};
  62.         quads.add(quad);
  63.       }
  64.     }
  65.   }
  66.  
  67.   Collections.shuffle(quads);
  68.  
  69.   for(float[] quad : quads) {
  70.     float a = quad[0];
  71.     float b = quad[1];
  72.     float c = quad[2];
  73.     int z = qzeroes(a, b, c);
  74.     if(z == 2) {
  75.       stroke(0, 0, 255, 50);
  76.       two++;
  77.     } else if(z == 0) {
  78.       stroke(255, 0, 0, 50);
  79.       non++;
  80.     } else {
  81.       stroke(127, 127, 127, 50);
  82.       one++;
  83.     }
  84.     boolean first = true;
  85.     float px = 0;
  86.     float py = 0;
  87.     float vertex = qvertex(a, b, c);
  88.     for(float x = vertex - w; x <= vertex + w; x += 1/scale) {
  89.       float y = quadratic(a, b, c, x);
  90.       if(!first) {
  91.         line(px,py,x,y);
  92.       } else {
  93.         first = false;
  94.       }
  95.       px = x;
  96.       py = y;
  97.     }
  98.     //point(vertex, quadratic(a, b, c, vertex));
  99.   }
  100.  
  101.   print("Mode:",mode,"\n");
  102.   print("Two: ",two,"",two/(0.+two+one+non),"\n");
  103.   print("One: ",one,"",one/(0.+two+one+non),"\n");
  104.   print("None:",non,"",non/(0.+two+one+non),"\n\n");
  105. }
  106.  
  107. void keyTyped() {
  108.   mode = !mode;
  109.   redraw();
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement