This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
in204:tds:sujets:td1:part4 [2019/09/19 15:41] bmonsuez [Question n°1] |
in204:tds:sujets:td1:part4 [2022/11/18 10:49] (current) |
||
|---|---|---|---|
| Line 108: | Line 108: | ||
| return true; | return true; | ||
| } | } | ||
| - | if(m_start.getX() < m_end.getX()) | + | double ratio = |
| - | { | + | (double)(m_start.getY() - m_end.getY() |
| - | if(aPoint.getX() < m_start.getX() | + | / (double)(m_start.getX() - m_end.getX()); |
| - | || aPoint.getX() > m_end.getX()) | + | int pointY = |
| - | return false; | + | (int)floor(ratio * (aPoint.getX() - m_end.getX()) |
| - | } | + | + m_end.getY(); |
| - | else | + | return pointY == aPoint.getY(); |
| - | { | + | } |
| - | if(aPoint.getX() < m_end.getX() | + | bool intersectsWith( |
| - | || aPoint.getX() > m_start.getX()) | + | const Segment& anotherSegment, |
| - | return false; | + | Point& theIntersectingPoint) |
| - | } | + | { |
| - | return | + | Droite line(m_start, m_end); |
| - | atan2(m_start.getY() - m_end.getY(), | + | Droite anotherLine(anotherSegment.m_start, anotherSegment.m_end); |
| - | m_start.getX() - m_end.getX()) | + | |
| - | != atan2(aPoint.getY() - m_end.getY(), | + | double intersectionX, intersectionY; |
| - | aPoint.getX() - m_end.getY()); | + | if(!line.intersectsWith( |
| + | anotherLine, | ||
| + | intersectionX, intersectionY)) | ||
| + | return false; | ||
| + | |||
| + | theIntersectingPoint = | ||
| + | Point( | ||
| + | (int)round(intersectionX), | ||
| + | (int)round(intersectionY)); | ||
| + | return contains(theIntersectingPoint ) | ||
| + | || anotherSegment.contains(theIntersectingPoint); | ||
| } | } | ||
| }; | }; | ||
| Line 130: | Line 140: | ||
| </hidden> | </hidden> | ||
| - | <hidden Correction classe ''Line''> | + | <hidden Correction classe ''Droite''> |
| <code cpp> | <code cpp> | ||
| - | class Line | + | class Droite |
| { | { | ||
| private: | private: | ||
| Line 139: | Line 149: | ||
| public: | public: | ||
| - | Line(double anAngle, const Point& aPoint): | + | Droite(double anAngle, const Point& aPoint): |
| m_point(aPoint), | m_point(aPoint), | ||
| m_angle(anAngle) | m_angle(anAngle) | ||
| {} | {} | ||
| - | Line(const Point& theStart, const Point& theEnd): | + | Droite(const Point& theStart, const Point& theEnd): |
| m_point(theStart), | m_point(theStart), | ||
| m_angle(atan2( | m_angle(atan2( | ||
| Line 149: | Line 159: | ||
| (double)(theEnd.getX() - theStart.getX()))) | (double)(theEnd.getX() - theStart.getX()))) | ||
| {} | {} | ||
| - | Line(const Line& anotherLine): | + | Droite(const Line& anotherLine): |
| m_point(anotherLine.m_point), | m_point(anotherLine.m_point), | ||
| m_angle(anotherLine.m_angle) | m_angle(anotherLine.m_angle) | ||
| {} | {} | ||
| - | bool intersectWith(const Line& anotherLine) const | + | bool intersectsWith(const Line& anotherLine) |
| + | { | ||
| + | double intersectionX; | ||
| + | double intersectionY; | ||
| + | return intersectsWith(anotherLine, | ||
| + | intersectionX, intersectionY); | ||
| + | } | ||
| + | bool intersectsWith( | ||
| + | const Line& anotherLine, | ||
| + | double& theIntersectionX, | ||
| + | double& theIntersectionY) | ||
| { | { | ||
| if(m_point.getX() | if(m_point.getX() | ||
| Line 166: | Line 186: | ||
| anotherLine.m_point.getX() - m_point.getX()); | anotherLine.m_point.getX() - m_point.getX()); | ||
| if(angle != m_angle) | if(angle != m_angle) | ||
| - | return false; | + | return false; |
| } | } | ||
| return true; | return true; | ||
| Line 178: | Line 198: | ||
| Ajouter à toutes ces classes des méthodes d’affichage pour le déboguage que l’on appellera ''print''. | Ajouter à toutes ces classes des méthodes d’affichage pour le déboguage que l’on appellera ''print''. | ||
| + | |||
| + | <hidden Correction> | ||
| + | |||
| + | <code cpp> | ||
| + | class Point | ||
| + | { | ||
| + | // Ajouter la méthode suivante après les autres | ||
| + | // méthodes. | ||
| + | public: | ||
| + | void print() const | ||
| + | { | ||
| + | std::cout << "(" << m_x << ", " << m_y << ")"; | ||
| + | } | ||
| + | }; | ||
| + | |||
| + | class Segment | ||
| + | { | ||
| + | // Ajouter la méthode suivante après les autres | ||
| + | // méthodes. | ||
| + | public: | ||
| + | void print() const | ||
| + | { | ||
| + | std::cout << "["; | ||
| + | m_start.print(); | ||
| + | std::cout << "-"; | ||
| + | m_end.print(); | ||
| + | std::cout << "]"; | ||
| + | } | ||
| + | }; | ||
| + | |||
| + | class Droite | ||
| + | { | ||
| + | // Ajouter la méthode suivante après les autres | ||
| + | // méthodes. | ||
| + | public: | ||
| + | void print() const | ||
| + | { | ||
| + | std::cout << "<"; | ||
| + | m_point.print(); | ||
| + | std::cout << ", " m_angle << ">"; | ||
| + | } | ||
| + | }; | ||
| + | |||
| + | </code> | ||
| + | |||
| + | </hidden> | ||
| + | |||
| ==== Question n° 3 ==== | ==== Question n° 3 ==== | ||
| Line 184: | Line 251: | ||
| * Ajouter une méthode rotate90() effectuant une rotation de 90 degrés à une droite. | * Ajouter une méthode rotate90() effectuant une rotation de 90 degrés à une droite. | ||
| * Ajouter une méthode move(int x, int y) effectuant une translation à une droite. | * Ajouter une méthode move(int x, int y) effectuant une translation à une droite. | ||
| + | |||
| + | |||
| + | <hidden Correction> | ||
| + | |||
| + | Cette version correspond à des méthodes qui crée un nouvel objet qui est une copie de l'objet courant et ne modifie par l'objet courant. | ||
| + | |||
| + | <code cpp> | ||
| + | ... | ||
| + | |||
| + | const double pi = 3.14; | ||
| + | |||
| + | ... | ||
| + | |||
| + | class Droite | ||
| + | { | ||
| + | // Code précédent de la classe. | ||
| + | ... | ||
| + | | ||
| + | public: | ||
| + | Droite rotate(double anAngle) const | ||
| + | { | ||
| + | return Droite(m_Point, m_angle + anAngle); | ||
| + | } | ||
| + | Droite rotate90(double anAngle) const | ||
| + | { | ||
| + | return rotate(m_Point, m_angle + 3.14/4); | ||
| + | } | ||
| + | Droite move(int x, int y) const | ||
| + | { | ||
| + | return Droite( | ||
| + | Point(m_Point.getX() + x, | ||
| + | m_Point.GetY() + y), | ||
| + | m_angle); | ||
| + | } | ||
| + | ... | ||
| + | }; | ||
| + | </code> | ||
| + | |||
| + | Une autre possibilité consiste à modifier l'objet courant. C'est ce que fait le code suivant : | ||
| + | |||
| + | <code cpp> | ||
| + | ... | ||
| + | |||
| + | const double pi = 3.14; | ||
| + | |||
| + | ... | ||
| + | |||
| + | class Droite | ||
| + | { | ||
| + | ... // Code précédent de la classe. | ||
| + | | ||
| + | | ||
| + | public: | ||
| + | Droite& rotate(double anAngle) | ||
| + | { | ||
| + | m_angle += anAngle; | ||
| + | return *this; | ||
| + | } | ||
| + | Droite& rotate90(double anAngle) | ||
| + | { | ||
| + | return Turn(m_Point, m_angle + pi/4); | ||
| + | } | ||
| + | Droite& move(int x, int y) | ||
| + | { | ||
| + | mPoint = | ||
| + | Point(m_Point.getX() + x, | ||
| + | m_Point.GetY() + y); | ||
| + | return *this; | ||
| + | } | ||
| + | }; | ||
| + | </code> | ||
| + | |||
| + | Typiquement, nous retournons une référence à l'objet que nous venons de modifier. Ceci permet d'enchaîner les transformations comme suit : | ||
| + | |||
| + | <code cpp> | ||
| + | Droite droite(Point(0,0), pi/8); | ||
| + | | ||
| + | droite.Move(2,3).rotate(-pi/8); | ||
| + | // Nous effectuons une droite et puis nous effectuons | ||
| + | // une rotation. | ||
| + | </code> | ||
| + | |||
| + | </hidden> | ||
| ==== Question n° 4 ==== | ==== Question n° 4 ==== | ||
| Line 206: | Line 356: | ||
| } | } | ||
| </code> | </code> | ||
| + | |||
| + | <hidden correction> | ||
| + | |||
| + | Il suffit d'exécuter le code pour les classes réalisées et vérifier que les résultas sont conformes aux résultats attendus. | ||
| + | |||
| + | </hidden> | ||
| + | |||