× Aktuálně z oboru

SHIELD Experience Upgrade 7 – méně hledání a více zábavy [ clanek/2018052902-shield-experience-upgrade-7-mene-hledani-a-vice-zabavy/ ]
Celá zprávička [ clanek/2018052902-shield-experience-upgrade-7-mene-hledani-a-vice-zabavy/ ]

Grafický editor – 6. díl

[ http://programujte.com/profil/27-jiri-chytil/ ]Google [ ?rel=author ]       [ http://programujte.com/profil/75-martin-zak/ ]Google [ :?rel=author ]       10. 3. 2006       11 295×

Dnes se podíváme na některé geometrické nástroje do našeho grafického editoru. Jako první se podíváme na přímku, ta je asi nejednodušší a velmi důležitá.

Přímku budeme vykreslovat pomocí metody line. Je to velmi jednoduché, ale budeme potřebovat tři procedury. Proč? No první je procedura, která reaguje na kliknutí. Druhá je procedura, která reaguje na pohyb myši a vykresluje onu úsečku v prozatímní poloze. A třetí nám ji ukotví. Kód může vypadat takto:


Dim xa1 As Single
Dim ya1 As Single
Dim barva As Long

Public Sub Line1(Img As PictureBox, Xa As Single, Ya As Single, Clr As Long)
    xa1 = Xa
    ya1 = Ya
End Sub

Public Sub Line2(Img As PictureBox, Xa As Single, Ya As Single, Width As Long, Button As Integer, Clr As Long, Shift As Integer)
    Img.DrawWidth = Width
    If Button = 1 Then
    Img.AutoRedraw = False
    Img.Refresh
    Img.Line (xa1, ya1)-(Xa, Ya), Clr
    Img.AutoRedraw = True
    End If
End Sub

Public Sub Line3(Img As PictureBox, Xa As Single, Ya As Single, Width As Long, Button As Integer, Clr As Long, Shift As Integer)
    Img.DrawWidth = Width
    If Button = 1 Then
    Img.AutoRedraw = True
    Img.Line (xa1, ya1)-(Xa, Ya), Clr
    Img.Refresh
    End If
End Sub

Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Line1 Picture1, X, Y
End Sub

Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Line2 Picture1, X, Y, 3, Button, barva
End Sub

Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Line3 Picture1, X, Y, 3, Button, barva
End Sub

Pokud si na formulář vložíte picture box a vložíte tento kód, můžete zkusit nakreslit přímku a mělo by to jít. Pokud ne, tak je to divné.

Důležitou věcí jsou i parametry. Z těchto parametrů:


Line3 Picture1, X, Y, 3, Button, barva

je Line3 volaná funkce, Picture1 je picture box, se kterým pracujeme, X a Y jsou souřadnice, na které budeme zapisovat, 3 je šířka čáry, Button je tlačítko na myši, které jsme použili, a barva určuje barvu čáry.

Uživatel je ale velmi náročný, proto se nespokojí jen s kdo ví čím. Je proto vhodné využít tlačítek Shift a Ctrl a zpříjemnit tak práci s aplikací. Prvně se podíváme na práci s tlačítkem Shift. To by uživateli mělo umožňovat dělat čáry buď vodorovné, svislé anebo čáry v úhlu 45&grad;. Toho se dosáhne takto:


Dim xa1 As Single
Dim ya1 As Single
Dim barva As Long

Public Sub Line1(Img As PictureBox, Xa As Single, Ya As Single, Clr As Long)
    xa1 = Xa
    ya1 = Ya
End Sub

Public Sub Line2(Img As PictureBox, Xa As Single, Ya As Single, Width As Long, Button As Integer, Clr As Long, Shift As Integer)
    Img.DrawWidth = Width
    If Button = 1 Then
    Img.AutoRedraw = False
    Img.Refresh
    If Shift = 1 Then
        If Abs(xa1 - Xa) < Abs(ya1 - Ya) - 600 Then
            Img.Line (xa1, ya1)-(xa1, Ya), Clr
        ElseIf Abs(xa1 - Xa) > Abs(ya1 - Ya) + 600 Then
            Img.Line (xa1, ya1)-(Xa, ya1), Clr
        Else
            If Xa < xa1 Then
                Xsig = -1
            Else
                Xsig = 1
            End If
            If Ya < ya1 Then
                Ysig = -1
            Else
                Ysig = 1
            End If
            chng = Abs(ya1 - Ya)
            Img.Line (xa1, ya1)-(xa1 + chng * Xsig, ya1 + chng * Ysig), Clr
        End If
    Else
        Img.Line (xa1, ya1)-(Xa, Ya), Clr
    End If
    Img.AutoRedraw = True
    End If
End Sub
Public Sub Line3(Img As PictureBox, Xa As Single, Ya As Single, Width As Long, Button As Integer, Clr As Long, Shift As Integer)
    Img.DrawWidth = Width
    If Button = 1 Then
    Img.AutoRedraw = True
    If Shift = 1 Then
        If Abs(xa1 - Xa) < Abs(ya1 - Ya) - 600 Then
            Img.Line (xa1, ya1)-(xa1, Ya), Clr
        ElseIf Abs(xa1 - Xa) > Abs(ya1 - Ya) + 600 Then
            Img.Line (xa1, ya1)-(Xa, ya1), Clr
        Else
            If Xa < xa1 Then
                Xsig = -1
            Else
                Xsig = 1
            End If
            If Ya < ya1 Then
                Ysig = -1
            Else
                Ysig = 1
            End If
            chng = Abs(ya1 - Ya)
            Img.Line (xa1, ya1)-(xa1 + chng * Xsig, ya1 + chng * Ysig), Clr
        End If
    Else
        Img.Line (xa1, ya1)-(Xa, Ya), Clr
    End If
    Img.Refresh
    End If
End Sub

Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Line1 Picture1, X, Y, barva
End Sub

Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Line2 Picture1, X, Y, 3, Button, barva, Shift
End Sub

Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Line3 Picture1, X, Y, 3, Button, barva, Shift
End Sub

Pozor, tento kód je psán pro práci v twipech.

Dále je pro uživatele příjemné, když může pracovat třeba v mřížce a pokud možno když si může nastavit její velikost. Tentokráte bude lepší pracovat v pixelech. Tento systém pracuje tak, že se přímka zachytává pouze na dovolených bodech. Jejich počet závisí na mřížce a na velikosti obrázku. Čím větší obrázek a čím menší rozteč bodů mřížky, tím jich bude víc. Pro vás je ale výhodné to, že nebudete muset zaokrouhlovat, ale bude vám stačit vhodně počítat.

Zde je zvolená velikost mřížky 20 pixelů, je důležité nastavit vlastnost ScaleMode na pixely.


Dim xa1 As Single
Dim ya1 As Single
Dim barva As Long

Public Sub Line1(Img As PictureBox, Xa As Single, Ya As Single, Clr As Long)
    Xa = Round(Xa / 20) * 20
    Ya = Round(Ya / 20) * 20
    xa1 = Xa
    ya1 = Ya
End Sub

Public Sub Line2(Img As PictureBox, Xa As Single, Ya As Single, Width As Long, Button As Integer, Clr As Long, Shift As Integer)
    Img.DrawWidth = Width
    If Button = 1 Then
    Img.AutoRedraw = False
    Img.Refresh
    If Shift = 1 Then
        Xa = Round(Xa / 20) * 20
        Ya = Round(Ya / 20) * 20
        Img.Line (xa1, ya1)-(Xa, Ya), Clr
    Else
        Img.Line (xa1, ya1)-(Xa, Ya), Clr
    End If
    Img.AutoRedraw = True
    End If
End Sub

Public Sub Line3(Img As PictureBox, Xa As Single, Ya As Single, Width As Long, Button As Integer, Clr As Long, Shift As Integer)
    Img.DrawWidth = Width
    If Button = 1 Then
    Img.AutoRedraw = True
    If Shift = 1 Then
        Xa = Round(Xa / 20) * 20
        Ya = Round(Ya / 20) * 20
        Img.Line (xa1, ya1)-(Xa, Ya), Clr
    Else
        Img.Line (xa1, ya1)-(Xa, Ya), Clr
    End If
    Img.Refresh
    End If
End Sub

Nějak nemůžu pochopit, proč tato vlastnost u mnoha moderních kreslicích nástrojů chybí, přestože se díky ní dá velmi rychle tvořit.

Modifikací je samozřejmě spousta, třeba dělení vzdálenosti pro zvýšení přesnosti tahu. To se zase udělá vydělením rozdílu bodu XYa1 a bodu XYa.

Takto:


Public Sub Line1(Img As PictureBox, Xa As Single, Ya As Single, Clr As Long)
    xa1 = Xa
    ya1 = Ya
End Sub

Public Sub Line2(Img As PictureBox, Xa As Single, Ya As Single, Width As Long, Button As Integer, Clr As Long, Shift As Integer)
    Img.DrawWidth = Width
    If Button = 1 Then
    Img.AutoRedraw = False
    Img.Refresh
    If Shift = 1 Then
        chngx = (xa1 - Xa) / 3
        chngy = (ya1 - Ya) / 3
        Img.Line (xa1, ya1)-(xa1 - chngx, ya1 - chngy), Clr
    Else
        Img.Line (xa1, ya1)-(Xa, Ya), Clr
    End If
    Img.AutoRedraw = True
    End If
End Sub
Public Sub Line3(Img As PictureBox, Xa As Single, Ya As Single, Width As Long, Button As Integer, Clr As Long, Shift As Integer)
    Img.DrawWidth = Width
    If Button = 1 Then
    Img.AutoRedraw = True
    If Shift = 1 Then
        chngx = (xa1 - Xa) / 3
        chngy = (ya1 - Ya) / 3
        Img.Line (xa1, ya1)-(xa1 - chngx, ya1 - chngy), Clr
    Else
        Img.Line (xa1, ya1)-(Xa, Ya), Clr
    End If
    Img.Refresh
    End If
End Sub

Další modifikace samozřejmě můžete vytvářet vy. Například jinou barvu čáry, když kreslíte pravým nebo levým tlačítkem. A jsou jich spousty. Je to jen na vaší představivosti. Více si povíme v příštím díle, ale to už o obdélníku.


Článek stažen z webu Programujte.com [ http://programujte.com/clanek/2006030503-graficky-editor-6-dil/ ].