From 5cb383bfb559f0c1259677657bca8f0cd46cc305 Mon Sep 17 00:00:00 2001
From: User01 <user01@uds-507826.ad.unistra.fr>
Date: Fri, 28 Mar 2025 17:44:09 +0100
Subject: [PATCH] projet 2

---
 square.msh   |  2 +-
 src/main.cpp | 14 +++++++++++++-
 src/msh.cpp  | 21 ++++++++++++++++++++-
 src/msh.h    |  3 +++
 4 files changed, 37 insertions(+), 3 deletions(-)

diff --git a/square.msh b/square.msh
index 3aab3e9..76ae375 100644
--- a/square.msh
+++ b/square.msh
@@ -12,7 +12,7 @@ $Nodes
 7 0.2999999999992664 0 0
 8 0.3999999999989749 0 0
 9 0.4999999999986943 0 0
-10 0.5999999999989468 0 0
+10 0.5999999999989468 0 0build
 11 0.69999999999921 0 0
 12 0.7999999999994734 0 0
 13 0.8999999999997368 0 0
diff --git a/src/main.cpp b/src/main.cpp
index 9e8851a..c142920 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -6,5 +6,17 @@ int main()
     Mesh m("../square.msh");
     //cout << m << endl;
     m.writeGnuplot("gnu.plt");
+    array<double, 2> p = {0.3, 0.4}; // Point à tester
+    int triIndex = m.findContainingTriangle(p);
+
+    if (triIndex >= 0) {
+        cout << "Le point est dans le triangle " << triIndex << endl;
+    } else {
+        cout << "Le point n'est dans aucun triangle" << endl;
+    }
+
     return 0;
-}
\ No newline at end of file
+}
+
+
+   
\ No newline at end of file
diff --git a/src/msh.cpp b/src/msh.cpp
index 5ddb7b1..80c4647 100644
--- a/src/msh.cpp
+++ b/src/msh.cpp
@@ -1,7 +1,26 @@
 #include "msh.h"
 #include <cassert>
+int Mesh::findContainingTriangle(const array<double, 2> &p) const {
+    for (int i = 0; i < triangle.size(); ++i) {
+        const auto &tri = triangle[i];
+        const auto &a = node[tri[0]];
+        const auto &b = node[tri[1]];
+        const auto &c = node[tri[2]];
 
-// classe maillage 2d avec liste de noeuds et liste de triangles
+        // Calcul des vecteurs
+        double detT = (b[0]-a[0])*(c[1]-a[1]) - (c[0]-a[0])*(b[1]-a[1]);
+        double alpha = ((b[0]-p[0])*(c[1]-p[1]) - (c[0]-p[0])*(b[1]-p[1])) / detT;
+        double beta  = ((c[0]-p[0])*(a[1]-p[1]) - (a[0]-p[0])*(c[1]-p[1])) / detT;
+        double gamma = 1.0 - alpha - beta;
+
+        if (alpha >= 0 && beta >= 0 && gamma >= 0) {
+            return i; // p est dans le triangle i
+        }
+    }
+    return -1; // Aucun triangle ne contient le point
+}
+
+//classe maillage 2d avec liste de noeuds et liste de triangles
 Mesh::Mesh(string gmsh2_filename)
 {
     ifstream file = ifstream(gmsh2_filename);
diff --git a/src/msh.h b/src/msh.h
index 4ee327c..7c0a820 100644
--- a/src/msh.h
+++ b/src/msh.h
@@ -19,6 +19,9 @@ public:
     friend ostream &operator<<(ostream &os, const Mesh &m);
     // affichage dans un fichier lisible par gnuplot
     void writeGnuplot(const string &filename) const;
+    int findContainingTriangle(const array<double, 2> &p) const;
+
 };
 
+
 #endif
-- 
GitLab