Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Collections;
- /* Quadratic Mode:
- true: a*a*x + b*x + c
- false: a*(x-b)*(x-b) + c
- */
- boolean mode = false;
- float N = 4; // size of parameter space
- float S = 0.5; // resolution of parameter space
- float W = .1; // width to draw, as percentage of N
- float w = N*W;
- float scale = 40;
- int two = 0;
- int one = 0;
- int non = 0;
- float quadratic(float a, float b, float c, float x) {
- if(mode) {
- return a*x*x + b*x + c;
- } else {
- return a*(x-b)*(x-b) + c;
- }
- }
- float qvertex(float a, float b, float c) {
- if(mode) {
- return a == 0 ? 0 : -b/(2*a);
- } else {
- return b;
- }
- }
- int qzeroes(float a, float b, float c) {
- if(mode) {
- return b*b == 4*a*c ? 1 : b*b > 4*a*c ? 2 : 0;
- } else {
- return c == 0 ? 1 : a*c <= 0 ? 2 : 0;
- }
- }
- void setup() {
- size(800,1000);
- noLoop();
- }
- void draw() {
- background(255);
- translate(width/2,height/2);
- scale(scale);
- stroke(0);
- strokeWeight(0);
- rect(0,-100,100,100);
- rect(-100,0,100,100);
- strokeWeight(1/scale);
- ArrayList<float[]> quads = new ArrayList<float[]>();
- for(float a = -N; a <= N; a += S) {
- for(float b = -N; b <= N; b += S) {
- for(float c = -N; c <= N; c += S) {
- float[] quad = {a, b, c};
- quads.add(quad);
- }
- }
- }
- Collections.shuffle(quads);
- for(float[] quad : quads) {
- float a = quad[0];
- float b = quad[1];
- float c = quad[2];
- int z = qzeroes(a, b, c);
- if(z == 2) {
- stroke(0, 0, 255, 50);
- two++;
- } else if(z == 0) {
- stroke(255, 0, 0, 50);
- non++;
- } else {
- stroke(127, 127, 127, 50);
- one++;
- }
- boolean first = true;
- float px = 0;
- float py = 0;
- float vertex = qvertex(a, b, c);
- for(float x = vertex - w; x <= vertex + w; x += 1/scale) {
- float y = quadratic(a, b, c, x);
- if(!first) {
- line(px,py,x,y);
- } else {
- first = false;
- }
- px = x;
- py = y;
- }
- //point(vertex, quadratic(a, b, c, vertex));
- }
- print("Mode:",mode,"\n");
- print("Two: ",two,"",two/(0.+two+one+non),"\n");
- print("One: ",one,"",one/(0.+two+one+non),"\n");
- print("None:",non,"",non/(0.+two+one+non),"\n\n");
- }
- void keyTyped() {
- mode = !mode;
- redraw();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement