如何解决如何在 Java 多边形上实现透视投影?
我正在尝试用 Java 在多边形上实现 1 点或 2 点透视投影。
我正在使用多边形来塑造 JButton 以具有类似 3D 的外观。
我想把 JButton 改成这样:
为此:
在实现投影之前,我使用此旋转矩阵在 X 轴上旋转多边形:
我使用的变换矩阵如下 (r = - 1/Zc):
我的代码如下:
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();
}
但它不会像我想要的那样旋转多边形。它打印出原始多边形:
如何在 Java-Swing 中实现对多边形的 1 点、2 点、3 点透视投影?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。