Skip to content

Instantly share code, notes, and snippets.

@Samasaur1
Created November 20, 2019 00:23
Show Gist options
  • Save Samasaur1/0c75ba05a929e734ee4d7600a1ac87f9 to your computer and use it in GitHub Desktop.
Save Samasaur1/0c75ba05a929e734ee4d7600a1ac87f9 to your computer and use it in GitHub Desktop.

Revisions

  1. Samasaur1 created this gist Nov 20, 2019.
    29 changes: 29 additions & 0 deletions N-Gon.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    import Darwin

    func positionOfPoint(_ i: Int, sideLength s: Double, exteriorAngle alpha: Double) -> (x: Double, y: Double) {
    if i == 0 {
    return (0, 0)
    }
    let previousResult = positionOfPoint(i - 1, sideLength: s, exteriorAngle: alpha)
    return (previousResult.x + (s * cos(Double(i - 1) * alpha)), previousResult.y + (s * sin(Double(i - 1) * alpha)))
    }

    positionOfPoint(0, sideLength: 5, exteriorAngle: Double.pi / 2)

    positionOfPoint(1, sideLength: 5, exteriorAngle: Double.pi / 2)
    positionOfPoint(2, sideLength: 5, exteriorAngle: Double.pi / 2)

    positionOfPoint(3, sideLength: 5, exteriorAngle: Double.pi / 2)

    func listOfPoints(sides n: Int, sideLength s: Double) -> [(x: Double, y: Double)] {
    let alpha = Double.pi * 2 / Double(n)

    var array: [(x: Double, y: Double)] = [(0, 0)]
    for i in 1..<n {
    array.append((array[i-1].x + (s * cos(Double(i - 1) * alpha)), array[i-1].y + (s * sin(Double(i - 1) * alpha))))
    }

    return array
    }

    listOfPoints(sides: 4, sideLength: 5)