Skip to content
Snippets Groups Projects
Commit 5cb383bf authored by User01's avatar User01
Browse files

projet 2

parent 0249a0e0
No related merge requests found
......@@ -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
......
......@@ -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
#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);
......
......@@ -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
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment