Polygon transform (25 points). Write a library of static methods that performs various geometric transforms on polygons. Mathematically, a polygon is defined by its sequence of vertices (x0, y 0), (x 1, y 1), (x 2, y 2), …. In Java, we will represent a polygon by storing the x– and y-coordinates of the vertices in two parallel arrays x[] and y[].

Respuesta :

Answer:

import java.lang.*;               //for using Math.cos() and Math.sin() and Math.toRadians() funcitons in rotate method

public class PolygonTransform

{

  public static double[] copy(double[] array)

  {

      double[] newArray = new double[array.length];

      for(int i=0;i<array.length;i++)

      {

          newArray[i] = array[i];

          return newArray;

      }

  }

  public static void scale(double[]x,double[]y,double alpha)

  {

      for(int i=0;i<x.length;i++)

      {

          x[i] *= alpha;

          y[i] *= alpha;

      }

  }

  public static void translate(double[]x,double[]y,double dx,double dy)

  {

      for(int i=0;i<x.length;i++)

      {

          x[i] += dx;

          y[i] += dy;

      }

  }

  public static void rotate(double[] x, double[]y,double theta)

  {

      double rad = Math.toRadians(theta);

      double temp;

      for(int i=0;i<x.length;i++)

      {

          temp = x[i];                           //For storing temporarily the previous value of x before changing so tha it can be used in changing y

          x[i] = x[i]*Math.cos(rad) - y[i]*Math.sin(rad);

          y[i] = y[i]*Math.cos(rad) + temp*Math.sin(rad);  

      }

  }

  public static void main(String args[])

  {

      //This is just implementing you testcase discripted.

      double[]x = new double[4];

      double[]y = new double[4];

      StdDraw.setScale(-5.0, +5.0);

      x = { 0, 1, 1, 0 };

      y = { 0, 0, 2, 1 };

      double alpha = 2.0;

      StdDraw.setPenColor(StdDraw.RED);

      StdDraw.polygon(x, y);

      scale(x, y, alpha);

      StdDraw.setPenColor(StdDraw.BLUE);

      StdDraw.polygon(x, y);

      // Translates polygon by (2, 1).

      StdDraw.setScale(-5.0, +5.0);

      x = { 0, 1, 1, 0 };

      y = { 0, 0, 2, 1 };

      double dx = 2.0, dy = 1.0;

      StdDraw.setPenColor(StdDraw.RED);

      StdDraw.polygon(x, y);

      translate(x, y, dx, dy);

      StdDraw.setPenColor(StdDraw.BLUE);

      StdDraw.polygon(x, y);

      // Rotates polygon 45 degrees.

      StdDraw.setScale(-5.0, +5.0);

      x = { 0, 1, 1, 0 };

      y = { 0, 0, 2, 1 };

      double theta = 45.0;

      StdDraw.setPenColor(StdDraw.RED);

      StdDraw.polygon(x, y);

      rotate(x, y, theta);

      StdDraw.setPenColor(StdDraw.BLUE);

      StdDraw.polygon(x, y);

  }

}

Explanation:

ACCESS MORE