वेलेंटाइन डे के रूप में देखकर तेजी से आ रहा है, मैंने दिल बनाने का फैसला किया। इसलिए मैं this heart from mathematica.se पाया:मैं इस दिल को कैसे ठीक करूं?
मैं Mathematica में चारों ओर खेला (जेड के लिए हल, आसपास कुछ चर स्विचिंग) दिल की z के मूल्य के लिए इस समीकरण प्राप्त करने के लिए, यह देखते हुए x और y मान (क्लिक पूर्ण आकार) के लिए:
मैं ईमानदारी से जावा के लिए इस समीकरण पोर्ट, एक जोड़ी के साथ काम कर बाहर के सीमा मामलों:
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)));
}
}
केवल समस्या यह है कि जब temp1 < 0
, मैं बस -1
वापस नहीं लौट सकते, जैसे मैं करता हूँ है:
if (temp1 < 0) {
return -1; // this is one possible out of bounds location
// this spot is the location of the problem
}
कि उस बिंदु पर दिल का व्यवहार नहीं है। यह है के रूप में, जब मैं अपने छवि बनाने की कोशिश:
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
}
मैं इस मिल: शीर्ष पर है कि डुबकी पर
देखो! मैं एक .5
है कि समस्याग्रस्त -1
बदल रहा करने की कोशिश की, इस में जिसके परिणामस्वरूप:
अब दिल सींग है। लेकिन यह स्पष्ट हो जाता है कि समस्याग्रस्त if
की स्थिति पूरी हो गई है।
मैं इस समस्या को कैसे ठीक कर सकता हूं? मैं शीर्ष पर अपने दिल में एक छेद नहीं चाहता, और मुझे एक सींग का दिल नहीं चाहिए। अगर मैं सींग को दिल के आकार में क्लिप कर सकता हूं, और बाकी को उचित रूप से रंग सकता हूं, तो यह बिल्कुल ठीक होगा। आदर्श रूप से, दिल के दोनों पक्ष एक बिंदु के रूप में एक साथ आते हैं (दिल में शामिल होने पर थोड़ा सा बिंदु होता है), लेकिन यदि वे सींग में दिखाए गए समान रूप से वक्र करते हैं, तो यह भी ठीक होगा। इसे कैसे प्राप्त किया जा सकता है?
'डबल दिल = दिल (xv, yv); '- आप परिणाम की जांच नहीं कर रहे हैं> = 0 –
@ लशाने मैं पूरी तरह से अवगत हूं, लेकिन -1 इसकी सीमा के लिए एक अच्छी, गुलाबी छाया उत्पन्न करता है (मुझसे मत पूछो)। यह उस समस्या को ठीक नहीं करता है जिसके बारे में मैं पूछ रहा हूं। सभी विशेष विशेषीकृत मूल्य गैर-ऋणात्मक संख्याएं उत्पन्न करते हैं (पायथन में चेक किया गया) – Justin