如何在 Java 多边形上实现透视投影?

问题描述

我正在尝试用 Java 在多边形上实现 1 点或 2 点透视投影。

我正在使用多边形来塑造 JButton 以具有类似 3D 的外观。

我想把 JButton 改成这样:

enter image description here

为此:

enter image description here

在实现投影之前,我使用此旋转矩阵在 X 轴上旋转多边形:

enter image description here

我使用的变换矩阵如下 (r = - 1/Zc):

enter image description here

我的代码如下:

private polygon polygon = new polygon();
private void setpolygon() {
    int xPoints[] = {0,getWidth()/3,getWidth(),getWidth()/3};
    int yPoints[] = {getHeight()/2,getHeight()/4,getHeight()*3/4,getHeight()};
    polygon = new polygon(xPoints,yPoints,xPoints.length);
    AffineTransform forSkew = new AffineTransform();
    PathIterator polygonPath = polygon.getPathIterator(forSkew);
    int[] xPointsPathIterator = new int[0];
    int[] yPointsPathIterator = new int[0];
    double cosG = Math.cos(Math.toradians(25));
    double sinG = Math.sin(Math.toradians(25));
    while(!polygonPath.isDone()) {
        double point[] = new double[2];
        int type = polygonPath.currentSegment(point);
        
        xPointsPathIterator = Arrays.copyOf(xPointsPathIterator,xPointsPathIterator.length+1);
        yPointsPathIterator = Arrays.copyOf(yPointsPathIterator,yPointsPathIterator.length+1);
        
        //Rotation - start
        int newX = (int) (point[0]);
        int newY = (int) (cosG*point[1]); // z is equal to 0 at first
        int newZ = (int) (-sinG*point[1]);
        //Rotation - end
        
        double h = 1 - newZ/(150); // we give center point the value z = 150;
        
        xPointsPathIterator[xPointsPathIterator.length-1] = (int)(newX/ h );
        yPointsPathIterator[yPointsPathIterator.length-1] = (int)(newY/ h );
        polygonPath.next();
    }

但它不会像我想要的那样旋转多边形。它打印出原始多边形:

enter image description here

如何在 Java-Swing 中实现对多边形的 1 点、2 点、3 点透视投影?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)