How to use Functions and Buttons
in
Programming Questions
•
10 months ago
The assignment:
Using buttons/rollovers, determine if user wants an area of a square opposed to an area of circle (Circle button shows area of the circle and square button shows the area of the square)
Randomly generate a number from 1 to 25
In your program have two functions: one for area of circle and the other one for the area of square
In your program display the user input, the random number and the area
Thanks in advance
Update:
int r = random(1,26);
println("The Random Number is " +r)
Void setup() {}
Update 2:
I'm really sorry if I broke any rules. I was only looking for the quick answer because I had no time to do it. Now I have an extension till Thursday. I have everything down such has the random number, the shapes all I don't know how to do is make the shapes buttons and the fuctions.
//random number
int r = (int)random(1, 26);
//size and the display of the number
void setup() {
size(400, 400);
println("The Random Number is "+r);
}
//background and the shapes
void draw(){
background(255);
stroke(0);
fill(0);
ellipse(300, 300, 100, 100);
fill (0);
rect(50, 50, 100, 100);
}
Update 2:
I'm really sorry if I broke any rules. I was only looking for the quick answer because I had no time to do it. Now I have an extension till Thursday. I have everything down such has the random number, the shapes all I don't know how to do is make the shapes buttons and the fuctions.
//random number
int r = (int)random(1, 26);
//size and the display of the number
void setup() {
size(400, 400);
println("The Random Number is "+r);
}
//background and the shapes
void draw(){
background(255);
stroke(0);
fill(0);
ellipse(300, 300, 100, 100);
fill (0);
rect(50, 50, 100, 100);
}
Update 3: As stated by asimes, I have changed the title. I apologize again, I am new to the forum and programming.
Asimes thanks for your help so far but I think I need to use the randomly generated number to find area of square when I click on square and same goes for the circle thanks a lot again
Update 4: I have use the randomly generated number (r) so when i rollover square it shows the area of square using the random number and same thing for the circle as well i tried doing it by myself but kep-t getting errpr
int r = (int) random(1, 26);
int circleX = 300, circleY = 300;
int circleSize = 100;
int rectX = 50, rectY = 50;
int rectSize = 100;
void setup() {
size(450, 400);
println("The Random Number is " + r);
}
void draw() {
background(255);
fill(255,0,0);
ellipse(circleX, circleY, circleSize, circleSize);
float ellipseDist = dist(circleX, circleY, mouseX, mouseY);
if (ellipseDist < circleSize / 2)
{
float area = calculateCircleArea(circleSize);
text("The Area of the Circle is " + nf(area, 1, 2), circleX - circleSize / 2, circleY + circleSize / 2 + 30);
}
fill(0,0,255);
rect(rectX, rectY, rectSize, rectSize);
if (mouseX > rectX && mouseX < rectX + rectSize) {
if (mouseY > rectY && mouseY < rectY + rectSize) {
float area = 45;
text("The Area of the Square is " + nf(area, 1, 2), rectX, rectY + rectSize + 45);
}
}
}
float calculateCircleArea(float size)
{
return size * 3;
}
Update 4: I have use the randomly generated number (r) so when i rollover square it shows the area of square using the random number and same thing for the circle as well i tried doing it by myself but kep-t getting errpr
int r = (int) random(1, 26);
int circleX = 300, circleY = 300;
int circleSize = 100;
int rectX = 50, rectY = 50;
int rectSize = 100;
void setup() {
size(450, 400);
println("The Random Number is " + r);
}
void draw() {
background(255);
fill(255,0,0);
ellipse(circleX, circleY, circleSize, circleSize);
float ellipseDist = dist(circleX, circleY, mouseX, mouseY);
if (ellipseDist < circleSize / 2)
{
float area = calculateCircleArea(circleSize);
text("The Area of the Circle is " + nf(area, 1, 2), circleX - circleSize / 2, circleY + circleSize / 2 + 30);
}
fill(0,0,255);
rect(rectX, rectY, rectSize, rectSize);
if (mouseX > rectX && mouseX < rectX + rectSize) {
if (mouseY > rectY && mouseY < rectY + rectSize) {
float area = 45;
text("The Area of the Square is " + nf(area, 1, 2), rectX, rectY + rectSize + 45);
}
}
}
float calculateCircleArea(float size)
{
return size * 3;
}
1