how to create Vectors and vertices in swift ?

I'm trying to make a hydraulic engineering app for iPad, where you can create a network of lines intersecting with each other in a 2D environment like vectors and vertices and to be able to click them and edit the data base associated with each line or delete it, the app will calculate hydraulic flows and pressures through the network. I'm new to swift and want to know whats the best way to approach it using swift frameworks and calsses? I'm stuck on how to create the graphical vectors (objects) that I can interact with them and add data associated with each line! do you think that Metal is the way to go? the hydraulic calculations behind it will use matrix operations and I found a way through Accelerate frame work, as for data management I worked on core data and it looks great, but I'm stuck in the graphical part on how to create the line from two points and be able to click and interact with it like a button. To be fair it was very hard to find documentations on matrices operations ( bigger than a 4x4 matrix) and now the graphical part looks even harder! so please any help will be appreciated 🙂

Accepted Reply

So, you should first implement the drawing, after studying the tutorials.


After, you should keep in an array [Line] the elements that defines the segments




struct Line {
     var start: CGPoint
     var end: CGPoint


     func isExtremity(point: CGPoint) -> Bool {
     //     you need to accept some margin of uncertainty for point
     }


     func isOnLine(point: CGPoint) -> Bool {
          let x1 = start.x
          let x2 = end.x
          let y1 = start.y
          let y2 = end.y
          let x = point.x
          let y = point.y
          let epsilon = 2.0     // adjust depending on precision you want
          // Test if point is between start and end ;
          // exact test is
          //     x - x1= a * ( x2 - x1)
          // &&
          //     y - y1 = a * ( y2 - y1)
          // &&
          // 0 <= a <= 1
          //     But you need to accept some margin of uncertainty for point
          // A very good solution here:
          // https://stackoverflow.com/questions/7050186/find-if-point-lays-on-line-segment
          // calling start : A, end: B and point P
          // if AB == AP + PB (with some tolerance), P in on segment AB
          let ab = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))
          let ap = sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1))
          let pb = sqrt((x2-x)*(x2-x)+(y2-y)*(y2-y))
          return abs (ab - (ap + pb)) < epsilon
}


var lines = [Line]()


Then, when you touch the view, check, for all elements of lines, if the

- hitpoint isExtremity : then do some action

- isOnLine then do another action

https://stackoverflow.com/questions/7050186/find-if-point-lays-on-line-segment

Replies

how to create Vectors and vertices in swift

Vertcies are vertical. For vectors, do you mean horizontal segment of really a vector of any direction ?


I'm stuck on how to create the graphical vectors (objects) that I can interact with them and add data associated with each line!

You can simply draw in a UIView and layers.

See here for tutorial:

h ttps://www.calayer.com/core-animation/2016/05/22/cashapelayer-in-depth.html

h ttps://www.raywenderlich.com/402-calayer-tutorial-for-ios-getting-started

or

h ttps://www.raywenderlich.com/3096-calayers-tutorial-for-ios-introduction-to-calayers


do you think that Metal is the way to go?

Not sure it is really necessary to start.


the hydraulic calculations behind it will use matrix operations and I found a way through Accelerate frame work, as for data management I worked on core data and it looks great, but I'm stuck in the graphical part on how to create the line from two points and be able to click and interact with it like a button

What type of interaction ? Moving it ? Changing its size ?

One solution would be to add gesture recognizers to each object.

Or to detect where you click in the view, which object was click and act accordingly…


Note. Please, when you post a long text, format it in several paragraphs to make it easier to read.

You should also move this thread to CocoaTouch section.

Thank you very much for your answer and the the time you took to do it, I moved this thread to the cocoa touch and I will edit my questions to several paragraphs.


Vertcies are vertical. For vectors, do you mean horizontal segment of really a vector of any direction ?

I meant like a straight line between two nodes/points with x and y coordinates ( z is always zero). The point/node might have more than one line connecting to it in a 2D plane like a road crossings. The points and the line are like three objects that you can touch and trigger a function. The line is always straight.



What type of interaction ? Moving it ? Changing its size ?

Mainly touch the node or the point that triggers a function and touching the line to trigger another function.



The ideal scenario is to draw lines using touch. The line will always start with a node and end with a node. You can split the line by adding a node in the middle, where you can branch from it another line.

So, you should first implement the drawing, after studying the tutorials.


After, you should keep in an array [Line] the elements that defines the segments




struct Line {
     var start: CGPoint
     var end: CGPoint


     func isExtremity(point: CGPoint) -> Bool {
     //     you need to accept some margin of uncertainty for point
     }


     func isOnLine(point: CGPoint) -> Bool {
          let x1 = start.x
          let x2 = end.x
          let y1 = start.y
          let y2 = end.y
          let x = point.x
          let y = point.y
          let epsilon = 2.0     // adjust depending on precision you want
          // Test if point is between start and end ;
          // exact test is
          //     x - x1= a * ( x2 - x1)
          // &&
          //     y - y1 = a * ( y2 - y1)
          // &&
          // 0 <= a <= 1
          //     But you need to accept some margin of uncertainty for point
          // A very good solution here:
          // https://stackoverflow.com/questions/7050186/find-if-point-lays-on-line-segment
          // calling start : A, end: B and point P
          // if AB == AP + PB (with some tolerance), P in on segment AB
          let ab = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))
          let ap = sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1))
          let pb = sqrt((x2-x)*(x2-x)+(y2-y)*(y2-y))
          return abs (ab - (ap + pb)) < epsilon
}


var lines = [Line]()


Then, when you touch the view, check, for all elements of lines, if the

- hitpoint isExtremity : then do some action

- isOnLine then do another action

https://stackoverflow.com/questions/7050186/find-if-point-lays-on-line-segment

Thank you very much that was extremely helpful 🙂