Small Numbers Not Working
in
Programming Questions
•
3 months ago
I am writing a program that records user input for the location of two axes of what will become a graph. Once distances from the edge of the window are set, I want the program to create a ratio that will allow the window to be resized so that when it is, the axes are still the same relative distance away from the window borders. If that statement isn't clear enough, I want the user to place (for example) the graph's y axis in the desired location. Then, if the user decreases the width of the window by 50%, the distance between the window's edge and the y axis also decreases by 50% rather than there being a constant difference of space between them (i.e. 32 pixels, or something like that).
I thought that that would be a fairly simple thing to do, but I have been running into problems with, what I assume, is Processing rounding the numbers I need for the ratio to zero. Unless I have made some coding mistake that I missed, the numbers (only two decimal places) put out by the simple line of division always end up being zero.
There is the code, as it stands, in my sketch right now. Although some of it may not make sense, I was doing a bunch of commenting-out for debugging purposes. The only area of interest relative to this problem (I think) is the highlighted region, but I included all of the code so that it's in-context.
Thanks!
~J
I thought that that would be a fairly simple thing to do, but I have been running into problems with, what I assume, is Processing rounding the numbers I need for the ratio to zero. Unless I have made some coding mistake that I missed, the numbers (only two decimal places) put out by the simple line of division always end up being zero.
There is the code, as it stands, in my sketch right now. Although some of it may not make sense, I was doing a bunch of commenting-out for debugging purposes. The only area of interest relative to this problem (I think) is the highlighted region, but I included all of the code so that it's in-context.
Thanks!
~J
- //Initialize variables
PFont font;
int xofy, yofx; //x-cor of y axis, y-cor of x axis
float xrat, yrat; //ratio of difference between window width and x-cor of y axis to window width, ratio of difference between window height and y-cor of x axis to window height
boolean clicked = false;
boolean done = false;
void setup() {
size(800, 500);
frame.setResizable(true);
font = loadFont("CourierNewPSMT-16.vlw");
//font = createFont("CourierNewPSMT", 16, true);
}
void draw() {
//Set up to draw lines and write text
background(0);
fill(255);
stroke(255);
textFont(font, 16);
textAlign(CENTER, CENTER); /*
if(!(done)) { //If selections have not been made
if (clicked) { //If the user has already aligned the y axis
line(xofy, 0, xofy, mouseY); //Draw a line from the top of the window to the mouse's y-cor along the selected x-cor value
line(xofy, mouseY, width, mouseY); //Draw a line from the bottom left corner where the already selected x-cor meets the mouse's y-cor
if (yofx!=0) { //If the mouse has not been clicked a second time
text(width+" pixels by "+height+" pixels are the window dimensions\nx: "+mouseX+" "+xofy+"\ny: "+(height-mouseY)+" "+yofx, width/2, (height/2)-75);
} else {
text(width+" pixels by "+height+" pixels are the window dimensions\nx: "+mouseX+" "+xofy+"\ny: "+(height-mouseY)+" "+(height-mouseY), width/2, (height/2)-75);
}
} else { //If the mouse has not been clicked
line(mouseX, height/2, width, height/2); //Draw a line from the mouse's x-cor to the edge of te window along the center of the window horizontally
line(mouseX, 0, mouseX, height); //Draw a line to the top and bottom of the screen in line with the mouse's x-cor
if (yofx!=0) { //If the mouse has not been clicked a second time
text(width+" pixels by "+height+" pixels are the window dimensions\nx: "+mouseX+" "+mouseX+"\ny: "+(height-mouseY)+" "+yofx, width/2, (height/2)-75);
} else {
text(width+" pixels by "+height+" pixels are the window dimensions\nx: "+mouseX+" "+mouseX+"\ny: "+(height-mouseY)+" "+(height-mouseY), width/2, (height/2)-75);
}
}
} else { *///If the selections for both axes have been made
xofy = 40;
yofx = height-25;
frame.setResizable(false); //Temporarily, the user can no longer resize the window
//line(xofy, yofx, xofy, 0); //Both axes with their finalized points are made
//line(xofy, yofx, width, yofx);
text("Your lines are "+xofy+" pixels from the left window border\nand "+(height-yofx)+" pixels away from the bottom window border.\nThe window is "+width+" pixels by "+height+" pixels.\nClick anywhere to continue.", width/2, height/2);
xrat = (height-yofx)/height; //Ratio of distance between window's edge and y axis to height
yrat = xofy/width; //Ratio of distance between window's edge and x axis to height
//println(xrat+" "+yrat);
/*line(yrat*width, abs((xrat*height)-((1-xrat)*height)), yrat*width, 0);
line(yrat*width, abs((xrat*height)-((1-xrat)*height)), width, abs((xrat*height)-((1-xrat)*height)));*/
}
//}
void mouseClicked() {
if (!(done)) {
if (clicked) {
yofx = mouseY;
done = true;
clicked = false;
} else {
xofy = mouseX;
clicked = true;
}
} else {
//Do nothing
}
}
1