Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // original facebook post:
- // https://www.facebook.com/groups/javaharikrishna/permalink/1596271237165097/
- // JUnit test classes:
- // https://pastebin.com/iiUDpLZG
- // https://pastebin.com/9CCBV1C9
- // Related "Six Sided Star" classes:
- // Implementation:
- // https://pastebin.com/7g2keFVE
- // Tests:
- // https://pastebin.com/zmdNaJ7k
- import java.util.BitSet;
- public class IntersectingTriangles {
- public static void printIntersectingTriangles(final int triangleLines) {
- final int topLines = (triangleLines + 1) / 3;
- printIntersectingTriangles(triangleLines, topLines);
- }
- public static void printIntersectingTriangles(final int triangleLines, final int topLines) {
- if (triangleLines < 0) {
- throw new IllegalArgumentException("Argument value for 'triangleLines' must not be negative.");
- }
- final int totalLineCount = triangleLines + topLines;
- for (int lineIndex = 0; lineIndex < totalLineCount; ++lineIndex) {
- final BitSet starsOnThisLine = new BitSet();
- if (lineIndex < triangleLines) {
- setTriangleBits(starsOnThisLine, triangleLines, lineIndex);
- }
- final int indexFromEnd = totalLineCount - lineIndex - 1;
- if (indexFromEnd < triangleLines) {
- setTriangleBits(starsOnThisLine, triangleLines, indexFromEnd);
- }
- System.out.println(bitsToStars(starsOnThisLine));
- }
- }
- protected static void setTriangleBits(final BitSet starsOnThisLine, final int triangleLines, final int lineIndex) {
- if (lineIndex + 1 < triangleLines) {
- starsOnThisLine.set(triangleLines - lineIndex - 1, true);
- starsOnThisLine.set(triangleLines + lineIndex - 1, true);
- } else {
- for (int bitIndex = 0; bitIndex < triangleLines * 2 - 1; bitIndex += 2) {
- starsOnThisLine.set(bitIndex, true);
- }
- }
- }
- protected static String bitsToStars(final BitSet starsOnThisLine) {
- final StringBuilder result = new StringBuilder();
- for (int bitIndex = 0; bitIndex < starsOnThisLine.length(); ++bitIndex) {
- result.append(starsOnThisLine.get(bitIndex) ? '*' : ' ');
- }
- return result.toString();
- }
- private static String spaces(final int count) {
- return new String(new char[count]).replace('\0', ' ');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement