package convexHull; import java.awt.*; /** * This class allows to draw scaled graphics. * * @author Pavol Federl */ public class Graphics2D { private Graphics g; private double minx, miny, maxx, maxy; private int outw, outh; /** * Constructor - sets the bounding box for the graphics. * [minx,miny] will be the left bottom corner of the window. * * @param g Graphics associted with this 2D graphics * @param minx left edge * @param maxx right edge * @param miny bottom edge * @param maxy top edge * @param outw output width * @param outh output height */ public Graphics2D( Graphics g, double minx, double miny, double maxx, double maxy, int outw, int outh) { this.g = g; this.minx = minx; this.miny = miny; this.maxx = maxx; this.maxy = maxy; this.outw = outw; this.outh = outh; } /** * Sets the current color. * * @param color new color */ public void setColor( Color color) { g.setColor( color); } /** * Clears given rectangle. * * @param x the x coordinate * @param y the y coordinate * @param w the width of the rectangle * @param h the height of the rectangle */ public void clearRect( double x, double y, double w, double h) { g.clearRect( tx( x), outh - ty(y), tw(w), th(h)); } /** * Draw rectangle. * * @param x the x coordinate * @param y the y coordinate * @param w the width of the rectangle * @param h the height of the rectangle */ public void drawRect( double x, double y, double w, double h) { g.drawRect( tx( x), ty(y), tw(w), th(h)); } /** * Draw line. * * @param x1 the x coordinate of the starting point * @param y1 the y coordinate of the starting point * @param x2 the x coordinate of the ending point * @param y2 the y coordinate of the ending point */ public void drawLine( double x1, double y1, double x2, double y2) { g.drawLine( tx(x1), ty(y1), tx(x2), ty(y2)); } /** * Draw a point with given size. * * @param x the x coordinate of the point * @param y the y coordinate of the point * @param the size of the point in pixels (drawn as a rectangle) */ public void drawPoint( double x, double y, int size) { int ix = tx( x); int iy = ty( y); int hsize = size/2; g.drawRect( ix-hsize, iy-hsize, size, size); } /** * Draw a string at specified coordinates. * * @param s string to draw * @param x the x coordinate of the string * @param y the y coordinate of the string */ public void drawString( String s, double x, double y) { g.drawString( s, tx( x), ty( y)); } /** * Translate the x coordinate into screen coordinates. */ private int tx( double x) { return (int) (((x-minx) / (maxx-minx)) * outw); } /** * Translate the y coordinate into screen coordinates. */ private int ty( double y) { return (int) (((maxy-y) / (maxy-miny)) * outh); } /** * Translate the width in model units into screen units. */ private int tw( double w) { return (int) ((w / (maxx-minx)) * outw); } /** * Translate the height in model units into screen units. */ private int th( double h) { return (int) ((h / (maxy-miny)) * outh); } }