Skip to content

Instantly share code, notes, and snippets.

@menangen
Created January 13, 2017 15:40
Show Gist options
  • Save menangen/cdcb5dd212a398f49460ee0307e0694e to your computer and use it in GitHub Desktop.
Save menangen/cdcb5dd212a398f49460ee0307e0694e to your computer and use it in GitHub Desktop.
PlaygroundPage drawing on Apple Swift 3.0.1
//: Playground - noun: a place where people can play
import Cocoa
import PlaygroundSupport
let evening: CGFloat = 0.1
var square_side: CGFloat = 10
var countRects = 50
let radius = 5
struct Matrix {
let x = 29
let y = 25
}
let figure = Matrix()
/*
let sineWave = (0..<sineArraySize).map {
amplitude1 * sin(2.0 * M_PI / Double(sineArraySize) * Double($0) * frequency1 + phase1)
}
func CircleDraw(_ x: Int, _ y: Int) -> Bool {
let xDiff = x - figure.x
let yDiff = y - figure.y
let xQ = xDiff * xDiff
let yQ = yDiff * yDiff
let plot = xQ + yQ
return abs(radius * radius - plot) > radius - 1
}
func BooblicDraw(_ x: Int, _ y: Int) -> Bool {
let radiusQ = radius * radius
let xDiff = x - figure.x
let yDiff = y - figure.y
let xQ = xDiff * xDiff
let yQ = yDiff * yDiff
let one = Float(xQ + yQ) - Float(radiusQ)
let plot = Float(one * one) - 2.0 * Float(radiusQ) * Float(xQ + yQ)
return 0 < plot
}
func CardioidDraw(_ x: Int, _ y: Int) -> Bool {
let radiusQ = radius * radius
let xDiff = x - figure.x
let yDiff = y - figure.y
let xQ = xDiff * xDiff
let yQ = yDiff * yDiff
let one = xQ + yQ + 2 * radius * xDiff
let plot = 4 * radiusQ * (xQ + yQ)
return one * one > plot
}
func NephroidDraw(_ x: Int, _ y: Int) -> Bool {
let radiusQ = Int(pow(Double(radius), 4.0))
let xDiff = x - figure.x
let yDiff = y - figure.y
let xQ = xDiff * xDiff
let yQ = yDiff * yDiff
let one = xQ + yQ - 4 * radius * radius
let plot = 108 * radiusQ * yQ
return one * one * one > plot
}
func Figure1Draw(_ x: Int, _ y: Int) -> Bool {
let radiusQ = radius * radius
let xDiff = x - figure.x
let yDiff = y - figure.y
let xQ = powf(Float(xDiff), 2.0)
let yQ = powf(Float(yDiff), 2.0)
let one = xQ + yQ - Float(2 * radiusQ)
let plot = 2.0 * powf(Float(radiusQ) * yQ, 0.6)
return one * one > plot
}
func Figure2Draw(_ x: Int, _ y: Int) -> Bool {
let radiusQ = radius * radius
let xDiff = x - figure.x
let yDiff = y - figure.y
let xQ = powf(Float(xDiff), 2.0)
let yQ = powf(Float(yDiff), 2.0)
let one = xQ + yQ - Float(radius)
let plot = 4.0 * powf(Float(radiusQ) * yQ, 0.6)
return one * one > plot
}
*/
func NephroidDraw(_ x: Int, _ y: Int) -> Bool {
let radiusQ = Int(pow(Double(radius), 4.0))
let xDiff = x - figure.x
let yDiff = y - figure.y
let xQ = xDiff * xDiff
let yQ = yDiff * yDiff
let one = xQ + yQ - 4 * radius * radius
let plot = 108 * radiusQ * yQ
return one * one * one > plot
}
func isSky(x: Int, y: Int) -> Bool {
return NephroidDraw(x, y)
}
class SquareWithCircleView: NSView{
var Color = NSColor(red: 0.3, green: 0.3, blue: 0.3, alpha: 1.0)
var drawCount: UInt8 = 0
override func draw(_ dirtyRect: NSRect)
{
drawCount += 1
if (drawCount > 1) {
Swift.print("Draw")
for y in 0..<countRects {
for x in 0..<countRects {
var red: CGFloat = 0.1
var green: CGFloat = 0.14
var blue: CGFloat = 0.16
if isSky(x: x, y: y) {
red = 1.0 - CGFloat(y) / CGFloat(countRects)
green = 1.0 - CGFloat(y + x) / CGFloat(countRects * 2)
blue = 1.0 - CGFloat(x) / CGFloat(countRects) / (1.0 + evening)
}
let fillColor = NSColor(red: red, green: green, blue: blue, alpha: 1.0)
fillColor.setFill()
NSRectFill(NSRect(x: (CGFloat(x) + square_side) * square_side, y: (CGFloat(y) + square_side) * square_side, width: square_side, height: square_side))
}
}
}
}
}
var view = SquareWithCircleView(frame:
NSRect(x: 0, y: 0, width: 800, height: 800))
PlaygroundPage.current.liveView = view
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment