Hi! I am currently making a program that draws a variety of graphs, and I’ve finished with the bar graph and line graph. They work well, and I am trying to add another function: a pie graph.
I am making the drawing part first before I put it together with the entire code, but it does not work well. I think there is a problem with the dividing part. However, I cannot find out the problem. I saved the results as a float variable just in case int can not hold the answer, but it still always ends up with 0. Can someone help me find the problem? The code is below. I boldened the part that causes the problem.
import javax.swing.JOptionPane;
PFont f;
void setup()
{
background(0);
size(2000, 4000);
textSize(30);
arr = new IntList();
deg = new IntList();
arr.append(8);
arr.append(9);
arr.append(6);
arr.append(8);
arr.append(9);
arr.append(6);
arr.append(6);
num=7;
//title =
title = "dss";
}
int num=0;
String title = "Untitled #1";
IntList arr;
IntList deg;
int a = 0;
void draw()
{
int hap = 0;//Is the sum(hap means sum in Korean:))
for(int i = 0; i < num; i++){
hap += arr.get(i);
}
//////////////////////////////////////////////////////////////problem part starts//////////////////////////////////////////////////////////////////////
float a;
for(int i = 0; i < num; i++){
a = arr.get(i) / hap;
deg.append((int)a * 360);
println(i+"- a : "+a);
println(i+" : "+deg.get(i));
}
//////////////////problem part ends////////////////////////////
println("hap ="+hap);
int before = deg.get(0);
arc(350, 350, 380, 380, radians(0), radians(deg.get(0)));
for(int i = 1; i < num; i++){
fill(i*107%255, i*61%255, i*162%255);
arc(350, 350, 380, 380, radians(before), radians(deg.get(i)));
before = deg.get(i);
}
}
You have 2 times the variable a. This might cause problems. Once a global int a and then the float a in your method…
What i assume happens :
//Global
int a = 0;
//method
float a;
for(int i = 0; i < num; i++){
a = arr.get(i) / hap; // float version of a is set.
deg.append((int)a * 360);//int version of a (meaning it‘s never set, so only gives 0)
println(i+"- a : "+a); //these might also be the Int version.
println(i+" : "+deg.get(i)); // deg was only set to Int a, which was always 0
}
Oh! Thanks for that, I didn’t realize it. But it doesn’t seem to be the problem…
I am about to get nuts trying to find this thing… Maybe it’s the whole function itself that causes the problem?
sorry, where is a JAVA documentation what talks about that?
first ()a
second a * b
third c + d
anyhow if a program / logic / function not does what you think it should
use heavy diagnostic printing println("function name : variable "+variable);
a good trick is
boolean diagp = true; // false;
//..
if ( diagp ) println("function name : variable "+variable);
To change Division to multiplication with values that were intended for Division you need to take the inverse, which is a division, and therefore basically comes down to the same.
In your example, you have the Original of :
width/2, where 2 is the factor. To get width/0.5, you need to take the inverse of /2, which is 1/2 = 0.5, so you could just as well use width/2 from the beginning, since it ends up with dividing through the value Anyway.