简体   繁体   English

使用抽象形状类的三角形类

[英]Triangle class using an abstract shape class

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

You don't need any math. 您不需要任何数学。 Just pass the degrees to p.turn() . 只需将度数传递给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); 示例:pen.turn(-45); Rotate the pen 45 degrees clockwise. 将笔顺时针旋转45度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM