La espiral logarítmica es una curva matemática que se genera a partir de la relación logarítmica entre su distancia radial y su ángulo polar.
Código java (LogarithmicSpiral.java):
package logarithmicspiral;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class LogarithmicSpiral extends JComponent {
@Override
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    int width = getWidth();
    int height = getHeight();
    g2d.translate(width / 2, height / 2);
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    double theta = 0.5;
    double a = 1;
    double b = 0.12;
    int x1 = 0;
    int y1 = 0;
    g2d.setColor(Color.BLUE);
    for (int i = 0; i <= 500; i++) {
      double r = a * Math.exp(b * theta);
      int x2 = (int) (r * Math.cos(theta));
      int y2 = (int) (r * Math.sin(theta));
      g2d.setStroke(new BasicStroke((float) i / 100));
      g2d.drawLine(x1, y1, x2, y2);
      x1 = x2;
      y1 = y2;
      theta += 0.1;
    }
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new LogarithmicSpiral());
    frame.setSize(800, 800);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}
Resultado: 
Una forma sencilla y rápida de aprender JAVA, observando y deduciendo cómo se comporta el lenguaje a través de ejemplos prácticos.
Archivo del blog
domingo, 12 de febrero de 2023
Espirales. Graficar Espiral Logarítmica.
Suscribirse a:
Comentarios (Atom)
Con la tecnología de Blogger.