fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. }
  14. }
Success #stdin #stdout 0.09s 54612KB
stdin
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
import java.util.*;
import java.util.function.*;

public class WaveguidePlot {

    // 你的参数:E21 (TM21), a=23mm, b=10mm, f=36GHz
    static double a = 23e-3;
    static double b = 10e-3;
    static double f = 36e9;
    static double c = 3e8;
    static double m = 2;   // TM21 的 m
    static double n = 1;   // TM21 的 n

    static double fc, lam, lam_g, beta, kc, omega;
    static final double eps0 = 8.854e-12;

    public static void main(String[] args) throws IOException {
        // 1. 计算常数
        fc = c / 2 * Math.sqrt(Math.pow(m / a, 2) + Math.pow(n / b, 2));
        lam = c / f;
        lam_g = lam / Math.sqrt(1 - Math.pow(fc / f, 2));
        beta = 2 * Math.PI / lam_g;
        kc = Math.PI * Math.sqrt(Math.pow(m / a, 2) + Math.pow(n / b, 2));
        omega = 2 * Math.PI * f;

        // 2. 构建 HTML 网页(内嵌 Base64 图片)
        StringBuilder html = new StringBuilder();
        html.append("<html><head><meta charset='UTF-8'><title>波导场分布图 (E21)</title>");
        html.append("<style>body{font-family:sans-serif;text-align:center;} img{max-width:100%;width:400px;margin:10px;border:1px solid #ccc;}</style>");
        html.append("</head><body>");
        html.append("<h1>E21 (TM21) 模场分布图</h1>");
        html.append("<p>参数:a=23mm, b=10mm, f=36GHz</p>");

        // --- 任务3:xy 平面 (z = 0, λ/8, λ/4, λ/2) ---
        html.append("<h2>任务3:xy 平面大尺寸场分布 (|E|)</h2>");
        double[] zVals = {0, lam / 8, lam / 4, lam / 2};
        for (double z : zVals) {
            String data = generateImageData(0, a, 0, b,
                    "xy, z=" + String.format("%.2f λ", z / lam),
                    "x (mm)", "y (mm)",
                    (x, y) -> getMag(x, y, z));
            html.append("<img src='data:image/png;base64,").append(data).append("'>");
        }

        // --- 任务3:xz 平面 (y = 0, b/8, b/4, b/2) ---
        html.append("<h2>任务3:xz 平面大尺寸场分布 (|E|)</h2>");
        double[] yVals = {0, b / 8, b / 4, b / 2};
        for (double yy : yVals) {
            String data = generateImageData(0, a, 0, lam / 2,
                    "xz, y=" + String.format("%.2f b", yy / b),
                    "x (mm)", "z (mm)",
                    (x, z) -> getMag(x, yy, z));
            html.append("<img src='data:image/png;base64,").append(data).append("'>");
        }

        // --- 任务3:zy 平面 (x = 0, a/8, a/4, a/2) ---
        html.append("<h2>任务3:zy 平面大尺寸场分布 (|E|)</h2>");
        double[] xVals = {0, a / 8, a / 4, a / 2};
        for (double xx : xVals) {
            String data = generateImageData(0, b, 0, lam / 2,
                    "zy, x=" + String.format("%.2f a", xx / a),
                    "y (mm)", "z (mm)",
                    (y, z) -> getMag(xx, y, z));
            html.append("<img src='data:image/png;base64,").append(data).append("'>");
        }

        html.append("</body></html>");

        // 3. 写入文件 output.html
        try (FileWriter fw = new FileWriter("output.html")) {
            fw.write(html.toString());
        }
        System.out.println("✅ 成功生成 output.html!请用浏览器打开查看所有场图。");
    }

    // 计算总场幅值 |E| = sqrt(Ex² + Ey² + Ez²)
    static double getMag(double x, double y, double z) {
        double sin_m = Math.sin(m * Math.PI * x / a);
        double sin_n = Math.sin(n * Math.PI * y / b);
        double cos_m = Math.cos(m * Math.PI * x / a);
        double cos_n = Math.cos(n * Math.PI * y / b);
        double E0 = 1.0;

        // 实部(t=0时刻)
        double Ez = E0 * sin_m * sin_n * Math.cos(beta * z);
        double coeff_x = -(beta * m * Math.PI / a) / (kc * kc) * E0;
        double Ex = coeff_x * cos_m * sin_n * (-Math.sin(beta * z));
        double coeff_y = -(beta * n * Math.PI / b) / (kc * kc) * E0;
        double Ey = coeff_y * sin_m * cos_n * (-Math.sin(beta * z));

        return Math.sqrt(Ex * Ex + Ey * Ey + Ez * Ez);
    }

    // 生成单张热力图,返回 Base64 字符串
    static String generateImageData(double xmin, double xmax, double ymin, double ymax,
                                    String title, String xlabel, String ylabel,
                                    BiFunction<Double, Double, Double> field) throws IOException {
        int W = 400, H = 300;
        BufferedImage img = new BufferedImage(W, H, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = img.createGraphics();

        // 白色背景
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, W, H);

        // 计算所有点的场值,找最大值
        double maxVal = 0;
        double[][] vals = new double[W][H];
        for (int i = 0; i < W; i++) {
            for (int j = 0; j < H; j++) {
                double x = xmin + (xmax - xmin) * i / (W - 1);
                double y = ymin + (ymax - ymin) * (H - 1 - j) / (H - 1);
                double v = field.apply(x, y);
                vals[i][j] = v;
                if (v > maxVal) maxVal = v;
            }
        }

        // 绘制热力图(Jet 颜色映射)
        for (int i = 0; i < W; i++) {
            for (int j = 0; j < H; j++) {
                double val = vals[i][j] / (maxVal + 1e-9);
                if (Double.isNaN(val)) val = 0;
                if (val < 0) val = 0;
                if (val > 1) val = 1;
                img.setRGB(i, j, getJetColor(val));
            }
        }

        // 绘制边框和文字
        g.setColor(Color.BLACK);
        g.drawRect(20, 20, W - 40, H - 40);
        g.drawString(title, 25, 15);
        g.drawString(xlabel, W / 2 - 20, H - 5);
        g.drawString(ylabel, 5, H / 2);
        g.dispose();

        // 转为 Base64
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(img, "png", baos);
        byte[] bytes = baos.toByteArray();
        return Base64.getEncoder().encodeToString(bytes);
    }

    // Jet 颜色映射
    static int getJetColor(double v) {
        int r, g, b;
        if (v <= 0.25) {
            r = 0;
            g = (int) (v * 4 * 255);
            b = 255;
        } else if (v <= 0.5) {
            r = 0;
            g = 255;
            b = (int) ((1 - (v - 0.25) * 4) * 255);
        } else if (v <= 0.75) {
            r = (int) ((v - 0.5) * 4 * 255);
            g = 255;
            b = 0;
        } else {
            r = 255;
            g = (int) ((1 - (v - 0.75) * 4) * 255);
            b = 0;
        }
        return (r << 16) | (g << 8) | b;
    }
}
stdout
Standard output is empty