public class Ellipse { public static void main(String[] args) { int width = 15; // 椭圆的宽度 int height = 9; // 椭圆的高度

    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            if (isInEllipse(j, i, width, height)) {
                System.out.print('*');
            } else {
                System.out.print(' ');
            }
        }
        System.out.println();
    }
}

// 判断坐标(x, y)是否在椭圆内
private static boolean isInEllipse(int x, int y, int width, int height) {
    double a = width / 2.0; // 长轴
    double b = height / 2.0; // 短轴
    double centerX = width / 2.0; // 椭圆中心点x坐标
    double centerY = height / 2.0; // 椭圆中心点y坐标

    double d = Math.pow((x - centerX) / a, 2) + Math.pow((y - centerY) / b, 2);
    return d <= 1;
}

}

Java 入门练习:打印椭圆图形

原文地址: https://www.cveoy.top/t/topic/jSwc 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录