0

So I have this triangle class I need to create using an abstract class. It will also be drawn by a tester class. I am part of the way through it but I am having serious issues with the math portion. I have set the coordinates in the tester class, I have no idea of how to get the pen to turn a certain degree to draw the next side of the triangle. Attached is all the classes and I have so far. Any help will be appreciated.

Tester class

    import TurtleGraphics.*;

      public class TestShapes1 {

        public static void main (String[] args) {

        // Declare and instantiate a pen, a circle and a wheel
        Pen p = new StandardPen();
        //Shape s1 = new Circle1 (20, 20, 20);
        //Shape s2 = new Wheel1 (-20, -20, 20, 6);
        Shape1 t2 = new Triangle1 (0, 0, 50, 0, 0, 30);

        // Draw the circle and wheel
        //s1.draw (p);
        t2.draw (p);
        }

      }

Shape Class

    import TurtleGraphics.Pen;

       public interface Shape1 {
         public double area();
         public void   draw (Pen p);
         public double getXPos();
         public double getYPos();
         public void   move (double xLoc, double yLoc);
         public void   stretchBy (double factor);
         public String toString();
         }

Triangle Class

    import TurtleGraphics.Pen;

      public class Triangle1 implements Shape1 {


    private double x1, y1, x2, y2, x3, y3;
    private double s1, s2, s3;
    private double d1, d2;
    //private double height, width;

    public Triangle1() {
      x1 = 0;
      y1 = 0;
      x2 = 1;
      y2 = 0;
      x3 = 0;
      y3 = 1;
      //height = 1;
      //width = 1;
    } 

    public Triangle1 (double xLoc1, double yLoc1, double xLoc2, double yLoc2, double xLoc3, double yLoc3) {
      x1 = xLoc1;
      y1 = yLoc1;
      x2 = xLoc2;
      y2 = yLoc2;
      x3 = xLoc3;
      y3 = yLoc3;
      //height = h;
      //width = w;
    }

    public double area() {
      return (Math.abs(x1*y2-x2*y1+x2*y3-x3*y2+x3*y1-x1*y3))/2.0;
    }

    public void draw (Pen p) {
      s1 = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
      s2 = Math.sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3));
      s3 = Math.sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1));
      p.up();
      p.move (x1, y1);
      p.down();
      p.setDirection (0); 
      p.move (s1);
      d1 = (Math.acos((s2*s2+s3*s3-s1*s1)/(2.0*s2*s3)))*180/Math.PI;
      p.turn (180 - d1); 
      p.move (s2);
      d2 = (Math.acos((s3*s3+s1*s1-s2*s2)/(2.0*s3*s1)))*180/Math.PI;
      p.turn (180 - d2); 
      p.move (s3);
      p.turn (-90); 
      //p.move ();
    }

    public double getXPos() { 
      return x1;
    }

    public double getYPos() {
      return y1;
    }

    public void move (double xLoc, double yLoc) {
      x1 = x1 + xLoc;
      y1 = y1 + yLoc;
      x2 = x2 + xLoc;
      y2 = y2 + yLoc;
      x3 = x3 + xLoc;
      y3 = y3 + yLoc;
    }

    public void stretchBy (double factor) {
      x1 *= factor;
      y1 *= factor;
    }

    public String toString() {
      String str = "TRIANGLE\n";
    //             + "Width & Height: " + width + " & " + height +"\n"
    //             + "(X,Y) Position: (" + xPos + "," + yPos + ")\n" 
    //             + "Area: " + area();
      return str;
    }
    }
4

1 回答 1

1

You don't need any math. Just pass the degrees to p.turn(). So use

p.turn(180); 

instead of

d1 = (Math.acos((s2*s2+s3*s3-s1*s1)/(2.0*s2*s3)))*180/Math.PI;
p.turn (180 - d1); 

See the documentation for reference:

The degrees can be an integer or floating-point number. Example: pen.turn(-45); Rotate the pen 45 degrees clockwise.

于 2013-04-26T06:16:31.633 回答