Advertisement
fairleyc

Space Filling Curve - Moore Curve

Jul 7th, 2014
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.77 KB | None | 0 0
  1. package com.gmail.lueejava;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5.  
  6. public class DrawSpaceFillingCurve {
  7.     public static void main(String args[]){
  8.         Scanner input = new Scanner(System.in);
  9.         int degree = 0;
  10.         int partX, sizeX = 0;
  11.         int partY, sizeY = 0;
  12.         int orient = 0;
  13.         String pattern = "LFL+F+LFL";
  14.         String changePattern = "";
  15.         ArrayList<StringBuilder> graphicRows = new ArrayList<StringBuilder>();
  16.        
  17.         System.out.println("Moore Curve - http://en.wikipedia.org/wiki/Moore_curve");
  18.         System.out.print("Enter Degree of the Space Filling Curve: ");
  19.         degree = input.nextInt();
  20.         System.out.println();
  21.        
  22.         // Builds Path to Follow
  23.         for(int i = 1; i < degree; i++){
  24.             for(int j = 0; j < pattern.length(); j++){
  25.                 if(pattern.charAt(j) == 'L')
  26.                     changePattern += "-RF+LFL+FR-";
  27.                 if(pattern.charAt(j) == 'R')
  28.                     changePattern += "+LF-RFR-FL+";
  29.                 if(pattern.charAt(j) == 'F' || pattern.charAt(j) == '+' || pattern.charAt(j) == '-')
  30.                     changePattern += pattern.charAt(j);
  31.             }
  32.             pattern = changePattern;
  33.             changePattern = "";
  34.         }
  35.         // Finds Full Size X/Y
  36.         sizeY = (int) (2 * Math.pow(2.0, degree) - 1.0);
  37.         sizeY -= 1; // Start at 0
  38.         sizeX = sizeY;
  39.        
  40.         // Setting Start Point
  41.         partX = (int) (Math.pow(2.0, degree) - 1.0);
  42.         partX -= 1; // Start at 0
  43.         partY = sizeY;
  44.         input.close();
  45.        
  46.         //Forcing Graphic List To Be Full Size
  47.         for(int i = 0; i <= sizeY; i++){
  48.             graphicRows.add(i, new StringBuilder(""));
  49.             graphicRows.get(i).setLength(sizeX+1);
  50.         }
  51.        
  52.        
  53.         //Start ASCII Drawing.
  54.         graphicRows.get(partY).setCharAt(partX, '#');
  55.         for(int i = 0; i < pattern.length(); i++){
  56.             if (pattern.charAt(i) == '+' || pattern.charAt(i) == '-'){
  57.                 orient = changeOrientation(pattern.charAt(i), orient);
  58.             } else if (pattern.charAt(i) == 'F'){
  59.                 for (int j = 0; j < 2; j++){
  60.                     if (orient == 0){
  61.                         partY--;
  62.                     }else if (orient == 1){
  63.                         partX++;
  64.                     }else if (orient == 2){
  65.                         partY++;
  66.                     }else if (orient == 3){
  67.                         partX--;
  68.                     }
  69.                     graphicRows.get(partY).setCharAt(partX, '#'); // Places #
  70.                 }
  71.             }
  72.         }
  73.        
  74.         // Reads Strings and Prints
  75.         for(int i = 0; i < graphicRows.size(); i++){
  76.             for(int j = 0; j < graphicRows.get(i).toString().length(); j++){
  77.                 if(graphicRows.get(i).toString().charAt(j) == '#'){
  78.                     System.out.print("#");
  79.                 }else{
  80.                     System.out.print(" ");
  81.                 }
  82.             }
  83.             System.out.println();
  84.         }
  85.         System.out.println();
  86.         System.out.println("Written by: Colton Fairley");
  87.     }
  88.  
  89.     private static int changeOrientation(char charAt, int orient) {
  90.         if (charAt == '+'){
  91.             if (orient == 3){
  92.                 orient = 0;
  93.             } else {
  94.                 orient++;
  95.             }
  96.         } else if (charAt == '-'){
  97.             if (orient == 0){
  98.                 orient = 3;
  99.             } else {
  100.                 orient--;
  101.             }
  102.         }
  103.         return orient;
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement