俄罗斯方块中的形状旋转变形算法 java 动画演示

Test.java:



import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;

public class Test {

     /*
            0  0  0  0  0  0  0  0  0  0  0  0
            0  0  0  0  0  0  0  0  0  0  0  0
            0  0  0  0  0  0  1  1  0  0  0  0
            0  0  0  0  0  0  1  0  0  0  0  0
            0  0  0  1  0  0  1  0  0  0  0  0
            0  0  0  1  1  1  1  1  1  1  0  0
            0  0  0  0  0  0  1  0  0  1  0  0
            0  0  0  0  0  0  1  0  0  0  0  0
            0  0  0  0  0  1  1  0  0  0  0  0
            0  0  0  0  0  0  0  0  0  0  0  0
            0  0  0  0  0  0  0  0  0  0  0  0

            中心点:5行6列
            5行6列             5行6列               5行6列
            4行6列             5行7列               6行6列
            3行6列  ==》        5行8列     ==》      7行6列
            2行6列             5行9列               8行6列
            2行7列             6行9列               8行5列


            0行0列              0行0列              0行0列
            -1行0列             0行1列              1行0列
            -2行0列             0行2列              2行0列
            -3行0列             0行3列              3行0列
            -3行1列             1行3列              3行-1列
         */
    public static void main(String[] args) throws Exception {

        ShapeVO shape1 = new ShapeVO();
        List points = new ArrayList<>();

        points.add( new PointVO( 4,7 ) );
        points.add( new PointVO( 5,7 ) );
        points.add( new PointVO( 5,6 ) );
        points.add( new PointVO( 5,8 ) );

        shape1.setPoints( points );

        // 将 shape1 按照一个点顺时针旋转90度得到 shape2,输出 shape1和 shape2
        PointVO centerPoint = new PointVO( 6,7 );
        ShapeVO shape2 = rotate90DegreesTowardsClockDirection( shape1,centerPoint );
        ShapeVO shape3 = rotate90DegreesTowardsClockDirection(shape2, centerPoint);
        ShapeVO shape4 = rotate90DegreesTowardsClockDirection(shape3, centerPoint);

        drawShape( 20,20,shape1,shape2,shape3,shape4,"C:\\E\\xxx.png" );
    }

    private static void printShape(ShapeVO shape) {
        List points = shape.getPoints();
        for( PointVO point:points ){
            System.out.println( point.getRowNum() + " 行 " + point.getColNum() + " 列" );
        }
    }

    /**
     * 将指定形状以指定点作为中心点顺时针旋转90度
     * @param shape
     * @param centerPoint
     * @return
     */
    private static ShapeVO rotate90DegreesTowardsClockDirection(ShapeVO shape, PointVO centerPoint) {
        //  遍历 shape中的每个点,让每个点的 rowNum、colNum 分别减去 centerPoint 的   rowNum、colNum 得到新的 rowNum、colNum
        // 然后交换  rowNum、colNum 获得新的  rowNum、colNum,然后 新的 colNum 乘以-1获得新的 colNum
        List points = shape.getPoints();
        ShapeVO shape_new = new ShapeVO();
        List points_new = new ArrayList<>();
        for ( PointVO point:points ){
            int rowNum = point.getRowNum() - centerPoint.getRowNum();
            int colNUm = point.getColNum() - centerPoint.getColNum();

            int temp = rowNum;
            rowNum = colNUm;
            colNUm = temp;

            colNUm = -1 * colNUm;

            rowNum = rowNum + centerPoint.getRowNum();
            colNUm = colNUm + centerPoint.getColNum();

            PointVO point_new = new PointVO(rowNum, colNUm);
            points_new.add( point_new );
        }
        shape_new.setPoints( points_new );
        return shape_new;
    }

    public static void  drawShape( int rowCount,int colCount,ShapeVO shape1,ShapeVO shape2,ShapeVO shape3,ShapeVO shape4,String outputPath ) throws IOException {
        int width = 500; // 图片宽度
        int height = 500; // 图片高度
        int cellWidth = width / colCount; // 格子宽度
        int cellHeight = height / rowCount; // 格子高度

        // 创建一个BufferedImage对象,用于绘制图片
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();

        // 绘制网格
        g.setColor(Color.WHITE);
        for (int i = 0; i <= rowCount; i++) {
            int y = i * cellHeight;
            g.drawLine(0, y, width, y);
        }
        for (int i = 0; i <= colCount; i++) {
            int x = i * cellWidth;
            g.drawLine(x, 0, x, height);
        }

        // 填充不同颜色的格子
        if( shape1 != null ){
            g.setColor(Color.RED);
            List points = shape1.getPoints();
            for ( PointVO point:points ){
                g.fillRect(cellWidth * point.getColNum(), cellHeight * point.getRowNum(), cellWidth, cellHeight);
            }
        }

        if( shape2 != null ){
            g.setColor(Color.BLUE);
            List points = shape2.getPoints();
            for ( PointVO point:points ){
                g.fillRect(cellWidth * point.getColNum(), cellHeight * point.getRowNum(), cellWidth, cellHeight);
            }
        }

        if( shape3 != null ){
            g.setColor(Color.GREEN);
            List points = shape3.getPoints();
            for ( PointVO point:points ){
                g.fillRect(cellWidth * point.getColNum(), cellHeight * point.getRowNum(), cellWidth, cellHeight);
            }
        }

        if( shape4 != null ){
            g.setColor(Color.YELLOW);
            List points = shape4.getPoints();
            for ( PointVO point:points ){
                g.fillRect(cellWidth * point.getColNum(), cellHeight * point.getRowNum(), cellWidth, cellHeight);
            }
        }


        // 将图片保存到本地
        File output = new File(outputPath);
        ImageIO.write(image, "png", output);
    }
}

PointVO.java:


import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;


@Getter
@Setter
public class PointVO implements Serializable {

    private int rowNum;
    private int colNum;

    public PointVO(int rowNum, int colNum) {
        this.rowNum = rowNum;
        this.colNum = colNum;
    }
}

ShapeVO.java:

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.List;


@Getter
@Setter
public class ShapeVO implements Serializable {

    private List points;

}

测试输出样例:

俄罗斯方块中的形状旋转变形算法 java 动画演示_第1张图片

俄罗斯方块中的形状旋转变形算法 java 动画演示_第2张图片

俄罗斯方块中的形状旋转变形算法 java 动画演示_第3张图片

俄罗斯方块中的形状旋转变形算法 java 动画演示_第4张图片

你可能感兴趣的:(算法,游戏开发,算法可视化,java,坐标旋转算法,俄罗斯方块,游戏开发,算法,数据结构)