Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Modify FractalGrammars.java program, so that when the curve turns 90 degrees, it will draw a rounded corner, like the Dragon curve in Figure 8.4 (page

Modify FractalGrammars.java program, so that when the curve turns 90 degrees, it will draw a rounded corner, like the Dragon curve in Figure 8.4 (page 256 of the textbook). Name your file curves.java. Another example that you could test is a variation of Koch curve, with the grammar defined as: (F-F-F-F, FF-F-F-F-F-F+F, nil, nil, nil, 90) The second generation image showing rounded corners "RoundedCorners.png" should be as the second image mentioned below.

image text in transcribed image text in transcribed

Roundedcorners.png

// FractalGrammars.java import java.awt.*; import java.awt.event.*; public class FractalGrammars extends Frame { public static void main(String[] args) { if (args.length == 0) System.out.println("Use filename as program argument."); else new FractalGrammars(args[0]); } FractalGrammars(String fileName) { super("Click left or right mouse button to change the level"); addWindowListener(new WindowAdapter() {public void windowClosing( WindowEvent e){System.exit(0);}}); setSize (800, 600); add("Center", new CvFractalGrammars(fileName)); show(); } } class CvFractalGrammars extends Canvas { String fileName, axiom, strF, strf, strX, strY; int maxX, maxY, level = 1; double xLast, yLast, dir, rotation, dirStart, fxStart, fyStart, lengthFract, reductFact; void error(String str) { System.out.println(str); System.exit(1); } CvFractalGrammars(String fileName) { Input inp = new Input(fileName); if (inp.fails()) error("Cannot open input file."); axiom = inp.readString(); inp.skipRest(); strF = inp.readString(); inp.skipRest(); strf = inp.readString(); inp.skipRest(); strX = inp.readString(); inp.skipRest(); strY = inp.readString(); inp.skipRest(); rotation = inp.readFloat(); inp.skipRest(); dirStart = inp.readFloat(); inp.skipRest(); fxStart = inp.readFloat(); inp.skipRest(); fyStart = inp.readFloat(); inp.skipRest(); lengthFract = inp.readFloat(); inp.skipRest(); reductFact = inp.readFloat(); if (inp.fails()) error("Input file incorrect."); addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent evt) { if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) != 0) { level--; // Right mouse button decreases level if (level  radians dx = len * Math.cos(rad), dy = len * Math.sin(rad); drawTo(g, xLast + dx, yLast + dy); } else turtleGraphics(g, strF, depth - 1, reductFact * len); break; case 'f': // Step forward without drawing // Start: (xLast, yLast), direction: dir, steplength: len if (depth == 0) { double rad = Math.PI/180 * dir, // Degrees -> radians dx = len * Math.cos(rad), dy = len * Math.sin(rad); moveTo(g, xLast + dx, yLast + dy); } else turtleGraphics(g, strf, depth - 1, reductFact * len); break; case 'X': if (depth > 0) turtleGraphics(g, strX, depth - 1, reductFact * len); break; case 'Y': if (depth > 0) turtleGraphics(g, strY, depth - 1, reductFact * len); break; case '+': // Turn right dir -= rotation; break; case '-': // Turn left dir += rotation; break; case '[': // Save position and direction xMark = xLast; yMark = yLast; dirMark = dir; break; case ']': // Back to saved position and direction xLast = xMark; yLast = yMark; dir = dirMark; break; } } } } 
Figure 8.4: Dragon curve of 8th generation, rounded corners

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transact SQL Cookbook Help For Database Programmers

Authors: Ales Spetic, Jonathan Gennick

1st Edition

1565927567, 978-1565927568

More Books

Students also viewed these Databases questions