ios – Is there any strategy to invert/flip the outcome determine?

Spread the love


Utilizing iOS – Swift Xcode 14
I’ve drawn a picture utilizing some factors… and I received a picture as output. However the picture is an inverted or a flipped picture.
I used a number of code for flipping/Inverting the picture however not one of the code was working.
by utilizing of some codes the outcome picture going out of the display.
The code supplied right here is 90% appropriate code however the picture is flipped/inverted.

import SceneKit

class StairTwoDSceneViewController: UIViewController {

    var plotPointsArray: [[Double]] = [[0.0,0.0,0.0], [0.229,0.0,0.005], [0.229,-0.0249,0.1926], [0.4566,-0.0249,0.2073], [0.4341,-0.0315,0.3989],[0.6754,-0.0315,0.4039],[0.6513,-0.0315,0.5892], [0.8782,-0.0315,0.5971], [0.8541,-0.0315,0.7834],[1.0765,-0.0315,0.78781]]
    let lineWidth: CGFloat = 0.1 // Modify this worth to extend or lower line thickness
    let cornerRadius: CGFloat = 0.02 // Modify this worth to manage the scale of the nook circles

    override func viewDidLoad() {
        tremendous.viewDidLoad()
        let sceneView = SCNView(body: view.bounds)
        sceneView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        view.addSubview(sceneView)

        let scene = SCNScene()

        if plotPointsArray.rely > 0 {
            let pointsNode = SCNNode()

            for (index, level) in plotPointsArray.enumerated() {
                guard level.rely == 2 || level.rely == 3 else {
                    proceed // Skip invalid factors
                }

                // Modify the y-coordinate to put the inexperienced sphere on the left backside
                let spherePosition = SCNVector3(
                    x: Float(level[0]),
                    y: Float(level.rely == 2 ? -0.0315 : -point[1]), // Flip and alter y-coordinate
                    z: Float(level[point.count - 1])
                )
                let sphereNode = drawSphere(at: spherePosition, radius: cornerRadius, shade: index == 0 ? UIColor.inexperienced : UIColor.purple)
                pointsNode.addChildNode(sphereNode)
            }

            scene.rootNode.addChildNode(pointsNode)

            for i in 0..<plotPointsArray.rely - 1 {
                let currentPoint = plotPointsArray[i]
                let nextPoint = plotPointsArray[i + 1]

                guard currentPoint.rely == nextPoint.rely else {
                    proceed // Skip invalid factors
                }

                let begin = SCNVector3(currentPoint[0], currentPoint.rely == 2 ? -0.0315 : -currentPoint[1], currentPoint[currentPoint.count - 1])
                let finish = SCNVector3(nextPoint[0], nextPoint.rely == 2 ? -0.0315 : -nextPoint[1], nextPoint[nextPoint.count - 1])

                let lineGeometry = SCNGeometry.lineGeometry(from: begin, to: finish)
                let lineNode = SCNNode(geometry: lineGeometry)
                scene.rootNode.addChildNode(lineNode)
            }
        }

        let cameraNode = SCNNode()
        cameraNode.digicam = SCNCamera()
        cameraNode.eulerAngles.x = -Float.pi / 2
        cameraNode.eulerAngles.y = 0
        cameraNode.place = SCNVector3(0.5, 2.5, 0.5)
        scene.rootNode.addChildNode(cameraNode)

        sceneView.scene = scene
        sceneView.backgroundColor = UIColor.black
        sceneView.allowsCameraControl = true
    }



    func drawLine(from begin: SCNVector3, to finish: SCNVector3, thickness: CGFloat, shade: UIColor) -> SCNNode {
        let lineGeometry = SCNGeometry.lineGeometry(from: begin, to: finish)
        lineGeometry.firstMaterial?.diffuse.contents = shade

        let lineNode = SCNNode(geometry: lineGeometry)
        lineNode.place = SCNVector3((begin.x + finish.x) / 2, (begin.y + finish.y) / 2, (begin.z + finish.z) / 2)
        lineNode.eulerAngles.z = -atan2(finish.x - begin.x, finish.y - begin.y)

        return lineNode
    }

    func drawSphere(at place: SCNVector3, radius: CGFloat, shade: UIColor) -> SCNNode {
        let sphereGeometry = SCNSphere(radius: radius)
        let sphereNode = SCNNode(geometry: sphereGeometry)
        sphereNode.place = place
        sphereNode.geometry?.firstMaterial?.diffuse.contents = shade
        return sphereNode
    }
}

extension SCNGeometry {
    static func lineGeometry(from begin: SCNVector3, to finish: SCNVector3) -> SCNGeometry {
        let indices: [Int32] = [0, 1]

        let supply = SCNGeometrySource(vertices: [start, end])
        let factor = SCNGeometryElement(indices: indices, primitiveType: .line)

        return SCNGeometry(sources: [source], parts: [element])
    }
}
extension UIImage {
    func rotate(levels: CGFloat) -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(self.dimension, false, self.scale)
        guard let context = UIGraphicsGetCurrentContext() else {
            return nil
        }
        context.rotate(by: levels * CGFloat.pi / 180)
        self.draw(in: CGRect(origin: .zero, dimension: self.dimension))
        let rotatedImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return rotatedImage
    }
}

Output Image Based on given code

Truly I need the Inverted-image like this under
The Output i need

Be aware : Dont change the factors, the factors getting from backend. I’ve used pattern factors right here

I have to flip/Invert the picture.

Leave a Reply

Your email address will not be published. Required fields are marked *