Post

Replies

Boosts

Views

Activity

Reply to swift compiler error
You don't show enough so that we can tell exactly: what does getCovidData(for: scope) return ? how is result defined (its struct)? how is dayData defined in the class (self) ? But likely, self?.dayData is an array of DayData and dayData is DayData Hence the error. You should have something like self?.dayData.append(dayData) Note: naming var with very meaningful term is very important to avoid coding errors. Hence, naming dayData something that is array in the same way that a single element is very confusing. You'd better change for instance as: var dayDatas : [DayData] or var dayDataSeries : [DayData] or var dayDataArray : [DayData] Siomething to remind you it is an array. With this, you would immediately see that this is wrong: self?.dayDataSeries = dayData
Topic: Programming Languages SubTopic: Swift Tags:
Oct ’21
Reply to Help! with String Interpolation
When you paste code, you'd better use Paste and Match Style, to avoid all extra blank lines. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "calCell", for: indexPath) as! CalendarCell cell.dayOfMonth.text = totalSquares[indexPath.item] //show the weekend days in different color switch indexPath.item { case 0,6,7,13,14,20,21,27,28,34,35,41: if Int(cell.dayOfMonth.text = "\(indexPath.item)" > 0 { cell.dayOfMonth.textColor = UIColor.lightGray } default: break } return cell } Where do you get the error ? On second line ? If so, I suspect totalSquares is an array of Int, not array of Strings. Then you should write: cell.dayOfMonth.text = String(totalSquares[indexPath.item])   There is probably another error. What do you want to test here: if Int(cell.dayOfMonth.text = "\(indexPath.item)" > 0 { Do you mean: if Int(cell.dayOfMonth.text) == "\(indexPath.item)" { But then what is the > 0 test ? Do you mean if Int(cell.dayOfMonth.text) == "\(indexPath.item)" && indexPath.item > 0 { But then just take 0 out of the case statement ; 0 will be handled in default. Please clarify
Topic: Programming Languages SubTopic: Swift Tags:
Oct ’21
Reply to Help! with String Interpolation
The error "Cannot convert value of type 'String' to expected argument type 'Int'" means that a String "\(indexPath.item)" should be converted to an Int to be compared to an Int (0). Which cannot be done. But don't you get a lot more errors? As: I am trying to get the string interpolation to equal the cells position. That's not very clear. In particular, what the test > 0 is for in your code. But as it is a test, I understand you want to test if the month equals the cell position ? If so, you should test: if Int(cell.dayOfMonth.text) == indexPath.item {
Topic: Programming Languages SubTopic: Swift Tags:
Oct ’21
Reply to Triangle Intersection in 3D Space
Some suggestion. Your triangles T1 (A1-B1-C1) and T2 (A2-B2-C2) are in Planes (P1 and P2). In 3D, those planes are defined by equations: (P1) : a1 x + b1 y + c1 z + d1 = 0 (P2) : a2 x + b2 y + c2 z + d2 = 0 You find a, b, c, d by resolving the system with 3 unknown, considering that each vertices of a triangle belongs to the plan. For instance A1 is in P1: a1 xA1 + b1 yA1 + c1 zA1+ d1 = 0 You will have to consider 2 cases: d = 0 (the origin is in the plan : you completely resolve the system of 3 equations d ≠ 0 Origin is out of plan : divide consider d = 1 to resolve Then the intersection of T1 and T2 is in fact intersection of P1 and P2, which is usually a line (may be empty if parallel plans or plan if plans are the same) The intersection is given by: a1 x + b1 y + c1 z + d1 = 0 a2 x + b2 y + c2 z + d2 = 0 solution may be expressed as: x = a t + xA y = b t + yA z = c t + zA Hope that helps.
Topic: Programming Languages SubTopic: Swift Tags:
Oct ’21
Reply to Connect Label to View Controller
I want to add a label to the view controller Do you mean adding an IBOutlet for the label that you have create in the storyboard ? with a label it won't work. What happens exactly ? Likely solution: Option-drag (alt) will duplicate the object (button, label) in the storyboard. That's not correct here. You have to control-drag from the Label (in the storyboard) to the code, inside the class. You will get a popup window asking to give a name to the IBOutlet. Note: Check that you have given the right class to the ViewController in the Storyboard: it must be the exact same name as the class in which you want to create the IBOutlet.
Oct ’21
Reply to How to set TableColumn in Table using ForEach
What do you get ? What did you expect ? The syntax includes a value: struct Person: Identifiable { let givenName: String let familyName: String let id = UUID() } @State private var people = [ Person(givenName: "Juan", familyName: "Chavez"), Person(givenName: "Mei", familyName: "Chen"), Person(givenName: "Tom", familyName: "Clark"), Person(givenName: "Gita", familyName: "Kumar"), ] TableColumn("Given Name", value: \Person.givenName) { person in Text(person.givenName) } So you should likely have this:             TableColumn(item.name, value : \.XXXXX.value) { data in Text(data.value)             } where XXXX is the struct containing value.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Oct ’21
Reply to Triangle Intersection in 3D Space
First, you didn't answer my question on what you are looking for exactly ! Back to your new question: But how do I solve for those 4 vars.(a,b,c,d) with only one equation You don't have one equation, you have 3. Triangle T1 (with vertices A1, B1, C1) in plane P1: A1 is (xA1, yA1, zA1) B1 is (xB1, yB1, zB1) C1 is (xC1, yC1, zC1) A1, B1, C1 are in plane P1; hence it satisfies 3 equations: a1 xA1 + b1 yA1 + c1 zA1+ d1 = 0 a1 xB1 + b1 yB1 + c1 zB1+ d1 = 0 a1 xC1 + b1 yC1 + c1 zC1+ d1 = 0 if Origine (0, 0, 0) is in plane P1, we have d1 = 0, hence 3 equations and 3 unknown: a1 xA1 + b1 yA1 + c1 zA1 = 0 a1 xB1 + b1 yB1 + c1 zB1 = 0 a1 xC1 + b1 yC1 + c1 zC1 = 0 if Origine (0, 0, 0) is not in plane P1, we have d1 ≠ 0, hence we can divide everything by d1, which is equivalent to d1 = 1 ; we have again 3 equations and 3 unknown: a1 xA1 + b1 yA1 + c1 zA1 + 1 = 0 a1 xB1 + b1 yB1 + c1 zB1 + 1 = 0 a1 xC1 + b1 yC1 + c1 zC1 + 1 = 0 There are tutorials to tell you how to solve this problem. And Apple's example: https://developer.apple.com/documentation/accelerate/solving_systems_of_linear_equations_with_lapack Once you have computed these coefficients, you have the equations of P1 and P2. The intersection is given by satisfying both: a1 x + b1 y + c1 z + d1 = 0 a2 x + b2 y + c2 z + d2 = 0 solution may be expressed as: x = a t + xA y = b t + yA z = c t + zA For example, if we have (P1) : 2 x + 3 y - z + 2 = 0 (P2) : x + y - 2z + 5 = 0 We get (P1) : z = 2 x + 3 y + 2 (P2) : x + y - 2 (2 x + 3 y + 2) + 5 = 0 <=> -3x - 5y + 1 = 0 By multiplying first by 3 second by 2: (P1) : 3z = 6x + 9 y + 6 (P2) : -6x - 10y + 2 = 0 summing the 2, we eliminate x (P1) : 6x + 9 y + 6 = 3z (P2) : -6x - 10y + 2 = 0 To get: (P1) : 6x + 9 y + 6 = 3z (P1) + (P2) : -y + 8 = 3z. Hence y = 8 - 3z Thus (P1) : z = 2 x + 3 y + 2 => z = 2 x + 3 (8 - 3z) + 2 <=> 10 z = 2 x + 26 <=> x = 5 z - 13 (P1) + (P2) : y = 8 - 3z So finally, the parametric equation of the intersect: x = 5 z - 13 y = - 3z + 8 z = z You can now transform this in code step by step (it is a bit tedious though). But may be your problem is a bit simpler, depending what your original triangles are. For instance if one in in the plane z = 0, that will simplify a lot. Note: this is to get the equation of intersection. If you just want to test if a point in on the intersection, it is much easier ; check if it satisfies (P1) and (P2)
Topic: Programming Languages SubTopic: Swift Tags:
Oct ’21
Reply to Triangle Intersection in 3D Space
Thanks, that is much more clear. Note that the 2 triangles could well not intersect. I wrote a basic algorithm to find those intersection points which are inside the triangle. Play with it by changing triangles (ABC and DEF) definition and tell me how it works. I added a lot of comments to help you understand what's going on. // Intersection of 2 triangles struct Point3D { var x: Double var y: Double var z: Double static func vector(from a: Point3D, to b: Point3D) -> Vector3D { Vector3D(x: b.x - a.x, y: b.y - a.y, z: b.z - a.z) } } struct Vector3D { // Same structure as Point3D var x: Double var y: Double var z: Double static func add(v1 u: Vector3D, v2 v: Vector3D) -> Vector3D { Vector3D(x: u.x + v.x, y: u.y + v.y, z: u.z + v.z) } static func prodScal(v1 u: Vector3D, v2 v: Vector3D) -> Double { u.x * v.x + u.y * v.y + u.z * v.z } static func prodVector(v1 u: Vector3D, v2 v: Vector3D) -> Vector3D { let wx = u.y*v.z - u.z*v.y let wy = u.z*v.x - u.x*v.z let wz = u.x*v.y - u.y*v.x return Vector3D(x: wx, y: wy, z: wz) } func multi(by d: Double) -> Vector3D { return Vector3D(x: d*self.x, y: d*self.y, z: d*self.z) } } /* Not needed so far struct Triangle3D { var a : Point3D var b : Point3D var c : Point3D } */ let pA = Point3D(x: 0, y: 5, z: 0) // For simplicity in this test, ABC is in the plane (x, y) ; but put anything you want let pB = Point3D(x: 5, y: 1, z: 0) let pC = Point3D(x: 3, y: 1, z: 0) // let tr1 = Triangle3D(a: pA, b: pB, c: pC) let pD = Point3D(x: 1, y: 2, z: 4) let pE = Point3D(x: 3, y: 1, z: 6) let pF = Point3D(x: 2, y: 4, z: -2) // let tr2 = Triangle3D(a: pD, b: pE, c: pF) // Compute "vertical" (perpendicular) vector AZ to ABC at point A // AZ is the vector product : AB ^ AC, given by formula: // u = AB ^ v = AC w = result // ux = [pB.x - pA.x] vx = [pC.x - pA.x] [uy vz - uz vy] // uy = [pB.y - pA.y] vy = [pC.y - pA.y] [uz vx - ux vz] // uz = [pB.z - pA.z] vz = [pC.z - pA.z] [ux vy - uy vx] let u = Point3D.vector(from: pA, to: pB) // vector AB let v = Point3D.vector(from: pA, to: pC) // vector AC let w = Vector3D.prodVector(v1: u, v2: v) // vector perpendicular to plane ABC // a point inside ABC triangle is defined as: // OM = α OA β OB γ OC (vectors) // with α + β + γ = 1 // and α > 0 β > 0 γ > 0 // a point inside DEF triangle is defined as: // OM = α' OD β' OE γ' OF (vectors) // with α' + β' + γ' = 1 // and α' ≥ 0 β' ≥ 0 γ' ≥ 0 // Let's use D as O point: // DM = α' DD + β' DE + γ' DF with α' + β' + γ' = 1 and α' ≥ 0 β' ≥ 0 γ' ≥ 0 ; α' no more used; β' ≥ 0 γ' ≥ 0 β' + γ' ≤ 1 // AM = AD + DM let ab = Point3D.vector(from: pA, to: pB) // To be used later let ac = Point3D.vector(from: pA, to: pC) // To be used later let ad = Point3D.vector(from: pA, to: pD) let de = Point3D.vector(from: pD, to: pE) let df = Point3D.vector(from: pD, to: pF) // With our above definitions and calling vector AM as t : t = ad + β de + γ df // We can now test all points in triangle DEF by looping on β and γ with a small increment // And see when they are in the plane of ABC : we test that scalar product AM * w is zero (in fact less than a small epsilon value // SIMD should be used here, but I do it rapidly // For each point M which is in ABC plan (AM * w is zero), we need to check if it is inside the ABC triangle. // That will be the case if AM = k AB + l AC with k ≥ 0, l ≥ 0 and k + l ≤ 1 // We have thus to resolve 3 equations with 2 unknown // Noting AM is vector (t.x, t.y, t.z) // (1) t.x = k ab.x + l ac.x // (2) t.y = k ab.y + l ac.y // (3) t.z = k ab.z + l ac.z // However, because M is in ABC plane, those equations are redundant : if 2 are satisfied, the third will be. // So we just have to solve by finding k and l : // (1) t.x = k ab.x + l ac.x // (2) t.y = k ab.y + l ac.y // Which is simple: (delta = ab.x ac.y - ac.x ab.y is called the determinant) // k = (t.x ac.y - t.y ac.x) / (ab.x ac.y - ac.x ab.y) // l = (t.y ab.x - t.x ab.y) / (ab.x ac.y - ac.x ab.y) // We shall test whether k and l are in correct bounds let epsilon = 0.001 for beta in stride(from: 0.0, through: 1.0, by: 0.01) { for gamma in stride(from: 0.0, through: 1.0 - beta, by: 0.01) { let t1 = Vector3D.add(v1: de.multi(by: beta), v2: df.multi(by: gamma)) let t = Vector3D.add(v1: ad, v2: t1) // AM: t = ad + β de + γ df ; for a point M inside DEF let prod = Vector3D.prodScal(v1: t, v2: w) if abs(prod) < epsilon { // M is in plane ABC // Is it inside triangle ABC ? let delta = ab.x * ac.y - ac.x * ab.y if delta == 0 { print("Determinant is zero ; use equations (2) and (3) instead") continue // skip this point, to not crash } let k = (t.x * ac.y - t.y * ac.x) / delta let l = (t.y * ab.x - t.x * ab.y) / delta print("k", k, "l", l) if k >= 0 && l >= 0 && k + l <= 1 { print("Point is inside ABC") } } } } Now that you can compute the intersection points, you can draw the line (between points defined by 2 extreme sets of valid values for k and l. Or do anything else you need. Good continuation.
Topic: Programming Languages SubTopic: Swift Tags:
Oct ’21
Reply to swift compiler error
You don't show enough so that we can tell exactly: what does getCovidData(for: scope) return ? how is result defined (its struct)? how is dayData defined in the class (self) ? But likely, self?.dayData is an array of DayData and dayData is DayData Hence the error. You should have something like self?.dayData.append(dayData) Note: naming var with very meaningful term is very important to avoid coding errors. Hence, naming dayData something that is array in the same way that a single element is very confusing. You'd better change for instance as: var dayDatas : [DayData] or var dayDataSeries : [DayData] or var dayDataArray : [DayData] Siomething to remind you it is an array. With this, you would immediately see that this is wrong: self?.dayDataSeries = dayData
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Oct ’21
Reply to Help! with String Interpolation
When you paste code, you'd better use Paste and Match Style, to avoid all extra blank lines. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "calCell", for: indexPath) as! CalendarCell cell.dayOfMonth.text = totalSquares[indexPath.item] //show the weekend days in different color switch indexPath.item { case 0,6,7,13,14,20,21,27,28,34,35,41: if Int(cell.dayOfMonth.text = "\(indexPath.item)" > 0 { cell.dayOfMonth.textColor = UIColor.lightGray } default: break } return cell } Where do you get the error ? On second line ? If so, I suspect totalSquares is an array of Int, not array of Strings. Then you should write: cell.dayOfMonth.text = String(totalSquares[indexPath.item])   There is probably another error. What do you want to test here: if Int(cell.dayOfMonth.text = "\(indexPath.item)" > 0 { Do you mean: if Int(cell.dayOfMonth.text) == "\(indexPath.item)" { But then what is the > 0 test ? Do you mean if Int(cell.dayOfMonth.text) == "\(indexPath.item)" && indexPath.item > 0 { But then just take 0 out of the case statement ; 0 will be handled in default. Please clarify
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Oct ’21
Reply to Help! with String Interpolation
The error "Cannot convert value of type 'String' to expected argument type 'Int'" means that a String "\(indexPath.item)" should be converted to an Int to be compared to an Int (0). Which cannot be done. But don't you get a lot more errors? As: I am trying to get the string interpolation to equal the cells position. That's not very clear. In particular, what the test > 0 is for in your code. But as it is a test, I understand you want to test if the month equals the cell position ? If so, you should test: if Int(cell.dayOfMonth.text) == indexPath.item {
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Oct ’21
Reply to Triangle Intersection in 3D Space
Could you explain more ? The 2 triangles are in an horizontal plane or not ? If it is 3D objects, intersection is probably a plan, not a line. What do you mean by their "vertecies" ? A small drawing, even a hand sketch would really help.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Oct ’21
Reply to RunLoop behaves differently between iOS15 and iOS14
What I read is not to run from the main thread, but: The RunLoop class is generally not thread-safe, and you must call its methods only within the context of the current thread. Don’t call the methods of a RunLoop object running in a different thread, which might cause unexpected results.
Topic: App & System Services SubTopic: Core OS Tags:
Replies
Boosts
Views
Activity
Oct ’21
Reply to Triangle Intersection in 3D Space
Some suggestion. Your triangles T1 (A1-B1-C1) and T2 (A2-B2-C2) are in Planes (P1 and P2). In 3D, those planes are defined by equations: (P1) : a1 x + b1 y + c1 z + d1 = 0 (P2) : a2 x + b2 y + c2 z + d2 = 0 You find a, b, c, d by resolving the system with 3 unknown, considering that each vertices of a triangle belongs to the plan. For instance A1 is in P1: a1 xA1 + b1 yA1 + c1 zA1+ d1 = 0 You will have to consider 2 cases: d = 0 (the origin is in the plan : you completely resolve the system of 3 equations d ≠ 0 Origin is out of plan : divide consider d = 1 to resolve Then the intersection of T1 and T2 is in fact intersection of P1 and P2, which is usually a line (may be empty if parallel plans or plan if plans are the same) The intersection is given by: a1 x + b1 y + c1 z + d1 = 0 a2 x + b2 y + c2 z + d2 = 0 solution may be expressed as: x = a t + xA y = b t + yA z = c t + zA Hope that helps.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Oct ’21
Reply to Connect Label to View Controller
I want to add a label to the view controller Do you mean adding an IBOutlet for the label that you have create in the storyboard ? with a label it won't work. What happens exactly ? Likely solution: Option-drag (alt) will duplicate the object (button, label) in the storyboard. That's not correct here. You have to control-drag from the Label (in the storyboard) to the code, inside the class. You will get a popup window asking to give a name to the IBOutlet. Note: Check that you have given the right class to the ViewController in the Storyboard: it must be the exact same name as the class in which you want to create the IBOutlet.
Replies
Boosts
Views
Activity
Oct ’21
Reply to iOS 15 VectorKit Crash
Could you show the code that crashes ?
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Oct ’21
Reply to How to set TableColumn in Table using ForEach
What do you get ? What did you expect ? The syntax includes a value: struct Person: Identifiable { let givenName: String let familyName: String let id = UUID() } @State private var people = [ Person(givenName: "Juan", familyName: "Chavez"), Person(givenName: "Mei", familyName: "Chen"), Person(givenName: "Tom", familyName: "Clark"), Person(givenName: "Gita", familyName: "Kumar"), ] TableColumn("Given Name", value: \Person.givenName) { person in Text(person.givenName) } So you should likely have this:             TableColumn(item.name, value : \.XXXXX.value) { data in Text(data.value)             } where XXXX is the struct containing value.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Oct ’21
Reply to Triangle Intersection in 3D Space
First, you didn't answer my question on what you are looking for exactly ! Back to your new question: But how do I solve for those 4 vars.(a,b,c,d) with only one equation You don't have one equation, you have 3. Triangle T1 (with vertices A1, B1, C1) in plane P1: A1 is (xA1, yA1, zA1) B1 is (xB1, yB1, zB1) C1 is (xC1, yC1, zC1) A1, B1, C1 are in plane P1; hence it satisfies 3 equations: a1 xA1 + b1 yA1 + c1 zA1+ d1 = 0 a1 xB1 + b1 yB1 + c1 zB1+ d1 = 0 a1 xC1 + b1 yC1 + c1 zC1+ d1 = 0 if Origine (0, 0, 0) is in plane P1, we have d1 = 0, hence 3 equations and 3 unknown: a1 xA1 + b1 yA1 + c1 zA1 = 0 a1 xB1 + b1 yB1 + c1 zB1 = 0 a1 xC1 + b1 yC1 + c1 zC1 = 0 if Origine (0, 0, 0) is not in plane P1, we have d1 ≠ 0, hence we can divide everything by d1, which is equivalent to d1 = 1 ; we have again 3 equations and 3 unknown: a1 xA1 + b1 yA1 + c1 zA1 + 1 = 0 a1 xB1 + b1 yB1 + c1 zB1 + 1 = 0 a1 xC1 + b1 yC1 + c1 zC1 + 1 = 0 There are tutorials to tell you how to solve this problem. And Apple's example: https://developer.apple.com/documentation/accelerate/solving_systems_of_linear_equations_with_lapack Once you have computed these coefficients, you have the equations of P1 and P2. The intersection is given by satisfying both: a1 x + b1 y + c1 z + d1 = 0 a2 x + b2 y + c2 z + d2 = 0 solution may be expressed as: x = a t + xA y = b t + yA z = c t + zA For example, if we have (P1) : 2 x + 3 y - z + 2 = 0 (P2) : x + y - 2z + 5 = 0 We get (P1) : z = 2 x + 3 y + 2 (P2) : x + y - 2 (2 x + 3 y + 2) + 5 = 0 <=> -3x - 5y + 1 = 0 By multiplying first by 3 second by 2: (P1) : 3z = 6x + 9 y + 6 (P2) : -6x - 10y + 2 = 0 summing the 2, we eliminate x (P1) : 6x + 9 y + 6 = 3z (P2) : -6x - 10y + 2 = 0 To get: (P1) : 6x + 9 y + 6 = 3z (P1) + (P2) : -y + 8 = 3z. Hence y = 8 - 3z Thus (P1) : z = 2 x + 3 y + 2 => z = 2 x + 3 (8 - 3z) + 2 <=> 10 z = 2 x + 26 <=> x = 5 z - 13 (P1) + (P2) : y = 8 - 3z So finally, the parametric equation of the intersect: x = 5 z - 13 y = - 3z + 8 z = z You can now transform this in code step by step (it is a bit tedious though). But may be your problem is a bit simpler, depending what your original triangles are. For instance if one in in the plane z = 0, that will simplify a lot. Note: this is to get the equation of intersection. If you just want to test if a point in on the intersection, it is much easier ; check if it satisfies (P1) and (P2)
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Oct ’21
Reply to Triangle Intersection in 3D Space
Please explain.  If I am not wrong two triangles intersect at a line. Are the 2 triangles in the same plane (you speak of 3D, so I guess they may be on different planes). Could you post a simple drawing to illustrate what you are looking for.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Oct ’21
Reply to Triangle Intersection in 3D Space
Thanks, that is much more clear. Note that the 2 triangles could well not intersect. I wrote a basic algorithm to find those intersection points which are inside the triangle. Play with it by changing triangles (ABC and DEF) definition and tell me how it works. I added a lot of comments to help you understand what's going on. // Intersection of 2 triangles struct Point3D { var x: Double var y: Double var z: Double static func vector(from a: Point3D, to b: Point3D) -> Vector3D { Vector3D(x: b.x - a.x, y: b.y - a.y, z: b.z - a.z) } } struct Vector3D { // Same structure as Point3D var x: Double var y: Double var z: Double static func add(v1 u: Vector3D, v2 v: Vector3D) -> Vector3D { Vector3D(x: u.x + v.x, y: u.y + v.y, z: u.z + v.z) } static func prodScal(v1 u: Vector3D, v2 v: Vector3D) -> Double { u.x * v.x + u.y * v.y + u.z * v.z } static func prodVector(v1 u: Vector3D, v2 v: Vector3D) -> Vector3D { let wx = u.y*v.z - u.z*v.y let wy = u.z*v.x - u.x*v.z let wz = u.x*v.y - u.y*v.x return Vector3D(x: wx, y: wy, z: wz) } func multi(by d: Double) -> Vector3D { return Vector3D(x: d*self.x, y: d*self.y, z: d*self.z) } } /* Not needed so far struct Triangle3D { var a : Point3D var b : Point3D var c : Point3D } */ let pA = Point3D(x: 0, y: 5, z: 0) // For simplicity in this test, ABC is in the plane (x, y) ; but put anything you want let pB = Point3D(x: 5, y: 1, z: 0) let pC = Point3D(x: 3, y: 1, z: 0) // let tr1 = Triangle3D(a: pA, b: pB, c: pC) let pD = Point3D(x: 1, y: 2, z: 4) let pE = Point3D(x: 3, y: 1, z: 6) let pF = Point3D(x: 2, y: 4, z: -2) // let tr2 = Triangle3D(a: pD, b: pE, c: pF) // Compute "vertical" (perpendicular) vector AZ to ABC at point A // AZ is the vector product : AB ^ AC, given by formula: // u = AB ^ v = AC w = result // ux = [pB.x - pA.x] vx = [pC.x - pA.x] [uy vz - uz vy] // uy = [pB.y - pA.y] vy = [pC.y - pA.y] [uz vx - ux vz] // uz = [pB.z - pA.z] vz = [pC.z - pA.z] [ux vy - uy vx] let u = Point3D.vector(from: pA, to: pB) // vector AB let v = Point3D.vector(from: pA, to: pC) // vector AC let w = Vector3D.prodVector(v1: u, v2: v) // vector perpendicular to plane ABC // a point inside ABC triangle is defined as: // OM = α OA β OB γ OC (vectors) // with α + β + γ = 1 // and α > 0 β > 0 γ > 0 // a point inside DEF triangle is defined as: // OM = α' OD β' OE γ' OF (vectors) // with α' + β' + γ' = 1 // and α' ≥ 0 β' ≥ 0 γ' ≥ 0 // Let's use D as O point: // DM = α' DD + β' DE + γ' DF with α' + β' + γ' = 1 and α' ≥ 0 β' ≥ 0 γ' ≥ 0 ; α' no more used; β' ≥ 0 γ' ≥ 0 β' + γ' ≤ 1 // AM = AD + DM let ab = Point3D.vector(from: pA, to: pB) // To be used later let ac = Point3D.vector(from: pA, to: pC) // To be used later let ad = Point3D.vector(from: pA, to: pD) let de = Point3D.vector(from: pD, to: pE) let df = Point3D.vector(from: pD, to: pF) // With our above definitions and calling vector AM as t : t = ad + β de + γ df // We can now test all points in triangle DEF by looping on β and γ with a small increment // And see when they are in the plane of ABC : we test that scalar product AM * w is zero (in fact less than a small epsilon value // SIMD should be used here, but I do it rapidly // For each point M which is in ABC plan (AM * w is zero), we need to check if it is inside the ABC triangle. // That will be the case if AM = k AB + l AC with k ≥ 0, l ≥ 0 and k + l ≤ 1 // We have thus to resolve 3 equations with 2 unknown // Noting AM is vector (t.x, t.y, t.z) // (1) t.x = k ab.x + l ac.x // (2) t.y = k ab.y + l ac.y // (3) t.z = k ab.z + l ac.z // However, because M is in ABC plane, those equations are redundant : if 2 are satisfied, the third will be. // So we just have to solve by finding k and l : // (1) t.x = k ab.x + l ac.x // (2) t.y = k ab.y + l ac.y // Which is simple: (delta = ab.x ac.y - ac.x ab.y is called the determinant) // k = (t.x ac.y - t.y ac.x) / (ab.x ac.y - ac.x ab.y) // l = (t.y ab.x - t.x ab.y) / (ab.x ac.y - ac.x ab.y) // We shall test whether k and l are in correct bounds let epsilon = 0.001 for beta in stride(from: 0.0, through: 1.0, by: 0.01) { for gamma in stride(from: 0.0, through: 1.0 - beta, by: 0.01) { let t1 = Vector3D.add(v1: de.multi(by: beta), v2: df.multi(by: gamma)) let t = Vector3D.add(v1: ad, v2: t1) // AM: t = ad + β de + γ df ; for a point M inside DEF let prod = Vector3D.prodScal(v1: t, v2: w) if abs(prod) < epsilon { // M is in plane ABC // Is it inside triangle ABC ? let delta = ab.x * ac.y - ac.x * ab.y if delta == 0 { print("Determinant is zero ; use equations (2) and (3) instead") continue // skip this point, to not crash } let k = (t.x * ac.y - t.y * ac.x) / delta let l = (t.y * ab.x - t.x * ab.y) / delta print("k", k, "l", l) if k >= 0 && l >= 0 && k + l <= 1 { print("Point is inside ABC") } } } } Now that you can compute the intersection points, you can draw the line (between points defined by 2 extreme sets of valid values for k and l. Or do anything else you need. Good continuation.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Oct ’21
Reply to For development, is the "MacBook Pro M1 8core 32GB" or "MacBook Pro M1 10core 16GB" good?
I would not hesitate to choose "MacBook Pro M1 8core 32GB". Speed is not that critical for Xcode ; and 8 or 10 cores will not much such a large difference. But available memory is critical (more and more). And don't be shy on storage: 2 GB is now the (minimum ?) target.
Topic: App & System Services SubTopic: Hardware Tags:
Replies
Boosts
Views
Activity
Oct ’21
Reply to Can I change the color of the active picker row which is greyish by default?
This should work:         picker.subviews[1].backgroundColor = UIColor.red.withAlphaComponent(0.2) Credit to the forum ! https://developer.apple.com/forums/thread/659184 This can also help understand picker structure: https://stackoverflow.com/questions/58855916/how-to-make-uipickerview-viewforrow-highlighted-in-the-center-the-reset-row-gr
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
Nov ’21
Reply to How to set TableColumn in Table using ForEach
So you should have something like:           ForEach(tableColumns) { item in             TableColumn(item.name, value : \.XXXXX.value) { data in Text(data.value)             }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Nov ’21