有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

矩阵Java:三角形旋转概念

我做错了什么?我得到了错误的结果(坐标)

一个三角形(见下图:蓝色是原始三角形,莱姆是旋转三角形:一个克隆。边A是一个固定点)

enter image description here 我使用的课程:

重点课程:

public class Point {
   private final double x;
   private final double y;
   public Point(double x, double y) {
      this.x = x;
      this.y = y;
   }
   public double getX() {
      return x;
   }
   public double getY() {
      return y;
   }
   @Override
   public String toString() {
       return "[(" + x + ") (" + y + ")]";
   }
}

抽象的Object2D类:

public abstract class Object2D {
    Point[] point;
    public Point getPoint(int i) {
        return point[i];
    }
    public double getLowestX() {
        return Arrays.asList(point).stream()
            .mapToDouble(Point::getX)
            .min().getAsDouble();
    }
    public double getHighestX() {
        return Arrays.asList(point).stream()
            .mapToDouble(Point::getX)
            .max().getAsDouble();
    }
    public double getLowestY() {
        return Arrays.asList(point).stream()
            .mapToDouble(Point::getY)
            .min().getAsDouble();
    }
    public double getHighestY() {
        return Arrays.asList(point).stream()
            .mapToDouble(Point::getY)
            .max().getAsDouble();
    }
    public double getLength() {
        return getSide(getLowestX(), getHighestX());
    }
    public double getHeight() {
       return getSide(getLowestY(), getHighestY());
    }
    private double getSide(double v1, double v2) {
        return (v1 < v2) ? (0 - v1) + v2 : (0 - v2) + v1;
    }
    @Override
       public String toString() {
       StringBuilder sb = new StringBuilder();
       for (Point pt : point) sb.append(pt).append("\n");
       return sb.toString();
    }
}

直线类:

public class Line extends Object2D {
   public Line(Point point0, Point point1) {
      point = new Point[2];
      point[0] = point0;
      point[1] = point1;
   }
   public double getLineLength() {
      return Math.sqrt(Math.pow(getLength(), 2) + Math.pow(getHeight(), 2));
   }
}

我的三角班:

public class Triangle extends Object2D {
    public Triangle(Point point0, Point point1, Point point2) {
        point = new Point[3];
        point[0] = point0;
        point[1] = point1;
        point[2] = point2;
    }
    public static Triangle getRotatedTriangle(Triangle triangle) {
       Point point0 = triangle.getPoint(0);
       Point point1 = triangle.getPoint(1);
       Point point2 = triangle.getPoint(2);
       Line baseLine = new Line(point0, point1);
       double rotationHeight = baseLine.getHeight();
       double baseLength = baseLine.getLineLength();
       double sinA = rotationHeight / baseLength;
       double angle = Math.asin(sinA);
       double cosA = Math.cos(angle);                  
       point1 = new Point(
            (point1.getX() * cosA - point1.getY() * sinA),
            (point1.getX() * sinA + point1.getY() * cosA));
       point2 = new Point(
            (point2.getX() * cosA - point2.getY() * sinA),
            (point2.getX() * sinA + point2.getY() * cosA));
       return new Triangle(point0, point1, point2);
   }
}

当然,我的主要课程是:

public class TestDrive {
      public static void main(String[] args) {
          Triangle triangle = new Triangle (
             new Point(-6.5, -1.5),
             new Point(2.5, 7.5),
             new Point(6.5, -5.5)
          );
          System.out.println(triangle);
          System.out.println(Triangle.getRotatedTriangle(triangle));
      }
}

共 (1) 个答案

  1. # 1 楼答案

    您在代码中犯了两个错误:

    • 你试图以α角旋转,而你应该以α角旋转(因为你是顺时针旋转的)
    • 乘法矩阵不正确:代码将围绕XY平面原点执行旋转,而不是围绕点a旋转。对于旋转矩阵围绕中心旋转的点(x, y),新旋转点(x', y')的正确公式为(x', y') = R * (x - a, y - b) + (a, b)

    这应该足以让您更正代码。作为参考,以下是我得到的旋转三角形的解决方案:

    [(-6.5) (-1.5)]
    [(6.227922061357855) (-1.5000000000000009)]
    [(-0.1360389693210724) (-13.520815280171309)]