Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nesting parabolic curves from straight lines

I am having trouble replicating the picture for my assignment. I would appreciate any tips or solutions. I believe I have the general idea down, but I am having a hard time figuring out the math to replicate the image and doing everything in a single loop.

My program needs to meet these criteria:

  1. Match the Image
  2. Generate a random color for each pair of parabolic curve
  3. Work with any width or height
  4. Use a single loop to draw the entire figure.

Here is the image: Image This is what I have tried so far

public static final int WIDTH = 500;
public static final int HEIGHT = 500;
public static final int LINE_INCREMENT = 5;

public static void main(String[] args) {

    DrawingPanel panel = new DrawingPanel(WIDTH, HEIGHT);
    Graphics g = panel.getGraphics();
    int d = 0;

    int iterations = HEIGHT/LINE_INCREMENT;
    Random rand = new Random();
    int red = 0, green = 0, blue = 0;
    red = rand.nextInt(128) + 128;
    green = rand.nextInt(128) + 128;
    blue = rand.nextInt(128) + 128;
    g.setColor(new Color(red,green,blue));

        for(int y = 0; y < iterations; y++) {
            g.drawLine(0, d, d, HEIGHT);
            g.drawLine(WIDTH, d, d, 0);

            d += LINE_INCREMENT;
        }

        red = rand.nextInt(128) + 128;
        green = rand.nextInt(128) + 128;
        blue = rand.nextInt(128) + 128;
        g.setColor(new Color(red,green,blue));
        d = 0;

        for (int x = 0; x < iterations/2; x++) {
            g.drawLine(WIDTH/4, d + HEIGHT/4, d + WIDTH/4, HEIGHT - HEIGHT/4);
            g.drawLine(d + WIDTH/4, WIDTH/4, WIDTH - WIDTH/4, d + WIDTH/4);

            d += LINE_INCREMENT;

        }           
}

The output:

Result

like image 393
Sneakypanda Avatar asked Nov 08 '22 11:11

Sneakypanda


1 Answers

The way to implement it is by overriding paintComponent:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ParabolicCurves extends JFrame {

    private static final int SIZE = 600;
    private static final int LINE_INCREMENT = 5;
    private static final int NUM_OF_PATTERNS = 4;
    private Random rand = new Random();

    ParabolicCurves()   {

        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        JPanel panel = new DrawingPanel(SIZE);
        add(panel, BorderLayout.CENTER);
        pack();
        setVisible(true);
    }

    class DrawingPanel extends JPanel{

        public DrawingPanel(int size) {
            setPreferredSize(new Dimension(size, size));
            setBackground(Color.WHITE);
        }

        @Override
        public void paintComponent(Graphics g) {

            super.paintComponent(g);

            int red, green, blue, delta , iterations;
            int height, width ,startX, startY, endX, endY ;
            Rectangle boundingRrectangle = getBounds();

            for(int pattern = 0 ; pattern < NUM_OF_PATTERNS; pattern++) {

                red = rand.nextInt(128) + 128;
                green = rand.nextInt(128) + 128;
                blue = rand.nextInt(128) + 128;
                g.setColor(new Color(red,green,blue));

                height = (int) boundingRrectangle.getHeight();
                width = (int) boundingRrectangle.getWidth();
                startX = (int) boundingRrectangle.getX();
                startY = (int) boundingRrectangle.getY();
                endX = startX+width;
                endY = startY+ height;

                iterations = Math.min(width, height)/LINE_INCREMENT;
                delta = 0;
                for (int x = 0; x < iterations ; x++) {

                    g.drawLine(startX, startY+delta, startX+delta, endY);
                    g.drawLine(endX, startY+delta, startX+delta, startY);
                    delta += LINE_INCREMENT;
                }
                //change bounding rectangle 
                boundingRrectangle = new Rectangle(startX+(width/4),
                                                startY+(width/4), width/2, height/2);
            }
        }
    }

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

Output:

enter image description here

like image 165
c0der Avatar answered Nov 15 '22 05:11

c0der