/* ***************************************************************************** * Name: COS 126 * NetID: cos126 * Precept: P0126 * * Description: Reads pairs of values, the first an (x, y) coordinate pair and * all following are pairs of (dx, dy). Draws line segments starting at (x, y) * and incremented by (dx, dy). Tests for closed paths (i.e., paths that end * the same place they start) and out of bounds segments (which it does not draw). **************************************************************************** */ public class DrawPath { public static void main(String[] args) { // starting point double x = StdIn.readDouble(); double y = StdIn.readDouble(); // save the origin for condition at the bottom double originX = x; double originY = y; // initialize counter here so we can increment for each iteration int segment = 0; while (!StdIn.isEmpty()) { double dx = StdIn.readDouble(); double dy = StdIn.readDouble(); // calculate new coordinates, but don't draw segment yet double newX = x + dx; double newY = y + dy; // only draw segment if within bounds (default is unit square) if (x < 0 || x > 1 || y < 0 || y > 1 || newX < 0 || newX > 1 || newY < 0 || newY > 1) { StdOut.println("Segment " + segment + " out of bounds"); } else { StdDraw.line(x, y, x + dx, y + dy); } // update for next iteration x = newX; y = newY; segment++; } // check if path ends near beginning if (Math.abs(originX - x) > 0.0001 || Math.abs(originY - y) > 0.0001) { StdOut.println("Not closed"); } } }