IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Delphi Discussion :

Canvas détecter le croisement de deux droites


Sujet :

Delphi

  1. #1
    Membre habitué
    Homme Profil pro
    Owner
    Inscrit en
    Décembre 2004
    Messages
    466
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Owner
    Secteur : Santé

    Informations forums :
    Inscription : Décembre 2004
    Messages : 466
    Points : 137
    Points
    137
    Par défaut Canvas détecter le croisement de deux droites
    Bonjour,
    Je dois tracer une route aléatoire entre plusieurs points.
    Je cherche à détecter un croisement éventuel pour pouvoir rejeter cette route.
    J'utilise Canvas et des coordonnés x,y bien sûr associées à chaque point.
    Autre détail, la route revient à son point de départ.
    Merci pour vos idées...

    PhilLu

  2. #2
    Expert confirmé
    Avatar de BeanzMaster
    Homme Profil pro
    Amateur Passionné
    Inscrit en
    Septembre 2015
    Messages
    1 899
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Suisse

    Informations professionnelles :
    Activité : Amateur Passionné
    Secteur : Tourisme - Loisirs

    Informations forums :
    Inscription : Septembre 2015
    Messages : 1 899
    Points : 4 346
    Points
    4 346
    Billets dans le blog
    2
    Par défaut
    Bonjour un petit tour vers les maths et la géométrie devrait te permettre de trouver le point d'intersection de deux lignes

    https://en.wikipedia.org/wiki/Line–line_intersection
    http://mathworld.wolfram.com/Line-LineIntersection.html
    https://martin-thoma.com/how-to-chec...nts-intersect/

    A+

    [EDIT] Je viens de trouver ca en Delphi : http://www.delphipages.com/tip/test_...ion-10800.html et https://stackoverflow.com/questions/...code-in-pascal
    • "L'Homme devrait mettre autant d'ardeur à simplifier sa vie qu'il met à la compliquer" - Henri Bergson
    • "Bien des livres auraient été plus clairs s'ils n'avaient pas voulu être si clairs" - Emmanuel Kant
    • "La simplicité est la sophistication suprême" - Léonard De Vinci
    • "Ce qui est facile à comprendre ou à faire pour toi, ne l'est pas forcément pour l'autre." - Mon pèrei

    Mes projets sur Github - Blog - Site DVP

  3. #3
    Membre expert
    Avatar de Charly910
    Homme Profil pro
    Ingénieur TP
    Inscrit en
    Décembre 2006
    Messages
    2 339
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur TP
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 339
    Points : 3 107
    Points
    3 107
    Par défaut
    Bonjour,

    tu veux tester l'intersection de 2 segments ou de 2 droites ? si c'est 2 droites, c'est plus simple (tester la pente des 2 droites)

    Sinon ce code tiré de SwissDelphiCenter teste l'intersection de 2 segments :

    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
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    function Intersect(const x1, y1, x2, y2, x3, y3, x4, y4: Double): Boolean;
    var
      UpperX: Double;
      UpperY: Double;
      LowerX: Double;
      LowerY: Double;
      Ax: Double;
      Bx: Double;
      Cx: Double;
      Ay: Double;
      By: Double;
      Cy: Double;
      D: Double;
      F: Double;
      E: Double;
    begin
      Result := False;
     
      Ax := x2 - x1;
      Bx := x3 - x4;
     
      if Ax < 0.0 then
      begin
        LowerX := x2;
        UpperX := x1;
      end
      else
      begin
        UpperX := x2;
        LowerX := x1;
      end;
     
      if Bx > 0.0 then
      begin
        if (UpperX < x4) or (x3 < LowerX) then
          Exit;
      end
      else if (Upperx < x3) or (x4 < LowerX) then
        Exit;
     
      Ay := y2 - y1;
      By := y3 - y4;
     
      if Ay < 0.0 then
      begin
        LowerY := y2;
        UpperY := y1;
      end
      else
      begin
        UpperY := y2;
        LowerY := y1;
      end;
     
      if By > 0.0 then
      begin
        if (UpperY < y4) or (y3 < LowerY) then
          Exit;
      end
      else if (UpperY < y3) or (y4 < LowerY) then
        Exit;
     
      Cx := x1 - x3;
      Cy := y1 - y3;
      d  := (By * Cx) - (Bx * Cy);
      f  := (Ay * Bx) - (Ax * By);
     
      if f > 0.0 then
      begin
        if (d < 0.0) or (d > f) then
          Exit;
      end
      else if (d > 0.0) or (d < f) then
        Exit;
     
      e := (Ax * Cy) - (Ay * Cx);
     
      if f > 0.0 then
      begin
        if (e < 0.0) or (e > f) then
          Exit;
      end
      else if (e > 0.0) or (e < f) then
        Exit;
     
      Result := True;
     
    (*
     
      Simple method, yet not so accurate for certain situations and a little more
      inefficient (roughly 19.5%).
      Result := (
      ((Orientation(x1,y1, x2,y2, x3,y3) * Orientation(x1,y1, x2,y2, x4,y4)) <= 0) and
      ((Orientation(x3,y3, x4,y4, x1,y1) * Orientation(x3,y3, x4,y4, x2,y2)) <= 0)
      );
    *)
    end;
    (* End of SegmentIntersect *)

    Lien original

    A+
    Charly

  4. #4
    Membre émérite
    Avatar de ALWEBER
    Homme Profil pro
    Expert Delphi
    Inscrit en
    Mars 2006
    Messages
    1 483
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 68
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Expert Delphi

    Informations forums :
    Inscription : Mars 2006
    Messages : 1 483
    Points : 2 734
    Points
    2 734
    Billets dans le blog
    10
    Par défaut
    J'ai retrouvé cela dans mes archives
    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
     
      TDPoint = record
        X,Y : Double ;
      end ;
     
    function CalculeIntersec (P1,P2,P3,P4 : TDPoint; var X,Y : Double ) : boolean ;
    var
      a1,a2,b1,b2 : Double ;
    begin
      Result := false ;
      if ((P1.x-P2.x)=0) or ((P3.x-P4.x)=0) then
        exit ;
      a1 := (P1.y-P2.y)/(P1.x-P2.x) ;
      b1 := P1.y - (P1.x * a1)  ;
      a2 := (P3.y-P4.y)/(P3.x-P4.x) ;
      b2 := P3.y - (P3.x * a2) ;
      if a1=a2 then exit ;
      x := (b2-b1)/(a1-a2) ;
      y := a1*x + b1 ;
      Result := true ;
    end ;

  5. #5
    Membre expert
    Avatar de Charly910
    Homme Profil pro
    Ingénieur TP
    Inscrit en
    Décembre 2006
    Messages
    2 339
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur TP
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 339
    Points : 3 107
    Points
    3 107
    Par défaut
    Bonjour,

    @ALWEBER : cela donne l'intersection des droites portant les segments P1P2 et P3P4. Mais cela ne dit pas si l'intersection se fait sur les segments ou en dehors.

    A+
    Charly

  6. #6
    Membre expert
    Avatar de Charly910
    Homme Profil pro
    Ingénieur TP
    Inscrit en
    Décembre 2006
    Messages
    2 339
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur TP
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 339
    Points : 3 107
    Points
    3 107
    Par défaut
    Bonjour,

    j'ai un peu modifié le code d'ALWEBER, car il ne traitait pas le cas ou l'un des 2 segments était vertical. Le petit logiciel joint donne aussi la position de l'intersection par rapport aux 2 segments.

    Intersection.zip

    A+
    Charly

  7. #7
    Membre émérite
    Avatar de ALWEBER
    Homme Profil pro
    Expert Delphi
    Inscrit en
    Mars 2006
    Messages
    1 483
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 68
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Expert Delphi

    Informations forums :
    Inscription : Mars 2006
    Messages : 1 483
    Points : 2 734
    Points
    2 734
    Billets dans le blog
    10
    Par défaut
    Citation Envoyé par Charly910 Voir le message
    Bonjour,

    j'ai un peu modifié le code d'ALWEBER, car il ne traitait pas le cas ou l'un des 2 segments était vertical. Le petit logiciel joint donne aussi la position de l'intersection par rapport aux 2 segments.
    ...
    merci

  8. #8
    Membre expert
    Avatar de Charly910
    Homme Profil pro
    Ingénieur TP
    Inscrit en
    Décembre 2006
    Messages
    2 339
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur TP
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 339
    Points : 3 107
    Points
    3 107
    Par défaut
    Arghh ! Bug sur la détermination de la position du point d'intersection par rapport à P1P2 ou P3P3 (bug si un des segments est vertical !). Voici une version corrigée avec une autre méthode utilisant les produits scalaires :

    Intersection.zip

    A+
    Charly

  9. #9
    Expert confirmé
    Avatar de BeanzMaster
    Homme Profil pro
    Amateur Passionné
    Inscrit en
    Septembre 2015
    Messages
    1 899
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Suisse

    Informations professionnelles :
    Activité : Amateur Passionné
    Secteur : Tourisme - Loisirs

    Informations forums :
    Inscription : Septembre 2015
    Messages : 1 899
    Points : 4 346
    Points
    4 346
    Billets dans le blog
    2
    Par défaut
    Salut,

    vu que le sujet m'intéresse et que j'ai besoin de ce genre de chose dans un de mes projets.
    Avec Lazarus, j'ai réalisé une petite application de test sur la manipulation et l'application des formules mathématiques de lignes 2D dans un repère cartésien.
    Cette application n'est qu'une ébauche et c''est encore à peaufiner.

    La conversion vers Delphi ne devrait pas poser de problème majeur.

    Je mets le code à disposition ici, en attendant d' améliorer encore un peu la classe "TLine2D" et d'uploader cette exemple dans les "Téléchargements" de DVP

    L'exécutable en 64bits de l'application pour Windows est également présent afin de pouvoir tester.

    Nom : screenshot.jpg
Affichages : 195
Taille : 198,8 Ko

    Si vous avez des suggestions n'hésitez pas

    LineTool.zip

    Bon dimanche

    A+
    • "L'Homme devrait mettre autant d'ardeur à simplifier sa vie qu'il met à la compliquer" - Henri Bergson
    • "Bien des livres auraient été plus clairs s'ils n'avaient pas voulu être si clairs" - Emmanuel Kant
    • "La simplicité est la sophistication suprême" - Léonard De Vinci
    • "Ce qui est facile à comprendre ou à faire pour toi, ne l'est pas forcément pour l'autre." - Mon pèrei

    Mes projets sur Github - Blog - Site DVP

Discussions similaires

  1. Réponses: 9
    Dernier message: 12/07/2011, 18h25
  2. Lister avec croisement entre deux tables
    Par wehtam dans le forum Langage SQL
    Réponses: 3
    Dernier message: 11/01/2008, 00h10
  3. Croisement de deux tables avec SUM et sans doublon
    Par ar|equin dans le forum Requêtes et SQL.
    Réponses: 2
    Dernier message: 13/06/2007, 18h52
  4. Réponses: 5
    Dernier message: 20/05/2007, 01h06
  5. Angle entre deux droites quelconques par produit scalaire
    Par cyber_N dans le forum Mathématiques
    Réponses: 6
    Dernier message: 22/05/2005, 21h46

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo