I'm writing a graphic calculator via Java GUI (yes I know there's libraries for this already), when the user types in a function I.E. x^6, I want the drawbox to dynamically size itself, according to the minimum and maximum.
For example, if the user enters x^2 from -4 to 4 I have a min of 0 and a max of 16, so I'd want to display the whole graph in the box, and size it properly. Right now the graph is very small and if I use x^6 as the polynomial it looks very cramped.
Here's where I'm at.
Graphics2D g2 = (Graphics2D) g;
double myStart1 = Double.parseDouble(myStart);
double myEnd1 = Double.parseDouble(myEnd);
g2.translate(getWidth() / 2, getHeight() / 2);
g2.scale(5.0, 5.0);
g2.draw(new Line2D.Double( -myStart1 * 100,0, -myEnd1 * 100,0));
if (min < 0) {
g2.draw(new Line2D.Double(0, -min * 100, 0, -max * 100));
} else {
g2.draw(new Line2D.Double(0, min * 100, 0, -max * 100));
}
g2.setColor(Color.RED);
for (int i = 0; i < x.length; i++) {
if (i + 1 < x.length) {
g2.draw(new Line2D.Double(x[i], -y[i], x[i + 1], -y[i + 1]));
} else {
break;
}
}
}