Advertisement
JeffGrigg

IntersectingTriangles

Jul 22nd, 2018
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.41 KB | None | 0 0
  1. // original facebook post:
  2. // https://www.facebook.com/groups/javaharikrishna/permalink/1596271237165097/
  3.  
  4. // JUnit test classes:
  5. // https://pastebin.com/iiUDpLZG
  6. // https://pastebin.com/9CCBV1C9
  7.  
  8. // Related "Six Sided Star" classes:
  9. // Implementation:
  10. // https://pastebin.com/7g2keFVE
  11. // Tests:
  12. // https://pastebin.com/zmdNaJ7k
  13.  
  14. import java.util.BitSet;
  15.  
  16. public class IntersectingTriangles {
  17.  
  18.     public static void printIntersectingTriangles(final int triangleLines) {
  19.         final int topLines = (triangleLines + 1) / 3;
  20.         printIntersectingTriangles(triangleLines, topLines);
  21.     }
  22.  
  23.     public static void printIntersectingTriangles(final int triangleLines, final int topLines) {
  24.         if (triangleLines < 0) {
  25.             throw new IllegalArgumentException("Argument value for 'triangleLines' must not be negative.");
  26.         }
  27.  
  28.         final int totalLineCount = triangleLines + topLines;
  29.         for (int lineIndex = 0; lineIndex < totalLineCount; ++lineIndex) {
  30.             final BitSet starsOnThisLine = new BitSet();
  31.             if (lineIndex < triangleLines) {
  32.                 setTriangleBits(starsOnThisLine, triangleLines, lineIndex);
  33.             }
  34.             final int indexFromEnd = totalLineCount - lineIndex - 1;
  35.             if (indexFromEnd < triangleLines) {
  36.                 setTriangleBits(starsOnThisLine, triangleLines, indexFromEnd);
  37.             }
  38.             System.out.println(bitsToStars(starsOnThisLine));
  39.         }
  40.     }
  41.  
  42.     protected static void setTriangleBits(final BitSet starsOnThisLine, final int triangleLines, final int lineIndex) {
  43.         if (lineIndex + 1 < triangleLines) {
  44.             starsOnThisLine.set(triangleLines - lineIndex - 1, true);
  45.             starsOnThisLine.set(triangleLines + lineIndex - 1, true);
  46.         } else {
  47.             for (int bitIndex = 0; bitIndex < triangleLines * 2 - 1; bitIndex += 2) {
  48.                 starsOnThisLine.set(bitIndex, true);
  49.             }
  50.         }
  51.     }
  52.  
  53.     protected static String bitsToStars(final BitSet starsOnThisLine) {
  54.         final StringBuilder result = new StringBuilder();
  55.         for (int bitIndex = 0; bitIndex < starsOnThisLine.length(); ++bitIndex) {
  56.             result.append(starsOnThisLine.get(bitIndex) ? '*' : ' ');
  57.         }
  58.         return result.toString();
  59.     }
  60.  
  61.     private static String spaces(final int count) {
  62.         return new String(new char[count]).replace('\0', ' ');
  63.     }
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement