I have this track in Java using StdDraw. How can I implement limits for the coor
ID: 3729812 • Letter: I
Question
I have this track in Java using StdDraw. How can I implement limits for the coordinates x and y, so it only works within the borders? I was thinking if statements could do it, but I don't know how to implement it because of the corners.
244 245 246 247 public static void track20 I int n 40 int r1- 9, r2-3; int x1 = r1+1, x2 = n/2, x3 = n-(r1+1) ; int y1 = r1+1, y2 = n-2*t1+1, y3 = x3 ; 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 StdDraw.setCanvasSize(800, 800); // Adjust the size of the window StdDraw.setScale(0,n); // Set size of the coordinate system StdDraw.clear; StdDraw.setPenColor(StdDraw. LIGHT_GRAY; StdDraw.filledSquare(n/2.0, n/2.0, n/2.0); StdDraw.setPenColor(StdDraw. BLACK); StdDraw.setPenRadius(9.0/((double)n*50.0)); StdDraw.square(n/2.0, n/2.0,n/2.0); StdDraw.setPenRadius(4.0/(Cdouble)n*100.0)); for(int i = 1; iExplanation / Answer
A simple check through if statement can be included before every call to stdDraw. Let the size of the grid be m x n. First check whether all x coordinates (x1,x2,x3) lie between 0 and m and all y coordinates (y1,y2,y3) lie between 0 and n. After that, whenever these coordinates are incremented (or decremented), check whether those values lie within the above mentioned values. For instance, in the statement: StdDraw.line(x1+r2, y1, x2-r1, y2-1), the following check needs to be included:
if(x1+r2<m && x2-r1>0 && y2-1>0)
{
StdDraw.line(x1+r2, y1, x2-r1, y2-1)
}
This can be done before every StdDraw call or wherever you think is necessary