使用抽象形状类的三角类

时间:2013-04-26 06:11:12

标签: java turtle-graphics

所以我需要使用抽象类创建这个三角形类。它也将由测试者类绘制。我是通过它的方式的一部分,但我有数学部分的严重问题。我在测试器类中设置了坐标,我不知道如何让笔在一定程度上绘制三角形的下一侧。附件是我到目前为止所有的课程。任何帮助将不胜感激。

测试者课程

    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);
        }

      }

形状类

    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();
         }

三角类

    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;
    }
    }

1 个答案:

答案 0 :(得分:1)

你不需要任何数学。只需将度数传递给p.turn()即可。所以使用

p.turn(180); 

而不是

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

请参阅documentation以获取参考:

  

度数可以是整数或浮点数。例:   pen.turn(-45);顺时针旋转笔45度。