我该如何修复这颗心?

Jus*_*tin 5 java image-processing

看到情人节快到了,我决定创造一颗心.所以我从mathematica.se找到了这个心脏:

在此输入图像描述

我在Mathematica中玩了(解决了z,切换了一些变量)来得到心脏z值的这个等式,给定x和y值(点击全尺寸):

在此输入图像描述

我忠实地将这个等式移植到Java,处理了几个越界的情况:

import static java.lang.Math.cbrt;
import static java.lang.Math.pow;
import static java.lang.Math.sqrt;

...

public static double heart(double xi, double yi) {
    double x = xi;
    double y = -yi;
    double temp = 5739562800L * pow(y, 3) + 109051693200L * pow(x, 2) * pow(y, 3)
                  - 5739562800L * pow(y, 5);
    double temp1 = -244019119519584000L * pow(y, 9) + pow(temp, 2);
    //
    if (temp1 < 0) {
        return -1; // this is one possible out of bounds location
                   // this spot is the location of the problem
    }
    //
    double temp2 = sqrt(temp1);
    double temp3 = cbrt(temp + temp2);
    if (temp3 != 0) {
        double part1 = (36 * cbrt(2) * pow(y, 3)) / temp3;
        double part2 = 1 / (10935 * cbrt(2)) * temp3;
        double looseparts = 4.0 / 9 - 4.0 / 9 * pow(x, 2) - 4.0 / 9 * pow(y, 2);
        double sqrt_body = looseparts + part1 + part2;
        if (sqrt_body >= 0) {
            return sqrt(sqrt_body);
        } else {
            return -1; // this works; returns -1 if we are outside the heart
        }
    } else {
        // through trial and error, I discovered that this should
        // be an ellipse (or that it is close enough)
        return Math.sqrt(Math.pow(2.0 / 3, 2) * (1 - Math.pow(x, 2)));
    }
}
Run Code Online (Sandbox Code Playgroud)

唯一的问题是,当temp1 < 0我不能简单地回归时-1,就像我一样:

if (temp1 < 0) {
    return -1; // this is one possible out of bounds location
               // this spot is the location of the problem
}
Run Code Online (Sandbox Code Playgroud)

那不是那时心脏的行为.事实上,当我试图制作我的形象时:

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

import static java.lang.Math.cbrt;
import static java.lang.Math.pow;
import static java.lang.Math.sqrt;

public class Heart {
    public static double scale(int x, int range, double l, double r) {
        double width = r - l;
        return (double) x / (range - 1) * width + l;
    }

    public static void main(String[] args) throws IOException {
        BufferedImage img = new BufferedImage(1000, 1000, BufferedImage.TYPE_INT_RGB);
        // this is actually larger than the max heart value
        final double max_heart = 0.679;
        double max = 0.0;
        for (int x = 0; x < img.getWidth(); x++) {
            for (int y = 0; y < img.getHeight(); y++) {
                double xv = scale(x, img.getWidth(), -1.2, 1.2);
                double yv = scale(y, img.getHeight(), -1.3, 1);
                double heart = heart(xv, yv); //this isn't an accident
                // yes I don't check for the return of -1, but still
                // the -1 values return a nice shade of pink: 0xFFADAD
                // None of the other values should be negative, as I did
                // step through from -1000 to 1000 in python, and there 
                // were no negatives that were not -1
                int r = 0xFF;
                int gb = (int) (0xFF * (max_heart - heart));
                int rgb = (r << 16) | (gb << 8) | gb;
                img.setRGB(x, y, rgb);
            }
        }
        ImageIO.write(img, "png", new File("location"));
    }
    // heart function clipped; it belongs here
}
Run Code Online (Sandbox Code Playgroud)

我明白了:

在此输入图像描述

看看顶部的那个倾角!我尝试将有问题的更改-1为a .5,从而导致:

在此输入图像描述

现在心脏有角.但很明显,问题if的条件得到满足.

我该如何解决这个问题?我不希望在我心中有一个洞,我不想要一颗有角的心.如果我可以将角状物剪成心形,并对其余部分进行适当的着色,那就完全没问题了.理想情况下,心脏的两侧会聚在一起作为一个点(心脏在连接处有一点点),但如果它们像角中所示一样弯曲,那也没关系.我怎样才能做到这一点?