For our assignment this week, we were tasked with writing the code to develop a program that uses the StdDraw library to print a series of squares that produce a graphical simulation of the Sieve of Eratosthenes, similar to the one on this wiki page:
(http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes)
The program itself I have working, although for numbers past 100, the squares run off the screen. I could submit the code as is, but id prefer to have the code be able to scale the program to the inputted amount of squares, although I'm having trouble divising an algorithm that correctly scales the Standard Draw Java window.
I have tried dividing the user input over 1000 and adding that to the X_MAX axis and subtracting it from the Y_MIN axis in order to create a pinch scale type method, but this wasnt perfect and i also tried making it exponential although this does not work also.
Any idea what alogrithm i could use to correctly scale the window to fit whatever number of squares the user inputs?
My code is below for anyone curious
Thanks
Working: https://i.sstatic.net/HGzJY.jpg Not Working: https://i.sstatic.net/SpVoE.jpg
It looks like "scale" defines the user-coordinate system, so you need to calculate the dimensions of your sieve then set the coordinate system so the sieve's in the middle of it, perhaps with a bit of padding around it.
You need something like this, in the main()
method, before you draw the squares:
double columnWidth = 0.105;
double columnCount = Math.sqrt(input);
double sieveWidth = columnCount * columnWidth;
double sieveHeight = sieveWidth;
double sieveTop = 0.95;
double sieveLeft = 0.045;
double padding = 0.15;
StdDraw.setXscale(sieveLeft - padding, sieveLeft + sieveWidth + padding);
StdDraw.setYscale(sieveTop - sieveHeight - padding, sieveTop + padding);
Make sure you remove any other spurious calls to setXScale()
and setYScale()
.