Loading...
Logo
Processing Forum
Hi everybody, 

I'm in a computer programming class and one of our assignments was to make a quadratic formula solver and I found that really easy. I wanted to make it more difficult and give the user the ability to change the a, b, and c in the equation. Here is my code so you can see how it's set up  currently.

Copy code
  1. float a, b, c, x1, x2, placeholder;

  2. void setup() {
  3.   size(400, 200);
  4.   textAlign(CENTER);
  5.   a = 10;
  6.   b = 3.45;
  7.   c = -5;
  8. }

  9. void draw() {
  10.   background(0);
  11.   quadratic_formula();
  12.   display();
  13.   change_cursor();
  14. }

  15. void quadratic_formula() {
  16.   placeholder = b*b;
  17.   placeholder = placeholder-(4*a*c);
  18.   placeholder = sqrt(placeholder);
  19.   x1 = ((-b+placeholder)/(2*a))*100;
  20.   x1 = round(x1);
  21.   x1 = x1/100;
  22.   x2 = ((-b-placeholder)/(2*a))*100;
  23.   x2 = round(x2);
  24.   x2 = x2/100;
  25. }

  26. void display() {
  27.   textSize(30);
  28.   fill(255);
  29.   text("y = "+a+"x^2+"+b+"x+"+c, width/2, height/2);
  30.   text("x = "+x1+" and "+x2, width/2, height*3/4);
  31.   textSize(40);
  32.   fill(255, 50, 50);
  33.   text("a", width/3, height/4);
  34.   fill(50, 255, 50);
  35.   text("b", width/2, height/4);
  36.   fill(50, 50, 255);
  37.   text("c", width*2/3, height/4);
  38. }

  39. void change_cursor() {
  40.   if (dist(mouseX, mouseY, width/3, height/4-15) < 20
  41.     || dist(mouseX, mouseY, width/2, height/4-15) < 20
  42.     || dist(mouseX, mouseY, width*2/3, height/4-15) < 20) {
  43.     cursor(HAND);
  44.   } 
  45.   else {
  46.     cursor(ARROW);
  47.   }
  48. }
I want to be able to press multiple keys in a row and have those numbers change the variables a, b, and c. 

Any help or ideas would be great! 

Thank you

Replies(1)

You want something like a text box...
This is probably a bit beyond the assignment.

I would recommend using ControlP5's text box (yes, that's a library, and it will take a bit to get used to... well beyond the assignment). Alternatively, you could create your own text box... for this, you're on your own.

It is probably possible to create a more primitive input method that wouldn't stretch the bounds of this project as much... you'd need to detect keyPressed() events, for a start...