Jump to content

Drawing Dotted Lines


YellowLab
 Share

Recommended Posts

I was looking to see if someone had done a script for drawing dotted lines without using GDI plus. I didn't find one, so I came up with my own:

; $hGraphic = the graphic control to recieve the line
; $xStart = the x starting point
; $yStart = the y starting point
; $xEnd = the x ending point
; $yEnd = the y ending point
; $nSolid = the length of the solid portion of the dash in pixels
; $nBlank = the length of the blank portion of the dash in pixels
; dependent on GUIConstantsEX.au3
Func _DottedLine($hGraphic,$xStart,$yStart,$xEnd,$yEnd,$nSolid,$nBlank)
    Local $fAngle=ATan(($yEnd-$yStart)/($xEnd-$xStart)), $Length
    Local $xCurrent=$xStart
    Local $yCurrent=$yStart
    Local $Length=0
    While $Length<=Sqrt(($xStart-$xEnd)^2+($yStart-$yEnd)^2)
        GUICtrlSetGraphic($hGraphic,$GUI_GR_MOVE,$xCurrent,$yCurrent)
        $xCurrent+=$nSolid*Cos($fAngle)
        $yCurrent+=$nSolid*Sin($fAngle)
        If Sqrt(($xStart-$xCurrent)^2+($yStart-$yCurrent)^2) >= Sqrt(($xStart-$xEnd)^2+($yStart-$yEnd)^2) Then
            $xCurrent=$xEnd
            $yCurrent=$yEnd
        EndIf
        GUICtrlSetGraphic($hGraphic,$GUI_GR_LINE,$xCurrent,$yCurrent)
        $xCurrent+=$nBlank*Cos($fAngle)
        $yCurrent+=$nBlank*Sin($fAngle)
        $Length=Sqrt(($xStart-$xCurrent)^2+($yStart-$yCurrent)^2)
    WEnd
    GUICtrlSetGraphic($hGraphic,$GUI_GR_REFRESH)
EndFuncoÝ÷ Øw«z+ªèìZ^²0x!£­Â䱫­¢+Ø¥¹±Õ±ÐíU%
½¹ÍѹÑÍà¹ÔÌÐì)=ÁÐ ÅÕ½Ðí5½ÕÍ
½½É5½ÅÕ½Ðì°È¤((ÀÌØí¡QÍÐôU%
ÉÑ ÅÕ½ÐíÍ¡1¥¹QÍÐÅÕ½Ðì°ÐÀÀ°ÐÀÀ°À°À¤(ÀÌØí¡1¥¹õU%
Ñɱ
ÉÑÉÁ¡¥ À°À°ÐÀÀ°ÐÀÀ¤)U%
ÑɱMÑMÑÑ ´Ä°ÀÌØíU%}%M  1¤(ÀÌØí¡1°õU%
Ñɱ
ÉÑ1° ÅÕ½ÐíM±ÐA½¥¹ÐÄÅÕ½Ðì°À°À°ÐÀÀ°ÐÀÀ¤)U%
ÑɱMÑ   ­
½±½È ÀÌØí¡1°°ÀÌØíU%} -
=1=I}QI9MAI9P¤)1½°ÀÌØíÉA½¥¹ÑÌ()U%MÑMÑÑ¡M]}M!=°ÀÌØí¡QÍФ()¼($ÀÌØí¹5ÍõU%Ñ5Í ¤(%%ÀÌØí¹5ÍôÀÌØí¡1°Q¡¸($$ÀÌØíÉQµÀõ5½ÕÍÑA½Ì ¤($%%%ÍÉÉä ÀÌØíÉA½¥¹Ñ̤Q¡¸($$$ÀÌØíÉA½¥¹ÑÍlÉtôÀÌØíÉQµÁlÁt($$$ÀÌØíÉA½¥¹ÑÍlÍtôÀÌØíÉQµÁlÅt($$%U%
Ñɱ±Ñ ÀÌØí¡1°¤($$%U%
ÑɱMÑÉÁ¡¥ ÀÌØí¡1¥¹°ÀÌØíU%}I}5=Y°ÀÌØíÉA½¥¹ÑÍlÉt°ÀÌØíÉA½¥¹ÑÍlÍt¤($$%U%
ÑɱMÑÉÁ¡¥ ÀÌØí¡1¥¹°ÀÌØíU%}I}=P°ÀÌØíÉA½¥¹ÑÍlÉt°ÀÌØíÉA½¥¹ÑÍlÍt¤($$%U%
ÑɱMÑÉÁ¡¥ ÀÌØí¡1¥¹°ÀÌØíU%}I}IIM ¤($$%á¥Ñ1½½À($%±Í($$%¥´ÀÌØíÉA½¥¹ÑÍlÑt($$$ÀÌØíÉA½¥¹ÑÍlÁtôÀÌØíÉQµÁlÁt($$$ÀÌØíÉA½¥¹ÑÍlÅtôÀÌØíÉQµÁlÅt($$%U%
ÑɱMÑÑ ÀÌØí¡1°°ÅÕ½ÐíM±ÐA½¥¹ÐÈÅÕ½Ðì¤($$%U%
ÑɱMÑÉÁ¡¥ ÀÌØí¡1¥¹°ÀÌØíU%}I}5=Y°ÀÌØíÉA½¥¹ÑÍlÁt°ÀÌØíÉA½¥¹ÑÍlÅt¤($$%U%
ÑɱMÑÉÁ¡¥ ÀÌØí¡1¥¹°ÀÌØíU%}I}=P°ÀÌØíÉA½¥¹ÑÍlÁt°ÀÌØíÉA½¥¹ÑÍlÅt¤($$%U%
ÑɱMÑÉÁ¡¥ ÀÌØí¡1¥¹°ÀÌØíU%}I}IIM ¤($$%¹%(%¹%(%%ÀÌØí¹5Íô´ÌÑ¡¸á¥Ð)U¹Ñ¥°ÄôÈ()}½ÑÑ1¥¹ ÀÌØí¡1¥¹°ÀÌØíÉA½¥¹ÑÍlÁt°ÀÌØíÉA½¥¹ÑÍlÅt°ÀÌØíÉA½¥¹ÑÍlÉt°ÀÌØíÉA½¥¹ÑÍlÍt°ÄÔ°Ô¤()¼($ÀÌØí¹5ÍõU%Ñ5Í ¤)U¹Ñ¥°ÀÌØí¹5Íô´Ì()Õ¹}½ÑÑ1¥¹ ÀÌØí¡ÉÁ¡¥°ÀÌØíáMÑÉаÀÌØíåMÑÉаÀÌØíá¹°ÀÌØíå¹°ÀÌØí¹M½±¥°ÀÌØí¹    ±¹¬¤(%1½°ÀÌØí¹±õQ¸  ÀÌØíå¹´ÀÌØíåMÑÉФ¼ ÀÌØíá¹´ÀÌØíáMÑÉФ¤°ÀÌØí1¹Ñ (%1½°ÀÌØíá
ÕÉɹÐôÀÌØíáMÑÉÐ(%1½°ÀÌØíå
ÕÉɹÐôÀÌØíåMÑÉÐ(%1½°ÀÌØí1¹Ñ ôÀ(%]¡¥±ÀÌØí1¹Ñ ±ÐìõMÅÉÐ  ÀÌØíáMÑÉдÀÌØíá¹¥xȬ ÀÌØíåMÑÉдÀÌØíå¹¥xȤ($%U%
ÑɱMÑÉÁ¡¥ ÀÌØí¡ÉÁ¡¥°ÀÌØíU%}I}5=Y°ÀÌØíá
ÕÉɹаÀÌØíå
ÕÉɹФ($$ÀÌØíá
ÕÉɹЬôÀÌØí¹M½±¥©
½Ì ÀÌØí¹±¤($$ÀÌØíå
ÕÉɹЬôÀÌØí¹M½±¥©M¥¸ ÀÌØí¹±¤($%%MÅÉÐ  ÀÌØíáMÑÉдÀÌØíá
ÕÉɹХxȬ ÀÌØíåMÑÉдÀÌØíå
ÕÉɹХxȤÐìôMÅÉÐ  ÀÌØíáMÑÉдÀÌØíá¹¥xȬ ÀÌØíåMÑÉдÀÌØíå¹¥xȤQ¡¸($$$ÀÌØíá
ÕÉɹÐôÀÌØíá¹($$$ÀÌØíå
ÕÉɹÐôÀÌØíå¹($%¹%($%U%
ÑɱMÑÉÁ¡¥ ÀÌØí¡ÉÁ¡¥°ÀÌØíU%}I}1%9°ÀÌØíá
ÕÉɹаÀÌØíå
ÕÉɹФ($$ÀÌØíá
ÕÉɹЬôÀÌØí¹    ±¹¬©
½Ì ÀÌØí¹±¤($$ÀÌØíå
ÕÉɹЬôÀÌØí¹    ±¹¬©M¥¸ ÀÌØí¹±¤($$ÀÌØí1¹Ñ õMÅÉÐ  ÀÌØíáMÑÉдÀÌØíá
ÕÉɹХxȬ ÀÌØíåMÑÉдÀÌØíå
ÕÉɹХxȤ(%]¹(%U%
ÑɱMÑÉÁ¡¥ ÀÌØí¡ÉÁ¡¥°ÀÌØíU%}I}IIM ¤)¹Õ¹

Bob

You can't see a rainbow without first experiencing the rain.

Link to comment
Share on other sites

An interesting thing about graphic controls is that the size is irrelevant. (I mention this because I noticed that you carefully sized your graphic to be the window client size.) If you set the size to 0 x 0 it will still draw a line wherever you tell it to and in any direction you like. The original x,y position is maintained as the origin for graphics but the size is ignored. And if you get the size after it has drawn a line using ControlGetPos it will report that the size is still 0 x 0. It's magic.

Nice function though :)

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Vrey nice!

I tried this approch to make your script draw mutiple lines, but it failed. What did I do wrong?

; Script Start - Add your code below here

#include <GUIConstantsEx.au3>
Opt("MouseCoordMode",2)

$hTest = GUICreate("Dashed Line Test",400,400,0,0)
$i = 1

While 1

$hLine=GUICtrlCreateGraphic(0,0,400,400)
GUICtrlSetState(-1,$GUI_DISABLE)
$hLabel=GUICtrlCreateLabel("Select Point 1",0,0,400,400)
GUICtrlSetBkColor($hLabel,$GUI_BKCOLOR_TRANSPARENT)
Local $arPoints

GUISetState(@SW_SHOW,$hTest)

Do
    $nMsg=GUIGetMsg()
    If $nMsg=$hLabel Then
        $arTemp=MouseGetPos()
        If $i = 2 Then
            $arPoints[2]=$arTemp[0]
            $arPoints[3]=$arTemp[1]
            GUICtrlDelete($hLabel)
            GUICtrlSetGraphic($hLine,$GUI_GR_MOVE,$arPoints[2],$arPoints[3])
            GUICtrlSetGraphic($hLine,$GUI_GR_DOT,$arPoints[2],$arPoints[3])
            GUICtrlSetGraphic($hLine,$GUI_GR_REFRESH)
            $i = 1
            ExitLoop
        Else
            Dim $arPoints[4]
            $arPoints[0]=$arTemp[0]
            $arPoints[1]=$arTemp[1]
            GUICtrlSetData($hLabel,"Select Point 2")
            GUICtrlSetGraphic($hLine,$GUI_GR_MOVE,$arPoints[0],$arPoints[1])
            GUICtrlSetGraphic($hLine,$GUI_GR_DOT,$arPoints[0],$arPoints[1])
            GUICtrlSetGraphic($hLine,$GUI_GR_REFRESH)
            $i = 2
        EndIf
    EndIf
    If $nMsg=-3 then Exit
Until 1=2

_DottedLine($hLine,$arPoints[0],$arPoints[1],$arPoints[2],$arPoints[3],5,5)

WEnd

Do
    $nMsg=GUIGetMsg()
Until $nMsg=-3

Func _DottedLine($hGraphic,$xStart,$yStart,$xEnd,$yEnd,$nSolid,$nBlank)
    Local $fAngle=ATan(($yEnd-$yStart)/($xEnd-$xStart)), $Length
    Local $xCurrent=$xStart
    Local $yCurrent=$yStart
    Local $Length=0
    While $Length<=Sqrt(($xStart-$xEnd)^2+($yStart-$yEnd)^2)
        GUICtrlSetGraphic($hGraphic,$GUI_GR_MOVE,$xCurrent,$yCurrent)
        $xCurrent+=$nSolid*Cos($fAngle)
        $yCurrent+=$nSolid*Sin($fAngle)
        If Sqrt(($xStart-$xCurrent)^2+($yStart-$yCurrent)^2) >= Sqrt(($xStart-$xEnd)^2+($yStart-$yEnd)^2) Then
            $xCurrent=$xEnd
            $yCurrent=$yEnd
        EndIf
        GUICtrlSetGraphic($hGraphic,$GUI_GR_LINE,$xCurrent,$yCurrent)
        $xCurrent+=$nBlank*Cos($fAngle)
        $yCurrent+=$nBlank*Sin($fAngle)
        $Length=Sqrt(($xStart-$xCurrent)^2+($yStart-$yCurrent)^2)
    WEnd
    GUICtrlSetGraphic($hGraphic,$GUI_GR_REFRESH)
EndFunc

~corgano

*EDIT*

works fine if point 1 is LEFT of point 2.....

Edited by corgano

0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Link to comment
Share on other sites

Okay, there was an issue with the function if Pt1 was right of Pt2 - this is fixed here:

; $hGraphic = the graphic control to recieve the line
; $xStart = the x starting point
; $yStart = the y starting point
; $xEnd = the x ending point
; $yEnd = the y ending point
; $nSolid = the length of the solid portion of the dash in pixels
; $nBlank = the length of the blank portion of the dash in pixels
; dependent on GUIConstantsEX.au3

Func _DottedLine($hGraphic,$xStart,$yStart,$xEnd,$yEnd,$nSolid,$nBlank)
    Local $fAngle=ATan(($yEnd-$yStart)/($xEnd-$xStart)), $Length
    If $xStart>$xEnd then $fAngle+=3.141592654
    Local $xCurrent=$xStart
    Local $yCurrent=$yStart
    Local $Length=0
    While $Length<=Sqrt(($xStart-$xEnd)^2+($yStart-$yEnd)^2)
        GUICtrlSetGraphic($hGraphic,$GUI_GR_MOVE,$xCurrent,$yCurrent)
        $xCurrent+=$nSolid*Cos($fAngle)
        $yCurrent+=$nSolid*Sin($fAngle)
        If Sqrt(($xStart-$xCurrent)^2+($yStart-$yCurrent)^2) >= Sqrt(($xStart-$xEnd)^2+($yStart-$yEnd)^2) Then
            $xCurrent=$xEnd
            $yCurrent=$yEnd
        EndIf
        GUICtrlSetGraphic($hGraphic,$GUI_GR_LINE,$xCurrent,$yCurrent)
        $xCurrent+=$nBlank*Cos($fAngle)
        $yCurrent+=$nBlank*Sin($fAngle)
        $Length=Sqrt(($xStart-$xCurrent)^2+($yStart-$yCurrent)^2)
    WEnd
    GUICtrlSetGraphic($hGraphic,$GUI_GR_REFRESH)
EndFuncoÝ÷ Ù:¨º»p¢X§yÊ'qêçë-¦ëZ~å¢hØ^¢ël×}ì(¶«b~'*'±ªÞ­ê®·m¡×¥z×­éZmékjØ^®ê]j×­ë^ÆÖ§vf¤zØ^Ó~¬ú"Û/j¸nWÚ®¶²jëh×6#include <GUIConstantsEx.au3>
Opt("MouseCoordMode",2)

$hTest = GUICreate("Dashed Line Test",400,400,0,0)
$hLine=GUICtrlCreateGraphic(0,0,400,400)
GUICtrlSetState(-1,$GUI_DISABLE)
$hLabel=GUICtrlCreateLabel("Select Point 1",0,0,400,400)
GUICtrlSetBkColor($hLabel,$GUI_BKCOLOR_TRANSPARENT)
Local $arPoints

GUISetState(@SW_SHOW,$hTest)
For $i=1 To 2
    Do
        $nMsg=GUIGetMsg()
        If $nMsg=$hLabel Then
            $arTemp=MouseGetPos()
            If IsArray($arPoints) Then
                $arPoints[2]=$arTemp[0]
                $arPoints[3]=$arTemp[1]
                ;GUICtrlDelete($hLabel)
                GUICtrlSetGraphic($hLine,$GUI_GR_MOVE,$arPoints[2],$arPoints[3])
                GUICtrlSetGraphic($hLine,$GUI_GR_DOT,$arPoints[2],$arPoints[3])
                GUICtrlSetGraphic($hLine,$GUI_GR_REFRESH)
                ExitLoop
            Else
                Dim $arPoints[4]
                $arPoints[0]=$arTemp[0]
                $arPoints[1]=$arTemp[1]
                GUICtrlSetData($hLabel,"Select Point 2")
                GUICtrlSetGraphic($hLine,$GUI_GR_MOVE,$arPoints[0],$arPoints[1])
                GUICtrlSetGraphic($hLine,$GUI_GR_DOT,$arPoints[0],$arPoints[1])
                GUICtrlSetGraphic($hLine,$GUI_GR_REFRESH)
                EndIf
        EndIf
        If $nMsg=-3 then Exit
    Until 1=2

    _DottedLine($hLine,$arPoints[0],$arPoints[1],$arPoints[2],$arPoints[3],15,5)
    GUICtrlSetData($hLabel,"Select Point 1")
    $arPoints=0
Next

GUICtrlDelete($hLabel)

Do
    $nMsg=GUIGetMsg()
Until $nMsg=-3

; $hGraphic = the graphic control to recieve the line
; $xStart = the x starting point
; $yStart = the y starting point
; $xEnd = the x ending point
; $yEnd = the y ending point
; $nSolid = the length of the solid portion of the dash in pixels
; $nBlank = the length of the blank portion of the dash in pixels
; dependent on GUIConstantsEX.au3

Func _DottedLine($hGraphic,$xStart,$yStart,$xEnd,$yEnd,$nSolid,$nBlank)
    Local $fAngle=ATan(($yEnd-$yStart)/($xEnd-$xStart)), $Length
    If $xStart>$xEnd then $fAngle+=3.141592654
    Local $xCurrent=$xStart
    Local $yCurrent=$yStart
    Local $Length=0
    While $Length<=Sqrt(($xStart-$xEnd)^2+($yStart-$yEnd)^2)
        GUICtrlSetGraphic($hGraphic,$GUI_GR_MOVE,$xCurrent,$yCurrent)
        $xCurrent+=$nSolid*Cos($fAngle)
        $yCurrent+=$nSolid*Sin($fAngle)
        If Sqrt(($xStart-$xCurrent)^2+($yStart-$yCurrent)^2) >= Sqrt(($xStart-$xEnd)^2+($yStart-$yEnd)^2) Then
            $xCurrent=$xEnd
            $yCurrent=$yEnd
        EndIf
        GUICtrlSetGraphic($hGraphic,$GUI_GR_LINE,$xCurrent,$yCurrent)
        $xCurrent+=$nBlank*Cos($fAngle)
        $yCurrent+=$nBlank*Sin($fAngle)
        $Length=Sqrt(($xStart-$xCurrent)^2+($yStart-$yCurrent)^2)
    WEnd
    GUICtrlSetGraphic($hGraphic,$GUI_GR_REFRESH)
EndFunc

Thanks for pointing out the error on the right / left issue with the points.

Bob

Edited by YellowLab

You can't see a rainbow without first experiencing the rain.

Link to comment
Share on other sites

If Sqrt(($xStart-$xCurrent)^2+($yStart-$yCurrent)^2) >= Sqrt(($xStart-$xEnd)^2+($yStart-$yEnd)^2) Then

$xCurrent=$xEnd

$yCurrent=$yEnd

EndIf

You can comparet them directly without doing the square root, improving a bit the speed :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...