Drawing Multiple Polygons using java with comments In the ma

Drawing Multiple Polygons, using java, with comments.

In the manyPolygons methods,  write the code to have the turtle draws the specified number of regular polygons, each starting at the same place, however,
after each polygon is drawn the turtle turns 360.0 / numPolys degrees.

Some calls to manyPolygons to try out:

manyPolygons(t, 45, 3, 100)

manyPolygons(t, 60, 30, 20)

Solution

----------------------------------------------------------------------------

// Program to draw polygon, Turle class used in this program is extended in //the main class.

import java.awt.*;      

public class Turtle {

       

        private double x;                             // x position of turtle

        private double y;                             // y position of turtle

        private double heading;                       // heading of turtle, in degrees

        private boolean pendown;                              // is turtle in drawing mode?

        private Color color;                                         // Red is the default

        private Graphics gfx;                 // graphics with which turtle draws

        private Rectangle bounds;                     // bounds of drawing surface

       

        public Turtle() {

               this.color = Color.red; // color used to draw polygon

               home();

               TurtleWorld.currentWorld.addTurtle(this);

        }

       

        public void home() {

               this.x = 0.0;

               this.y = 0.0;

               this.heading = 0.0;

               this.pendown = true;

               this.bounds = new Rectangle(0,0,600,600);

        }

       

        // forward method to move the turtle forward by pixels , which takes //int or double value

        public void forward(double dist) {

               double rads = degreesToRadians(heading);

               this.moveTo(x + (dist * Math.cos(rads)), y + (dist * Math.sin(rads)));

        }

       

        public void foward (int dist) {

               forward((double) dist);

        }

        // Backward

        public void backward(double dist) {

               this.forward(- dist);

        }

       

        public void backward (int dist) {

               backward((double) dist);

        }

        // left method : rotates the tutle towards left by specified angle  

        public void left(double degrees) {

               this.heading = heading + degrees;

        }

       

        public void left (int degrees) {

               left((double) degrees);

        }

        // right method : rotates the tutle towards right by specified angle

        public void right(double angle) {

               this.left(- angle);

        }

       

        public void right (int degrees) {

               right((double) degrees);

        }

        // Pen Up     

        public void pu() {

               this.pendown = false;

        }

        // Pen Down   

        public void pd() {

               this.pendown = true;

        }

       

        // Position

       

        public Point getPosition () {

               return new Point ((int) x, (int) y);

        }

       

        public void setPosition (Point p) {

               x = (double) p.x;

               y = (double) p.y;

        }

       

        public void setPosition (int ix, int iy) {

               x = (double) ix;

               y = (double) iy;

        }

       

        public void setPosition (double dx, double dy) {

               x = dx;

               y = dy;

        }

       

        // Heading

        public double getHeading () {

               return heading;

        }

       

        public void setHeading (double degrees) {

               heading = degrees;

        }

       

        public void setHeading (int degrees) {

               heading = (double) degrees;

        }

       

        // Color

       

        public Color getColor () {

               return color;

        }

       

        public void setColor (Color c) {

               color = c;

        }

       

       

               

        public void moveTo(double newx, double newy) {

               /* For testing purposes:

                       System.out.println(\"Move: heading = \" + heading

                                                     + \"; x = \" + x

                                                     + \"; y = \" + y

                                                     + \"; newx = \" + newx

                                                     + \"; newy = \" + newy);

               */

               if (pendown) {

                       ensureGraphics();

                       this.gfx.setColor(color);

                       this.gfx.drawLine(screenX(x), screenY(y), screenX(newx), screenY(newy));

               }

               x = newx;

               y = newy;

        }

       

        private void ensureGraphics () {

               //System.out.println(\"are we here?\");

               this.gfx = TurtleWorld.currentWorld.getTurtleGraphics();

               //this.bounds = gfx.getClipRect();

               //System.out.println(gfx.getClipBounds());

               //this.bounds = gfx.getClipBounds();

               gfx.getClipBounds(bounds);

               //System.out.println(bounds);

        }

       

        private int screenX(double xcoord) {

               //System.out.println(bounds);

               return (int) Math.round(bounds.x + (bounds.width / 2) + xcoord);

        }

       

        private int screenY(double ycoord) {

               return (int) Math.round(bounds.y + (bounds.height / 2) - ycoord);

        }

       

        private static double degreesToRadians (double degrees) {

               return 2 * Math.PI * (degrees / 360);

        }

       

       

}

Drawing Multiple Polygons, using java, with comments. In the manyPolygons methods, write the code to have the turtle draws the specified number of regular polyg
Drawing Multiple Polygons, using java, with comments. In the manyPolygons methods, write the code to have the turtle draws the specified number of regular polyg
Drawing Multiple Polygons, using java, with comments. In the manyPolygons methods, write the code to have the turtle draws the specified number of regular polyg
Drawing Multiple Polygons, using java, with comments. In the manyPolygons methods, write the code to have the turtle draws the specified number of regular polyg
Drawing Multiple Polygons, using java, with comments. In the manyPolygons methods, write the code to have the turtle draws the specified number of regular polyg

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site