Rysowanie kwadratowej spirali

0

Mam problem z narysowanie takiego rysunku spirali:
user image
Moj program dziala jedynie dla wspolczynika rownego 2. Wszystkie inne wspolczyniki podzialu boku kwadratu nie tworza spirali.

 

class Spirala extends JComponent {
	
	//Main:
	public static void main(String[] a) {
	    JFrame window = new JFrame();
	    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    window.setBounds(30, 30, 800, 800);
	    window.getContentPane().add(new Spirala());
	    window.setVisible(true);
	  }
	
	public void paint(Graphics g) {
		
         Graphics2D g2d = (Graphics2D) g;
         double size = 250;
         g2d.translate(size, size);
         
         int n = 20;  //ilosc
         int wsp = (int) 3; //wspolczynik
         int w = 40; //wielkosc
         
         int[] x = new int[]{w,-w,-w,w};
         int[] y = new int[]{w,w,-w,-w};
         
         g2d.setColor(Color.GREEN);
         g2d.fillPolygon(x, y, 4);
         
         
         for (int i = 0; i < n; i++) {
        	 
             g2d.setColor(i % 2 == 0 ? Color.black : Color.green);
             
             int ax = (x[0]+x[1])/wsp;
             int ay = (y[0]+y[1])/wsp;
             
             int bx = (x[1]+x[2])/wsp;
             int by = (y[1]+y[2])/wsp;
             
             int cx = (x[2]+x[3])/wsp;
             int cy = (y[2]+y[3])/wsp;
             
             int dx = (x[0]+x[3])/wsp;
             int dy = (y[0]+y[3])/wsp;
             
             //Points
             System.out.println("AX " + ax + " " +ay);
             System.out.println("BX " + bx + " " +by);
             System.out.println("CX " + cx + " " +cy);
             System.out.println("DX " + dx + " " +dy);
             System.out.println("--------------------");

             x[0]=ax;
             x[1]=bx;
             x[2]=cx;
             x[3]=dx;
             
             y[0]=ay;
             y[1]=by;
             y[2]=cy;
             y[3]=dy;
             
             //Rysuj
             g2d.fillPolygon(x, y, x.length);
   
         }
	}
}
0

Nie chciało mi się analizować Twojego kodu, a rysowanie tych kwadratów mnie zainteresowało więc napisałem własny kod.
Efekt dla a = 0,11user image

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Spirala extends JFrame 
{
    private JTextField tf = new JTextField("  0.5");
    private boolean draw = false;   
    //------------------------
    public static void main(String[] a) 
    {
        new Spirala();
    }
    //------------------------
    public Spirala()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        panel.add(tf);
        JButton button = new JButton("Rysuj");
        button.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                draw = true;
                repaint();
            }
        });
        panel.add(button);
        add(panel,BorderLayout.SOUTH);
        add(new MyPanel(),BorderLayout.CENTER);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }
    //------------------------
    class MyPanel extends JPanel
    {
        private Color first = new Color(255,211,0);
        private Color second = new Color(0,135,189);
        private int width = 400;
        //--------------------
        public MyPanel()
        {
            setBackground(Color.WHITE);
            setPreferredSize(new Dimension(600, 600));
        }
        //--------------------
        public void paintComponent(Graphics g) 
        {
            super.paintComponent(g);
            if(!draw)
            {
                return;
            }
            Graphics2D g2d = (Graphics2D) g;
            g2d.translate(300, 300);
 
            int n = 20;  //ilosc
            int w = width; //wielkosc
            float a = Float.parseFloat(tf.getText().trim());
            float wsp = (float)Math.hypot(a,1-a);
            
            g2d.setColor(first);
            g2d.fillRect(-200,-200,width,width);
            for (int i = 0; i < n; i++) 
            {
                w = Math.round(w*wsp);
                g2d.rotate(Math.atan(a/(1-a)));
                g2d.setColor(i % 2 == 0 ? second : first);
                g2d.fillRect(-w/2,-w/2, w, w); 
            }
        }
    }
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1