Robotics C++ Physics II AP Physics B Electronics Java Astronomy Other Courses Summer Session  

Epicycloidal Rectangles

 

 

Output

 

 

Code

 

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Image;

 

import java.io.Serializable;

 

public class EpicycloidalRectangles extends java.applet.Applet implements Serializable

{

   private Graphics offScreenBuffer = null;

   private Image    offScreenImage  = null;

 

   private int width      = 800;

   private int height     = 500;

 

   private int basePointX = 300;

   private int basePointY = 200;

 

   private int currentX   = basePointX;

   private int currentY   = basePointY;

 

   private double offsetX = 0;

   private double offsetY = 0;

 

   private double sineAngle1   = 0;

   private double cosineAngle1 = 0;

 

   private double sineAngle2   = 0;

   private double cosineAngle2 = 0;

 

   private double numerator   = 0;

   private double denominator = 0;

 

   private double A = 110;

   private double B = 20;

 

   private int branches  = 1;

   private int colorPartition = 45;

   private int colorOffset = 0;

 

   private double radius = 80;

   private double Radius = 60;

 

   private double frequency = 1.0;

 

   private int eHeight    = 40;

   private int eWidth     = 40;

   private int minEHeight = 40;

   private int maxEHeight = 160;

 

   private int startTheta = (int)(0/frequency);

   private int endTheta   = (int)(1440/frequency);

 

   private int angleSpan = endTheta-startTheta;

 

   private Color[] rectangleColors =

   {

      Color.red, Color.green, Color.blue, Color.yellow,

      Color.white, Color.magenta

   };

 

   private int colorCount = rectangleColors.length;

 

 

   public EpicycloidalRectangles()

   {

   }

  

 

   public void init()

   {

      offScreenImage  = this.createImage(width, height);

      offScreenBuffer = offScreenImage.getGraphics();

 

   }

  

 

   public void update(Graphics gc)

   {

      paint(gc);

 

   } // update

 

 

   public void paint(Graphics gc)

   {

      offScreenBuffer.setColor(Color.lightGray);

      offScreenBuffer.fillRect(0, 0, width, height);

 

      offScreenBuffer.setColor(Color.red);

      drawSpiral(offScreenBuffer, gc);

    //gc.drawImage(offScreenImage, 0, 0, this);

 

   } // paint

 

 

   public void drawSpiral(Graphics gc, Graphics gcMain)

   {

      for(int angle=startTheta; angle<endTheta; angle++)

      {

         if( angle % colorPartition == 0 ) { ++colorOffset; }

 

         sineAngle1   = Math.sin(branches*angle*Math.PI/180);

         cosineAngle1 = Math.cos(branches*angle*Math.PI/180);

 

         sineAngle2   = Math.sin(((A/B)+1)*branches*angle*Math.PI/180);

         cosineAngle2 = Math.cos(((A/B)+1)*branches*angle*Math.PI/180);

 

         offsetX = (A+B)*cosineAngle1 - B*cosineAngle2;

         offsetY = (A+B)*sineAngle1   - B*sineAngle2;

 

         currentX = basePointX+(int)offsetX;

         currentY = basePointY+(int)offsetY;

 

         drawRectangle(gc, angle, currentX, currentY);

         gcMain.drawImage(offScreenImage, 0, 0, this);

      }

 

   }

 

 

   public void drawRectangle(Graphics gc, int x, int xCoord, int yCoord)

   {

      gc.setColor(rectangleColors[((x%2)*2+colorOffset)%colorCount]);

      gc.fillRect(xCoord, yCoord, eWidth, eHeight);

 

   } // drawRectangle

 

} // EpicycloidalRectangles

 

Exercise 1

 

Duplicate  the above graphic

 

Exercise 2

 

Duplicate the following graphic