Herz - Funktion

Thomas Darimont

Erfahrenes Mitglied
Hallo!

Witzig was man mit ein wenig Mathe so alles Zaubern kann ;-)

Java:
package de.tutorials;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;

/**
 * @author Tom
 * 
 */
public class HeartFunction extends JFrame {

    public HeartFunction() {
        super("HeartFunction");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(400, 300);
        setVisible(true);
    }

    public static void main(String[] args) {
        new HeartFunction();
    }

    public void paint(Graphics graphics) {
        Graphics2D g = (Graphics2D) graphics;
        g.fillRect(0, 0, getWidth(), getHeight());
        g.translate(getWidth() / 2, getHeight() / 2 - 50);
        g.rotate(-Math.toRadians(180));
        g.setColor(Color.RED);
        for (double x = -2.0; x < 2.0; x += 0.0001) {
            double yU = upperHeartFunction(x);
            double yL = lowerHeartFunction(x);

            int scaledX = (int) (x * 50);

            int scaledUpperY = (int) (yU * 50);
            int scaledLowerY = (int) (yL * 50);

            g.fillOval(scaledX, scaledUpperY, 3, 3);
            g.fillOval(scaledX, scaledLowerY, 3, 3);
        }

    }

    double lowerHeartFunction(double x) {
        return Math.acos(1 - Math.abs(x)) - Math.PI;
    }

    double upperHeartFunction(double x) {
        return Math.sqrt(1 - Math.pow(Math.abs(x) - 1, 2));
    }
}

Gruß Tom
 

Anhänge

  • 26918attachment.jpg
    26918attachment.jpg
    10 KB · Aufrufe: 300

Neue Beiträge

Zurück