0

I want to create a function that takes any equation and return cgpath. How do I make my condition for the equation a generic one? I have a temporary holder for the equation in the parameter as a String.

Here is the function I wrote which draws a path for the math function (x-4)^2 + (y-3)^2 = 4. I want to move this math equation as a parameter so I can use this function to solve any equation.

func createPath(for equation: String) -> CGPath {
   let path = CGMutablePath()
   var firstPoint = true
   for x in -100...100 {
     for y in -100...100 {
        if pow(Decimal(x - 4), 2) + pow(Decimal(y - 3), 2) == 4 {
            if firstPoint {
                path.move(to: CGPoint(x: x, y: y))
                firstPoint = false
            } else {
                path.addLine(to: CGPoint(x: x, y: y))
            } 
         }
      }
   }
   return path
}
Max2099
  • 1
  • 1
  • See https://stackoverflow.com/q/26719180/1187415 and https://stackoverflow.com/q/31168751/1187415: There is `NSExpression` (which has limited capabilities and no error checking), and third party libraries like DDMathParser. – Martin R Feb 07 '21 at 17:16
  • Thanks, Martin. This helps. – Max2099 Feb 09 '21 at 23:24

0 Answers0