├── swift-canvas.playground ├── timeline.xctimeline ├── contents.xcplayground └── section-1.swift └── README.md /swift-canvas.playground/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /swift-canvas.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | swift-canvas 2 | ============ 3 | small and simple Emoji based drawing program 4 | prints to the console 5 | only uses FP, no OOP 6 | 7 | You can draw the following objects: 8 | * single dots 9 | * vertical lines 10 | * horizontal lines 11 | * empty rectangle (only shows border) 12 | * full (filled) rectangle 13 | 14 | 15 | (make sure to open the assistant editor to view the console) 16 | -------------------------------------------------------------------------------- /swift-canvas.playground/section-1.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Small and simple Emoji based drawing program 3 | // prints to the console 4 | // Only uses FP, no OOP 5 | // 6 | // You can draw the following objects: 7 | // * single dots 8 | // * vertical & horizontal lines 9 | // * Empty rectangle (only shows border) 10 | // * Full rectangle 11 | // 12 | 13 | 14 | 15 | import Cocoa 16 | 17 | // Generates the canvas and returns a 2D array filled with strings 18 | // 19 | // paramters: 20 | // width: Int The width of the canvas 21 | // height: Int The height of the canvas 22 | func generateCanvas(width: Int, height: Int) -> String[][] { 23 | var canvasArray: String[][] = String[][](count: height, repeatedValue: String[]()) 24 | 25 | for i in 0..height { 26 | canvasArray[i] = String[](count:width, repeatedValue: "◻️") 27 | } 28 | 29 | return canvasArray 30 | } 31 | 32 | // Prints the canvas to the console 33 | // 34 | // parameters: 35 | // canvas: String[][] The canvas in question 36 | func printCanvas(canvas: String[][]) { 37 | 38 | var rulerX = "\t" 39 | for i in 1...canvas[0].count { 40 | if i < 10 { rulerX += toString(i) + " " } 41 | else { rulerX += toString(i) + " " } 42 | } 43 | println(rulerX) 44 | 45 | var rulerYcounter = 1 46 | 47 | for i in canvas { 48 | print(toString(rulerYcounter) + "\t") 49 | for j in i { 50 | print(j + " ") 51 | } 52 | println() 53 | 54 | rulerYcounter++ 55 | } 56 | } 57 | 58 | 59 | // Sets the field at a specific CGPoint 60 | // 61 | // parameters: 62 | // field: CGPoint The point to set 63 | // canvas: String[][] The canvas in question 64 | func setLevel(field: CGPoint, canvas: String[][] ) { 65 | canvas[(Int(field.y) - 1)][(Int(field.x) - 1)] = "◼️" 66 | } 67 | 68 | 69 | // Draws a line from point a to point b 70 | // only supports horizontal and vertical lines, no diogonal. 71 | // 72 | // paramters: 73 | // pointA: CGPoint point A 74 | // pointB: CGPoint point B 75 | // canvas: String[][] the canvas in question. 76 | func drawLine(pointA: CGPoint, pointB: CGPoint, canvas: String[][] ) { 77 | if pointA.x == pointB.x { 78 | if pointA.y >= pointB.y { 79 | for i in pointB.y...pointA.y { 80 | setLevel(CGPointMake(pointA.x, i), canvas) 81 | } 82 | } 83 | else if pointB.y >= pointA.y { 84 | for i in pointA.y...pointB.y { 85 | setLevel(CGPointMake(pointA.x, i), canvas) 86 | } 87 | } 88 | } 89 | 90 | else if pointA.y == pointB.y { 91 | if pointA.x >= pointB.x { 92 | for i in pointB.x...pointA.x { 93 | setLevel(CGPointMake(i, pointA.y), canvas) 94 | } 95 | } 96 | else if pointB.x >= pointA.x { 97 | for i in pointA.x...pointB.x { 98 | setLevel(CGPointMake(i, pointA.y), canvas) 99 | } 100 | } 101 | } 102 | } 103 | 104 | 105 | // Draws an empty rectangle (only shows the border) 106 | // 107 | // parameters: 108 | // pointA: CGPoint the top-left corner 109 | // pointB: CGPoint the bottom-right corner 110 | // canvas: String[][] the canvas in question 111 | func drawRectEmpty(pointA: CGPoint, pointB: CGPoint, canvas: String[][] ) { 112 | drawLine(CGPointMake(pointA.x, pointA.y), CGPointMake(pointB.x, pointA.y), canvas) 113 | drawLine(CGPointMake(pointA.x, pointB.y), CGPointMake(pointB.x, pointB.y), canvas) 114 | drawLine(CGPointMake(pointA.x, pointA.y), CGPointMake(pointA.x, pointB.y), canvas) 115 | drawLine(CGPointMake(pointB.x, pointA.y), CGPointMake(pointB.x, pointB.y), canvas) 116 | } 117 | 118 | 119 | // Draws a full (filled) rectangle 120 | // 121 | // parameters: 122 | // pointA: CGPoint the top-left corner 123 | // pointB: CGPoint the bottom-right corner 124 | // canvas: String[][] the canvas in question 125 | func drawRectFull(pointA: CGPoint, pointB: CGPoint, canvas: String[][] ) { 126 | for i in pointA.y...pointB.y { 127 | drawLine(CGPointMake(pointA.x, i), CGPointMake(pointB.x, i), canvas) 128 | } 129 | 130 | } 131 | 132 | // example usage 133 | var example = generateCanvas(20, 20) 134 | drawRectFull (CGPointMake(4, 6), CGPointMake(5, 8), example) 135 | drawRectFull (CGPointMake(16, 6), CGPointMake(17, 8), example) 136 | drawLine (CGPointMake(5, 15), CGPointMake(16, 15), example) 137 | drawLine (CGPointMake(5, 16), CGPointMake(16, 16), example) 138 | drawRectEmpty (CGPointMake(2, 2), CGPointMake(6,9), example) 139 | drawRectEmpty (CGPointMake(19, 2), CGPointMake(15, 9), example) 140 | printCanvas (example) --------------------------------------------------------------------------------