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

VB.NET Discussion :

Rectangle à 45 degré


Sujet :

VB.NET

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éprouvé
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2018
    Messages
    323
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2018
    Messages : 323
    Par défaut Rectangle à 45 degré
    Bonjour, je voudrais savoir si c'est possible faire un rectangle à 45 degré.

    Nom : Capture d’écran 2021-11-21 160609.png
Affichages : 173
Taille : 607 octets

    mon code avec polygons :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    Dim g As Graphics = e.Graphics
            Dim Angle As Integer = 36
            Dim x As Integer = e.ClipRectangle.X
            Dim y As Integer = e.ClipRectangle.Y
            Dim width As Integer = Me.Width - Angle
            Dim height As Integer = Me.Height - 1
            Dim points As New List(Of Point) From {New Point(x, y), New Point(x, y + height), New Point(x + width, y + height), New Point(x + Angle + width, y)}
            g.FillPolygon(New SolidBrush(Color.FromArgb(230, 230, 230)), points.ToArray)
            g.DrawPolygon(Pens.Black, points.ToArray)
    avez-vous une autre solution avec un rectangle à 45 degré?

  2. #2
    Membre éclairé Avatar de Sam Placi
    Homme Profil pro
    Développeur occasionnel
    Inscrit en
    Octobre 2019
    Messages
    68
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Aude (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Développeur occasionnel

    Informations forums :
    Inscription : Octobre 2019
    Messages : 68
    Par défaut
    Bonjour,

    D'un point de vue strictement géométrique, si tu connais les coordonnées de chaque point, il te suffit de tracer la figure avec DrawLine au lieu de DrawPolygon. Tu peux en principe, tu peux tracer ainsi n'importe quelle figure, même un cercle !
    Dés lors que tu connais les coordonnées d'un angle droit (idéalement le point de départ du tracé), et les longueurs des côtés, la ligne oblique doit pourvoir être tracée sans connaître sa longueur.

    Sam

  3. #3
    Membre extrêmement actif
    Inscrit en
    Avril 2008
    Messages
    2 573
    Détails du profil
    Informations personnelles :
    Âge : 65

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 573
    Par défaut
    BONJOUR

    comme dit par placid, d'un point de vue geometrique tout est possible mais dans l'API .NET il n'existe pas de primitive toute faite.
    voici un code exemple qui utlise la trigonometrie elementaire (niveau college) et trace:
    - le cote HAUT EN Rouge
    - le cote droit en Bleu
    - le cote bas en Marron
    - le cote gauche en Magenta
    dans cet ordre.
    il utilise un offset (decalage) pour deduire les 8 points (verticaux et horizontaux) des cotes HAUT DROIT BAS GAUCHE 0à partir d'un misérable rectangle.
    Les cotés à 45 degrés sont des demi-diagonales déduites de l'offset

    exemple code (utilise gr.drawgraphics && gr.drawpath et sa fonction Widen qui trace un contour de chaque cote du rectangle à angle de 45 degre)
    c'est un rectangle "multicolored"


    code du usercontrol à dropper sur un form user:
    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
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
     
    Imports System.Drawing.Drawing2D
     
    Public Class UserControl1
        Private Sub UserControl1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
            btnDrawLines.Text = "drawlines"
            btnPathDrawLines.Text = "pathdrawlines"
        End Sub
        Private Sub btnDrawLines_Click(sender As System.Object, e As System.EventArgs) Handles btnDrawLines.Click
     
            Using gr As Graphics = Me.CreateGraphics()
                gr.Clear(Me.BackColor)
                DrawLines(gr)
            End Using
        End Sub
     
        Private Sub btnPathDrawLines_Click(sender As System.Object, e As System.EventArgs) Handles btnPathDrawLines.Click
            Using gr As Graphics = Me.CreateGraphics()
                gr.Clear(Me.BackColor)
                PathDrawLines(gr)
            End Using
        End Sub
     
     
        Private Sub DrawLines(gr As Graphics)
            Dim red As New Pen(Brushes.Red, 2.0)
            Dim blue As New Pen(Brushes.Blue, 3.0)
            Dim brown As New Pen(Brushes.Brown, 3.0)
            Dim magenta As New Pen(Brushes.Magenta, 3.0)
            Dim offset As Integer = 20
            Dim angle As Double = Math.PI / 4
            Dim rect As New Rectangle(100, 50, 100, 200)
            Dim points() As Point
            'cote haut
            Dim p0 As New Point(rect.Left, rect.Top)
            Dim p1 As Point = New Point(rect.Right, rect.Top)
            'cote droit
     
            Dim p2 As New Point(rect.Right + offset, rect.Top + offset)
            Dim p3 As New Point(rect.Right + offset, rect.Top + offset + rect.Height)
            'cote bas
            Dim p4 As New Point(rect.Right, rect.Bottom + 2 * offset)
            Dim p5 As New Point(rect.Left, rect.Bottom + 2 * offset)
     
     
            'cote gauche
            Dim p6 As New Point(rect.Left - offset, rect.Top + offset + rect.Height)
            Dim p7 As New Point(rect.Left - offset, rect.Top + offset)
     
     
     
            gr.DrawLine(red, p0, p1)
            points = GetDiagonal(p0, 3 * angle, offset)
            gr.DrawLines(red, points)
            points = GetDiagonal(p1, angle, offset)
            gr.DrawLines(red, points)
     
            gr.DrawLine(blue, p2, p3)
            points = GetDiagonal(p2, -3 * angle, offset)
            gr.DrawLines(blue, points)
            points = GetDiagonal(p3, 3 * angle, offset)
            gr.DrawLines(blue, points)
     
     
            gr.DrawLine(brown, p4, p5)
            points = GetDiagonal(p4, -angle, offset)
            gr.DrawLines(brown, points)
            points = GetDiagonal(p5, -3 * angle, offset)
            gr.DrawLines(brown, points)
     
            gr.DrawLine(magenta, p6, p7)
            points = GetDiagonal(p6, angle, offset)
            gr.DrawLines(magenta, points)
            points = GetDiagonal(p7, -angle, offset)
            gr.DrawLines(magenta, points)
        End Sub
        Private Sub PathDrawLines(gr As Graphics)
            Dim brRed As New SolidBrush(Color.Red)
            Dim brBlue As New SolidBrush(Color.BlueViolet)
            Dim brBrown As New SolidBrush(Color.Brown)
            Dim brMagenta As New SolidBrush(Color.Magenta)
            Dim widenPen As New Pen(Brushes.Aquamarine, 10.0)
            Dim widenBrush As New SolidBrush(Color.Turquoise)
     
     
            Dim offset As Integer = 20
            Dim angle As Double = Math.PI / 4
            Dim rect As New Rectangle(100, 50, 100, 200)
            Dim points As New List(Of Point)
            'cote haut
            Dim p0 As New Point(rect.Left, rect.Top)
            Dim p1 As Point = New Point(rect.Right, rect.Top)
            'cote droit
     
            Dim p2 As New Point(rect.Right + offset, rect.Top + offset)
            Dim p3 As New Point(rect.Right + offset, rect.Top + offset + rect.Height)
            'cote bas
            Dim p4 As New Point(rect.Right, rect.Bottom + 2 * offset)
            Dim p5 As New Point(rect.Left, rect.Bottom + 2 * offset)
     
     
            'cote gauche
            Dim p6 As New Point(rect.Left - offset, rect.Top + offset + rect.Height)
            Dim p7 As New Point(rect.Left - offset, rect.Top + offset)
     
     
            Dim pth As New GraphicsPath
     
            points.AddRange(GetDiagonal(p0, 3 * angle, offset))
            points.AddRange(New Point() {p0, p1})
            points.AddRange(GetDiagonal(p1, angle, offset))
            pth.AddLines(points.ToArray)
            pth.Widen(widenPen)
            gr.FillPath(brRed, pth)
            pth.Reset()
            points.Clear()
     
     
            points.AddRange(GetDiagonal(p2, -3 * angle, offset))
            points.AddRange(New Point() {p2, p3})
            points.AddRange(GetDiagonal(p3, 3 * angle, offset))
            pth.AddLines(points.ToArray)
            pth.Widen(widenPen)
            gr.FillPath(brBlue, pth)
            pth.Reset()
            points.Clear()
     
            points.AddRange(GetDiagonal(p4, -angle, offset))
            points.AddRange(New Point() {p4, p5})
            points.AddRange(GetDiagonal(p5, -3 * angle, offset))
            pth.AddLines(points.ToArray)
            pth.Widen(widenPen)
            gr.FillPath(brBrown, pth)
            pth.Reset()
            points.Clear()
     
            points.AddRange(GetDiagonal(p6, angle, offset))
            points.AddRange(New Point() {p6, p7})
            points.AddRange(GetDiagonal(p7, -angle, offset))
            pth.AddLines(points.ToArray)
            pth.Widen(widenPen)
            gr.FillPath(brMagenta, pth)
     
            pth.Dispose()
        End Sub
        'FONCTION  DIAGONALE 
        Private Function GetDiagonal(startPoint As Point, angle As Double, offset As Integer) As Point()
     
            Dim length As Double = offset * Math.Sqrt(2) / 2
     
     
            Dim x As Double = startPoint.X + (Math.Cos(angle) * length)
            Dim y As Double = startPoint.Y + (Math.Sin(angle) * length)
     
            Dim endPoint As New Point(Convert.ToInt32(x), Convert.ToInt32(y))
     
            Dim pts() As Point = New Point() {startPoint, endPoint}
            Return pts
        End Function
     
     
    End Class
    évidemment pour produire un code valide en géométrie ,rien ne vaut un bon croquis de la figure à dessiner.
    bon code...

  4. #4
    Membre éprouvé
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2018
    Messages
    323
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2018
    Messages : 323
    Par défaut
    Bonjour, merci d'avoir répondu a question.

    je vais travailler avec fillpolygons. j'ai juste un petit souci pour avec 2 couleur comme un progressbar avec les valeur 10,20,30 ..etc.

    mon code de couleur :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    Dim valueLength As Integer = CInt((width / Maximum - Minimum) * Value)
            If valueLength = 0 Then Return
     
    Dim colors As New List(Of Color) From {Color.Blue, Color.Red}
            Dim brush As New PathGradientBrush(points.ToArray)
            For i = 0 To colors.Count - 1
                brush.CenterColor = colors(i)
                brush.SurroundColors = New Color() {colors(i)}
                g.FillPolygon(brush, New Point() {New Point(x, y), New Point(x, y + height), New Point(x + valueLength / colors.Count, y + height), New Point(x + Angle + valueLength / colors.Count, y)})
                x += valueLength / colors.Count
            Next
    avec un valeur à 50 pourcent ça me donne ceci en image. Nornalement à 50 pourcent il devrait avoir le bleu et apres à 60 pourcent. le rouge et bleu.
    Nom : Capture d’écran 2021-11-24 171437.png
Affichages : 104
Taille : 1,0 Ko

    avez-vous une solution à mon problème comme un progressbar avec les 2 couleur?

  5. #5
    Membre éprouvé
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2018
    Messages
    323
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2018
    Messages : 323
    Par défaut
    j'ai trouver ce que je voulais faire mais c dommage que on peux pas faire un angle 45 degré avec FillRegions.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    Dim r As Region = New Region(Path)
            Dim rectF As RectangleF = r.GetBounds(e.Graphics)
            Dim lngth As Integer = CInt((rectF.Width / (Maximum - Minimum)) * Value)
            If lngth = 0 Then Return
            Dim intersectRect As RectangleF = New RectangleF(rectF.X + 1, rectF.Y + 1, lngth, rectF.Height)
            r.Intersect(intersectRect)
            Dim brush As New LinearGradientBrush(rectF, _Color1, _Color2, LinearGradientMode.Horizontal)
            Dim ColorBlend As New ColorBlend()
            ColorBlend.Colors = New Color() {_Color1, _Color1, _Color2, _Color2}
            ColorBlend.Positions = New Single() {0, 0.5, 0.5, 1}
            brush.InterpolationColors = ColorBlend
            e.Graphics.FillRegion(brush, r)
    avez-vous un autre solution pour pour avoir un angle 45 degré avec les couleurs avec FillRegions? si vous voulez le code complet demander-moi.

Discussions similaires

  1. [prg jeux ]Définir l'intersection de deux rectangles
    Par mat.M dans le forum Algorithmes et structures de données
    Réponses: 6
    Dernier message: 30/07/2003, 18h11
  2. [MFC]Ecrire du texte dans un rectangle
    Par zaz16 dans le forum MFC
    Réponses: 8
    Dernier message: 29/07/2003, 10h31
  3. Comment centrer un Texte dans un rectangle ...
    Par Djedjeridoo dans le forum Composants VCL
    Réponses: 3
    Dernier message: 16/06/2003, 21h56
  4. Dessiner un rectangle avec bords et texte ...
    Par Djedjeridoo dans le forum Composants VCL
    Réponses: 3
    Dernier message: 16/06/2003, 17h17
  5. Réponses: 9
    Dernier message: 11/03/2003, 12h22

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