User input to change variables.
in
Programming Questions
•
1 year ago
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.
- float a, b, c, x1, x2, placeholder;
- void setup() {
- size(400, 200);
- textAlign(CENTER);
- a = 10;
- b = 3.45;
- c = -5;
- }
- void draw() {
- background(0);
- quadratic_formula();
- display();
- change_cursor();
- }
- void quadratic_formula() {
- placeholder = b*b;
- placeholder = placeholder-(4*a*c);
- placeholder = sqrt(placeholder);
- x1 = ((-b+placeholder)/(2*a))*100;
- x1 = round(x1);
- x1 = x1/100;
- x2 = ((-b-placeholder)/(2*a))*100;
- x2 = round(x2);
- x2 = x2/100;
- }
- void display() {
- textSize(30);
- fill(255);
- text("y = "+a+"x^2+"+b+"x+"+c, width/2, height/2);
- text("x = "+x1+" and "+x2, width/2, height*3/4);
- textSize(40);
- fill(255, 50, 50);
- text("a", width/3, height/4);
- fill(50, 255, 50);
- text("b", width/2, height/4);
- fill(50, 50, 255);
- text("c", width*2/3, height/4);
- }
- void change_cursor() {
- if (dist(mouseX, mouseY, width/3, height/4-15) < 20
- || dist(mouseX, mouseY, width/2, height/4-15) < 20
- || dist(mouseX, mouseY, width*2/3, height/4-15) < 20) {
- cursor(HAND);
- }
- else {
- cursor(ARROW);
- }
- }
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
1