• JAVA:实现Mandelbrot Mandelbrot曼德勃罗特集算法(附完整源码)


    JAVA:实现Mandelbrot Mandelbrot曼德勃罗特集算法

    package com.thealgorithms.others;
    
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    
    public class Mandelbrot {
    
        public static void main(String[] args) {
            // Test black and white
            BufferedImage blackAndWhiteImage = getImage(800, 600, -0.6, 0, 3.2, 50, false);
    
            // Pixel outside the Mandelbrot set should be white.
            assert blackAndWhiteImage.getRGB(0, 0) == new Color(255, 255, 255).getRGB();
    
            // Pixel inside the Mandelbrot set should be black.
            assert blackAndWhiteImage.getRGB(400, 300) == new Color(0, 0, 0).getRGB();
    
            // Test color-coding
            BufferedImage coloredImage = getImage(800, 600, -0.6, 0, 3.2, 50, true);
    
            // Pixel distant to the Mandelbrot set should be red.
            assert coloredImage.getRGB(0, 0) == new Color(255, 0, 0).getRGB();
    
            // Pixel inside the Mandelbrot set should be black.
            assert coloredImage.getRGB(400, 300) == new Color(0, 0, 0).getRGB();
    
            // Save image
            try {
                ImageIO.write(coloredImage, "png", new File("Mandelbrot.png"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    
        public static BufferedImage getImage(
                int imageWidth,
                int imageHeight,
                double figureCenterX,
                double figureCenterY,
                double figureWidth,
                int maxStep,
                boolean useDistanceColorCoding) {
            if (imageWidth <= 0) {
                throw new IllegalArgumentException("imageWidth should be greater than zero");
            }
    
            if (imageHeight <= 0) {
                throw new IllegalArgumentException("imageHeight should be greater than zero");
            }
    
            if (maxStep <= 0) {
                throw new IllegalArgumentException("maxStep should be greater than zero");
            }
    
            BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
            double figureHeight = figureWidth / imageWidth * imageHeight;
    
            // loop through the image-coordinates
            for (int imageX = 0; imageX < imageWidth; imageX++) {
                for (int imageY = 0; imageY < imageHeight; imageY++) {
                    // determine the figure-coordinates based on the image-coordinates
                    double figureX = figureCenterX + ((double) imageX / imageWidth - 0.5) * figureWidth;
                    double figureY = figureCenterY + ((double) imageY / imageHeight - 0.5) * figureHeight;
    
                    double distance = getDistance(figureX, figureY, maxStep);
    
                    // color the corresponding pixel based on the selected coloring-function
                    image.setRGB(
                            imageX,
                            imageY,
                            useDistanceColorCoding
                                    ? colorCodedColorMap(distance).getRGB()
                                    : blackAndWhiteColorMap(distance).getRGB());
                }
            }
    
            return image;
        }
    
        /**
         * Black and white color-coding that ignores the relative distance. The
         * Mandelbrot set is black, everything else is white.
         *
         * @param distance Distance until divergence threshold
         * @return The color corresponding to the distance.
         */
        private static Color blackAndWhiteColorMap(double distance) {
            return distance >= 1 ? new Color(0, 0, 0) : new Color(255, 255, 255);
        }
    
        /**
         * Color-coding taking the relative distance into account. The Mandelbrot
         * set is black.
         *
         * @param distance Distance until divergence threshold.
         * @return The color corresponding to the distance.
         */
        private static Color colorCodedColorMap(double distance) {
            if (distance >= 1) {
                return new Color(0, 0, 0);
            } else {
                // simplified transformation of HSV to RGB
                // distance determines hue
                double hue = 360 * distance;
                double saturation = 1;
                double val = 255;
                int hi = (int) (Math.floor(hue / 60)) % 6;
                double f = hue / 60 - Math.floor(hue / 60);
    
                int v = (int) val;
                int p = 0;
                int q = (int) (val * (1 - f * saturation));
                int t = (int) (val * (1 - (1 - f) * saturation));
    
                switch (hi) {
                    case 0:
                        return new Color(v, t, p);
                    case 1:
                        return new Color(q, v, p);
                    case 2:
                        return new Color(p, v, t);
                    case 3:
                        return new Color(p, q, v);
                    case 4:
                        return new Color(t, p, v);
                    default:
                        return new Color(v, p, q);
                }
            }
        }
    
        /**
         * Return the relative distance (ratio of steps taken to maxStep) after
         * which the complex number constituted by this x-y-pair diverges. Members
         * of the Mandelbrot set do not diverge so their distance is 1.
         *
         * @param figureX The x-coordinate within the figure.
         * @param figureX The y-coordinate within the figure.
         * @param maxStep Maximum number of steps to check for divergent behavior.
         * @return The relative distance as the ratio of steps taken to maxStep.
         */
        private static double getDistance(double figureX, double figureY, int maxStep) {
            double a = figureX;
            double b = figureY;
            int currentStep = 0;
            for (int step = 0; step < maxStep; step++) {
                currentStep = step;
                double aNew = a * a - b * b + figureX;
                b = 2 * a * b + figureY;
                a = aNew;
    
                // divergence happens for all complex number with an absolute value
                // greater than 4 (= divergence threshold)
                if (a * a + b * b > 4) {
                    break;
                }
            }
            return (double) currentStep / (maxStep - 1);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
  • 相关阅读:
    尚医通_第12章_用户平台首页数据
    nodejs+vue网上婚纱购物系统elementui
    c++基础2
    Cadence OrCAD Capture交叉参考报表生成方法图文教程
    Pytorch 实战 LESSON 8 单层回归神经网络 & Tensor新手避坑指南
    three.js入门 —— 实现第一个3D案例
    Word编辑论文,实现1.题目、摘要、关键词为通栏,正文为双栏 2.首页底端添加通栏脚注,在脚注中写作者简介,并使其实现悬挂对齐效果
    抖音小店无货源蓝海选品分享,月销十万+的玩法,强烈推荐
    记一次 kotlin 在 MutableList 中使用 remove 引发的问题
    python 进行图片的文字识别
  • 原文地址:https://blog.csdn.net/it_xiangqiang/article/details/126292880