如何让我的正方形产生彼此的后面,而不是海誓山盟的顶部,而无需使用递归?(How do I make

2019-09-30 03:22发布

我已经得到了在我的问题另一个一些帮助,让我到这地步,但我想知道如何让我的方格后面产生彼此,而不是彼此的顶部。 下面的代码就是我这么远。

这是把它们背后互相递归的代码:

import java.awt.image.*;
import java.awt.Color;
import java.io.*;
import javax.imageio.*;
import java.util.*;
public class FractalDriver {
    static final int SIDE = 1000; // image is SIDE X SIDE
    static BufferedImage image = new BufferedImage(SIDE, SIDE, BufferedImage.TYPE_INT_RGB);
    static final int WHITE = Color.WHITE.getRGB();
    static final int BLACK = Color.BLACK.getRGB();
    static Scanner kbd = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        String fileOut = "helloSquares.png";
        // make image black
        for (int i = 0; i < SIDE; i++) {
            for (int j = 0; j < SIDE; j++) {
                image.setRGB(i, j, BLACK);
            }
        }
        Square square = new Square(SIDE / 2, SIDE / 2, SIDE / 2, WHITE);
        drawSquare(square);

        // save image
        File outputfile = new File(fileOut);
        ImageIO.write(image, "jpg", outputfile);
    }

    public static void drawSquare(Square square) { // center of square is x,y length of side is s
        if (square.side <= 0) { // base case
            return;
        } else {
            // determine corners
            int left = square.center_x - (square.side / 2);
            int top = square.center_y - (square.side / 2);
            int right = square.center_x + (square.side / 2);
            int bottom = square.center_y + (square.side / 2);
            int newColor = square.color - 100000;
            Square newSquareA = new Square(left, top, square.side / 2, newColor);
            Square newSquareB = new Square(left, bottom, square.side / 2, newColor);
            Square newSquareC = new Square(right, top, square.side / 2, newColor);
            Square newSquareD = new Square(right, bottom, square.side / 2, newColor);

            // recursively paint squares at the corners
            drawSquare(newSquareA);
            drawSquare(newSquareB);
            drawSquare(newSquareC);
            drawSquare(newSquareD);
            //placing the for loop here starts at the end and moves toward     the beginning
            //it appears in the png that the squares are behind eachother
            for (int i = left; i < right; i++) {
                for (int j = top; j < bottom; j++) {
                    image.setRGB(i, j, square.color);
                }
            }
        }
    }
}

这是它们打印在海誓山盟顶部的代码:

import java.awt.image.*;
import java.awt.Color;
import java.io.*;
import javax.imageio.*;
import java.util.*;

public class FractalDriver {
    static final int SIDE = 1000; // image is SIDE X SIDE
    static BufferedImage image = new BufferedImage(SIDE, SIDE, BufferedImage.TYPE_INT_RGB);
    static final int WHITE = Color.WHITE.getRGB();
    static final int BLACK = Color.BLACK.getRGB();
    static Scanner kbd = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        String fileOut = "helloSquares.png";
        // make image black
        for (int i = 0; i < SIDE; i++) {
            for (int j = 0; j < SIDE; j++) {
                image.setRGB(i, j, BLACK);
            }
        }
        Square square = new Square(SIDE / 2, SIDE / 2, SIDE / 2, WHITE);
        drawSquare(square);

        // save image
        File outputfile = new File(fileOut);
        ImageIO.write(image, "jpg", outputfile);
    }

    public static void drawSquare(Square square) { // center of square is x,y length of side is s
        if (square.side <= 0) { // base case
            return;
        } else {
            // determine corners
            int left = square.center_x - (square.side / 2);
            int top = square.center_y - (square.side / 2);
            int right = square.center_x + (square.side / 2);
            int bottom = square.center_y + (square.side / 2);
            int newColor = square.color - 100000;
            Square newSquareA = new Square(left, top, square.side / 2, newColor);
            Square newSquareB = new Square(left, bottom, square.side / 2, newColor);
            Square newSquareC = new Square(right, top, square.side / 2, newColor);
            Square newSquareD = new Square(right, bottom, square.side / 2, newColor);
            for (int i = left; i < right; i++) {
                for (int j = top; j < bottom; j++) {
                    image.setRGB(i, j, square.color);
                }
            }
            // recursively paint squares at the corners
            drawSquare(newSquareA);
            drawSquare(newSquareB);
            drawSquare(newSquareC);
            drawSquare(newSquareD);
            //placing the for loop here starts at the end and moves toward the beginning
            //it appears in the png that the squares are behind eachother

        }
    }
}

这里唯一的不同是在for循环的位置(递归调用前一个,一个递归调用后)

这里是同一个程序,但没有递归:

import java.awt.image.*;
import java.awt.Color;
import java.io.*;
import javax.imageio.*;
import java.util.*;
public class TSquare {

    static final int SIDE = 1000; // image is SIDE X SIDE
    static final int WHITE = Color.WHITE.getRGB();
    static final int BLACK = Color.BLACK.getRGB();
    static Scanner kbd = new Scanner(System.in);

    public static void main(String[] args) throws IOException{
        BufferedImage image = new BufferedImage(SIDE, SIDE, BufferedImage.TYPE_INT_RGB);

        drawImage(SIDE, image);
        saveImage(image);
    }
    //#########################################################################
    protected static void drawImage(final int SIDE, BufferedImage image) {
        final int HALF = SIDE / 2;

        //Draw background
        new Square(HALF, HALF, SIDE, BLACK).draw(image);

        //Draw foreground starting with centered half sized square
        Square square = new Square(HALF, HALF, HALF, WHITE);
        drawSquare(square, image);
    }
    //#########################################################################
    protected static void drawSquare(Square square,BufferedImage image) {
        LinkedStack<Square> stack = new LinkedStack<>();
        stack.push(square);
        square.draw(image);
        while (!stack.isEmpty()) {
            square = stack.top();
            stack.pop();
            int half = square.half();
            if (half > 1) {

                int left = square.left();
                int top = square.top();
                int right = square.right();
                int bottom = square.bottom();
                int newColor = square.color - 100000;                

                stack.push(new Square(left, top, half, newColor,square));
                stack.push(new Square(left, bottom, half, newColor,square));
                stack.push(new Square(right, top, half, newColor,square));
                stack.push(new Square(right, bottom, half, newColor,square));
            }
                square.draw(image);  
        }
    }
    //#########################################################################
    public static void saveImage(BufferedImage image) throws IOException {
        String fileOut = "helloSquares.png";        
        File outputfile = new File(fileOut);
        ImageIO.write(image, "jpg", outputfile);
    }

}

Square类:

import java.awt.Color;
import java.awt.image.BufferedImage;
public class Square {
{
    final int BLACK = Color.BLACK.getRGB();
    final int WHITE = Color.WHITE.getRGB();
    protected int center_x;
    protected int center_y;
    protected int side;
    protected int color;
    protected Square parentSquare;

    public Square() {
        this.center_x = 0;
        this.center_y = 0;
        this.side = 0;
        this.color = WHITE;
        this.parentSquare = null;
    }

    public Square(int center_x, int center_y, int side, int color) {
        this.center_x = center_x;
        this.center_y = center_y;
        this.side = side;
        this.color = color;
        this.parentSquare = null;
    }

    public Square(int center_x, int center_y, int side, int color, Square parentSquare) {
        this.center_x = center_x;
        this.center_y = center_y;
        this.side = side;
        this.color = color;
        this.parentSquare = parentSquare;
    }

    public void setX(int center_x) {
        this.center_x = center_x;
    }

    public int getX() {
        return center_x;
    }

    public void setY(int center_y) {
        this.center_x = center_y;
    }

    public int getY() {
        return center_y;
    }

    public void setSide(int side) {
        this.side = side;
    }

    public int getSide() {
        return side;
    }

    public void setColor(int color) {
        this.color = color;
    }

    public int getColor() {
        return color;
    }

    public void setParent(Square parentSquare) {
        this.parentSquare = parentSquare;
    }

    public Square getParent() {
        return parentSquare;
    }

    public int half() {
        return side / 2;
    }

    public int left() {
        return center_x - half();
    }

    public int top() {
        return center_y - half();
    }

    public int right() {
        return center_x + half();
    }

    public int bottom() {
        return center_y + half();
    }

    public void draw(BufferedImage image) {

        int left = left();
        int top = top();
        int right = right();
        int bottom = bottom();

        for (int i = left; i < right; i++) {
            for (int j = top; j < bottom; j++) {
                image.setRGB(i, j, color);
            }
        }
    }

    public boolean contains(int x, int y) {
        int s = this.side;
        // If at least one of the dimensions is negative
        if (s < 0) {
            return false;
        }
        //if coordiante is top left and outside of square
        if (x < (this.center_x / 2) && (y < (this.center_y / 2))) {
            return false;
        }
        //if coordinate is bottom left and outside of square
        if ((x < (this.center_x / 2)) && (y > (this.center_y + (this.center_y / 2)))) {
            return false;
        } //checks up and down from x bounds
        else if (((x > (this.center_x / 2)) && (x < this.center_x + (this.center_x / 2)))
                && (y < (this.center_y / 2)) || y > this.center_y + (this.center_y / 2)) {
            return false;
        }
        //if coordiante is top right and outside of square
        if ((x > (this.center_x + (this.center_x / 2))) && (y < (this.center_y / 2))) {
            return false;
        }
        //if coordinate is bottom right and outside of square
        if ((x > (this.center_x + (this.center_x / 2))) && (y > (this.center_y + (this.center_y / 2)))) {
            return false;
        } //checks left and right from y bounds
        else if (((y > (this.center_y / 2)) && (y < this.center_y + (this.center_y / 2)))
                && (x < (this.center_x / 2)) || x > this.center_x + (this.center_x / 2)) {
            return false;
        }
        return true;
    }
}

所链接的Stack类:

public class LinkedStack<T> implements StackInterface<T>
{
  protected LLNode<T> top;   // reference to the top of this stack

  public LinkedStack()
  {
    top = null;
  }

  public void push(T element)
  // Places element at the top of this stack.
  { 
    LLNode<T> newNode = new LLNode<T>(element);
    newNode.setLink(top);
    top = newNode;
  }     

  public void pop()
  // Throws StackUnderflowException if this stack is empty,
  // otherwise removes top element from this stack.
  {                  
    if (isEmpty())
      throw new StackUnderflowException("Pop attempted on an empty stack.");
    else
      top = top.getLink();
  }

  public T top()
  // Throws StackUnderflowException if this stack is empty,
  // otherwise returns top element of this stack.
  {                 
    if (isEmpty())
      throw new StackUnderflowException("Top attempted on an empty stack.");
    else
      return top.getInfo();
  }

  public boolean isEmpty()
  // Returns true if this stack is empty, otherwise returns false.
  {              
    return (top == null); 
  }

  public boolean isFull()
  // Returns false - a linked stack is never full
  {              
      return false;
  }

}

堆栈溢出类:

public class StackOverflowException extends RuntimeException
{
  public StackOverflowException()
  {
    super();
  }

  public StackOverflowException(String message)
  {
    super(message);
  }
}

堆栈下溢类:

public class StackUnderflowException extends RuntimeException
{
  public StackUnderflowException()
  {
    super();
  }

  public StackUnderflowException(String message)
  {
    super(message);
  }
}

堆栈接口:

public interface StackInterface<T>
{
  void push(T element) throws StackOverflowException;
  // Throws StackOverflowException if this stack is full,
  // otherwise places element at the top of this stack.

  void pop() throws StackUnderflowException;
  // Throws StackUnderflowException if this stack is empty,
  // otherwise removes top element from this stack.

  T top() throws StackUnderflowException;
  // Throws StackUnderflowException if this stack is empty,
  // otherwise returns top element of this stack.

  boolean isEmpty();
  // Returns true if this stack is empty, otherwise returns false.

  boolean isFull();
  // Returns true if this stack is full, otherwise returns false.
}

THE LLNODE类:

public class LLNode<T> {
{
    protected LLNode<T> link;
    protected T info;

    public LLNode(T info) {
        this.info = info;
        link = null;
    }

    public void setInfo(T info) {
        this.info = info;
    }

    public T getInfo() {
        return info;
    }

    public void setLink(LLNode<T> link) {
        this.link = link;
    }

    public LLNode<T> getLink() {
        return link;
    }

    /**
     *Adds a link to a Node
     * 
     * @param newLink       The Link to be added
     */
    public void addLink(LLNode<T> newLink){
        if (link == null){
            this.link = newLink;
        }else{
            LLNode<T> temp = link;
            LLNode<T> pre = null;
            while (temp != null){
                pre = temp;
                temp = temp.link;
            }
        }
    }
}

基本上我想要实现我想修改我上面的代码(不含递归码)来画方格后面的海誓山盟,而不是海誓山盟的顶部类似于第一个FractalDriver类我怎么贴(最顶端的递归一个)涂料他们

Answer 1:

下面是一个迭代求解比递归慢,但像素完美。

我想就使其更快的工作,但我走的时间现在。

希望能帮助到你。

package com.stackoverflow.candied_orange;
import java.awt.image.*;
import java.awt.Color;
import java.io.*;
import javax.imageio.*;
import java.util.*;
public class InFrontInterative {

    public static void main(String[] args) throws IOException{

        final int SIDE = 1000; // image is SIDE X SIDE        
        BufferedImage image = new BufferedImage(SIDE,SIDE,BufferedImage.TYPE_INT_RGB);

        drawImage(SIDE, image);
        saveImage(image, "helloSquares.png");
    }

    //Removed IO to enable unit testing
    protected static void drawImage(final int SIDE, BufferedImage image) {
        final int BLACK = Color.BLACK.getRGB();
        final int WHITE = Color.WHITE.getRGB();
        final int HALF = SIDE / 2;

        //Draw background on whole image
        new Square(HALF, HALF, SIDE, BLACK).draw(image);

        //Draw foreground starting with centered half sized square
        Square square = new Square(HALF, HALF, HALF, WHITE);
        drawFractal(square, image);
    }

    private static void drawFractal(Square square, BufferedImage image){

        Stack<Square> squares = new Stack<>();
        Queue<Square> breeders = new LinkedList<>();
        breeders.add(square);

        //Produce
        while (breeders.size() > 0) {
            square = breeders.remove();

            int half = square.half();

            if (half > 0) {
                System.out.println(half);//TODO remove debugging code 

                int left = square.left();
                int top = square.top();
                int right = square.right();
                int bottom = square.bottom();
                int newColor = square.color - 100000;                

                breeders.add(new Square(left, top, half, newColor));
                breeders.add(new Square(left, bottom, half, newColor));
                breeders.add(new Square(right, top, half, newColor));
                breeders.add(new Square(right, bottom, half, newColor));
                squares.push(square);   
            }            
        }

        //Consume
        while (squares.size() > 0) {
            square = squares.pop();
            square.draw(image);
        }
    }

    protected static void saveImage(BufferedImage image, String fileOut)
    throws IOException {
        File outputfile = new File(fileOut);
        ImageIO.write(image, "jpg", outputfile);
    }    
}


文章来源: How do I make my squares generate behind each other rather than on top of eachother without using recursion?