Bonjour, demandez de l’aide: le programme doit calculer l’intersection de deux lignes droites dans un plan. Malheureusement, le résultat pour la valeur y est faux, la méthode graphique donne: Y = 2,8 , le programme y= 3,7; Mon application est-elle erronée avec *this? Merci.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 class Point { // Un mesenger public: float x, y, z; // Puisque cest juste un transporteur Point() { x = 0.0; y = 0.0; z = 0.0; } Point(float xi, float yi, float zi) : x(xi), y(yi), z(zi) {} Point(const Point& p) : x(p.x), y(p.y), z(p.z) {} Point& operator=(const Point rhs) { x = rhs.x; y = rhs.y; z = rhs.z; return *this; } friend ostream& operator<<(ostream& os, const Point& p) { return os << " x=" << p.x << " y=" << p.y << " z=" << p.z; } };
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 class Line : public Point { //?!? Point po1, po2; public: Line(Point poi1, Point poi2) : po1{ poi1 }, po2{ poi2 }{} float coin() { return atan((po2.y - po1.y) / (po2.x - po1.x)); } float x2_x1(Line li) const { return (li.po2.x - li.po1.x); } float y2_y1(Line li) const { return (li.po2.y - li.po1.y); } float x3y4_y3x4(Line Li1, Line li2) const { return (li2.po1.x * li2.po2.y - li2.po1.y * li2.po2.x); } float x1y2_y1x2(Line li1, Line li2) const{ return (li1.po1.x * li1.po2.y - li1.po1.y * li1.po2.x); } float dénominateur(Line li1, Line li2) const{ return (x2_x1(li1) * y4_y3(li2) - y2_y1(li1) * x4_x3(li2)); } Point& intersection( Line li2) { this->po1.x = (x2_x1(*this) * x3y4_y3x4(*this, li2) - x4_x3(li2) * x1y2_y1x2(*this, li2)) / dénominateur(*this, li2); this->po1.y = (y2_y1(*this) * x3y4_y3x4(*this, li2) - y4_y3(li2) * x1y2_y1x2(*this, li2)) / dénominateur(*this, li2); return this->po1; } float x4_x3(Line lin2) const { return (lin2.po2.x - lin2.po1.x); } float y4_y3(Line lin2) const{ return (lin2.po2.y - lin2.po1.y); } };
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 int main(){ Point pA(3.5, 4, 0); Point pB(-4, 2, 0); Point pC(2, -2, 0); Point pD(-2.0, 5, 0); Line gérades1(pA, pB); Line gérades2(pC, pD); cout << gérades1.intersection( gérades2); // intersection_d_deux_lignes_droites }
Partager