Announcement

Collapse
No announcement yet.

Line, any angle

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Line, any angle

    How to draw a line at any angle using DDT.
    Fim W.
    Fim Wästberg

  • #2
    Draw on a dialog, or GRAPHIC WINDOW/CONTROL?

    There is a difference.

    (I've seen non-rectangular dialogs which use angled lines for borders, frames, connecting controls) I can't help with this (SDK?) work. I just know it exists.

    Then there are GRAPHIC WINDOW and GRAPHIC CONTROLS. Are you having a problem with a GRAPHIC command?

    Cheers
    Dale

    Comment


    • #3
      Code:
      'PBWin10 program
      FUNCTION PBMAIN () AS LONG
      
      LOCAL DegreesToRadians, angle AS DOUBLE
      DegreesToRadians = 3.14159265358979/180
      
      LOCAL hWin,xStart, yStart, xEnd, yEnd, LineLength AS LONG
      
      
      GRAPHIC WINDOW "test", 100, 100, 500,500 TO hWin
      GRAPHIC ATTACH hWin, 0
      
      xStart = 250
      yStart = 250
      LineLength = 200
      
      FOR angle = 1 TO 360 STEP 10
          xEnd = xStart + LineLength*COS(angle * DegreesToRadians)
          yEnd = yStart + LineLength*SIN(angle * DegreesToRadians)
      
          SLEEP 100 'to slow it down
          GRAPHIC LINE (xStart,yStart) - (xEnd,yEnd)
      
      NEXT
      
      MSGBOX "done"
      
      END FUNCTION

      Comment


      • #4
        As I wrote, I use DDT, not GRAPHIC.
        It is ok to use an API if there is one.
        Fim Wästberg

        Comment


        • #5
          Originally posted by Fim Wästberg View Post
          How to draw a line at any angle using DDT.
          Fim W.
          You can't. There are no "DDT" drawing statements/functions. Intrinsic DDT and GRAPHIC statements/functions are two separate functional groups of PowerBasic syntax.

          CONTROL ADD GRAPHIC is a DDT statement, but once you've added the Graphic control, you need to use the GRAPHIC subset of statements/function or mix intrinsic PB statements with API calls.

          Why don't you use the intrinsic GRAPHIC statements/functions? They are a lot easier to use than API calls to a DDT control.

          Comment


          • #6
            My problem is as follows: First, I draw a line (at any angle) and later in the program and I will remove the line. Then a new line that will be deleted, etc.
            All that should be in a dialogue based on DDT.
            Fim Wästberg

            Comment


            • #7
              Well, with more information, such as the length of the line, what else is near the line, ( a couple of images would even suffice) someone might be able to give a workable solution. For example IMAGEX, GRAPHIC CONTROL. Draw the line on a bitmap at the angle you want, using the same colour for the background that is the background colour of the Window. Z-order control might be required to place the image behind other features as was done in the accompanying *.gif whic is really a regular rectangular graphic, sans border of course.

              Click image for larger version

Name:	help_meeee.gif
Views:	1
Size:	3.3 KB
ID:	774237
              Rod
              "To every unsung hero in the universe
              To those who roam the skies and those who roam the earth
              To all good men of reason may they never thirst " - from "Heaven Help the Devil" by G. Lightfoot

              Comment


              • #8
                Very similar to Paul's but the 181x181 graphic control is scaled to (-181,-181)-(181,181). You can choose the current line through the centre or from the centre by changing the commented GRAPHIC LINE statements.
                Code:
                #COMPILE EXE
                #DIM ALL
                
                '--------------------------------------------------------------------------------------------------------------------------------------------------------------
                '   ** Includes **
                '--------------------------------------------------------------------------------------------------------------------------------------------------------------
                
                #INCLUDE ONCE "WIN32API.INC"
                
                '--------------------------------------------------------------------------------------------------------------------------------------------------------------
                
                '--------------------------------------------------------------------------------------------------------------------------------------------------------------
                '   ** Constants **
                '--------------------------------------------------------------------------------------------------------------------------------------------------------------
                
                %IDD_DIALOG1       =  101
                %IDC_GRAPHIC1      = 1001
                %IDC_INCREASEANGLE = 1002
                %IDC_DECREASEANGLE = 1004
                %IDC_ANGLE         = 1003
                
                '--------------------------------------------------------------------------------------------------------------------------------------------------------------
                
                '--------------------------------------------------------------------------------------------------------------------------------------------------------------
                '   ** Main Application Entry Point **
                '--------------------------------------------------------------------------------------------------------------------------------------------------------------
                FUNCTION PBMAIN()
                  ShowDIALOG1 %HWND_DESKTOP
                END FUNCTION
                '--------------------------------------------------------------------------------------------------------------------------------------------------------------
                
                '--------------------------------------------------------------------------------------------------------------------------------------------------------------
                '   ** CallBacks **
                '--------------------------------------------------------------------------------------------------------------------------------------------------------------
                CALLBACK FUNCTION ShowDIALOG1Proc()
                
                  STATIC angle AS LONG
                  GRAPHIC ATTACH CB.HNDL, %IDC_GRAPHIC1, REDRAW
                  SELECT CASE AS LONG CB.MSG
                    CASE %WM_INITDIALOG
                      draw_angle CB.HNDL, %IDC_GRAPHIC1, angle
                
                    CASE %WM_NCACTIVATE
                      STATIC hWndSaveFocus AS DWORD
                      IF ISFALSE CB.WPARAM THEN
                        ' Save control focus
                        hWndSaveFocus = GetFocus()
                      ELSEIF hWndSaveFocus THEN
                        ' Restore control focus
                        SetFocus(hWndSaveFocus)
                        hWndSaveFocus = 0
                      END IF
                
                    CASE %WM_COMMAND
                      ' Process control notifications
                      SELECT CASE AS LONG CB.CTL
                        CASE %IDC_INCREASEANGLE
                          IF CB.CTLMSG = %BN_CLICKED OR CB.CTLMSG = 1 THEN
                            IF angle=360 THEN angle=0
                            angle+=7
                            draw_angle CB.HNDL, %IDC_GRAPHIC1, angle
                            CONTROL SET TEXT CB.HNDL, %IDC_ANGLE, FORMAT$(angle)
                          END IF
                
                        CASE %IDC_ANGLE
                
                        CASE %IDC_DECREASEANGLE
                          IF CB.CTLMSG = %BN_CLICKED OR CB.CTLMSG = 1 THEN
                            angle-=7
                            IF angle=0 THEN angle=360
                            CONTROL SET TEXT CB.HNDL, %IDC_ANGLE, FORMAT$(angle)
                            draw_angle CB.HNDL, %IDC_GRAPHIC1, angle
                          END IF
                
                      END SELECT
                  END SELECT
                END FUNCTION
                '--------------------------------------------------------------------------------------------------------------------------------------------------------------
                SUB draw_angle(hDlg AS LONG, eyeD AS LONG, angle AS LONG)
                  LOCAL startX, startY, end_X, end_Y, hypo AS SINGLE
                  LOCAL distance AS LONG
                  LOCAL DegreesToRadians AS DOUBLE
                  GRAPHIC SCALE(-181,-181)-(181,181)
                  DegreesToRadians = 3.14159265358979/180
                  distance=180
                  GRAPHIC CLEAR
                  end_X = startX + distance*COS(angle * DegreesToRadians)
                  end_Y = startY + distance*SIN(angle * DegreesToRadians)
                  'graphic line (startX, startY)-(end_X, end_Y), %RGB_red 'produce a line from centre
                  GRAPHIC LINE (-end_X, -end_Y)-(end_X, end_Y), %RGB_RED  'produce a line through centre
                  GRAPHIC REDRAW
                
                END SUB
                
                '--------------------------------------------------------------------------------------------------------------------------------------------------------------
                '   ** Dialogs **
                '--------------------------------------------------------------------------------------------------------------------------------------------------------------
                FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG
                  LOCAL lRslt  AS LONG
                  LOCAL hDlg   AS DWORD
                  LOCAL hFont1 AS DWORD
                
                  DIALOG NEW PIXELS, hParent, "Graphic any angle", 105, 114, 400, 277, %WS_POPUP OR %WS_BORDER OR %WS_DLGFRAME OR %WS_SYSMENU OR %WS_CLIPSIBLINGS OR _
                    %WS_VISIBLE OR %DS_MODALFRAME OR %DS_3DLOOK OR %DS_NOFAILCREATE OR %DS_SETFONT, %WS_EX_CONTROLPARENT OR %WS_EX_LEFT OR %WS_EX_LTRREADING OR _
                    %WS_EX_RIGHTSCROLLBAR, TO hDlg
                  CONTROL ADD GRAPHIC, hDlg, %IDC_GRAPHIC1, "", 110, 8, 181, 181, %WS_CHILD OR %WS_VISIBLE OR %SS_NOTIFY
                  CONTROL ADD BUTTON,  hDlg, %IDC_INCREASEANGLE, "Increase Angle", 8, 240, 150, 24
                  CONTROL ADD LABEL,   hDlg, %IDC_ANGLE, "0", 160, 240, 75, 20, %WS_CHILD OR %WS_VISIBLE OR %SS_CENTER OR %SS_CENTERIMAGE OR %SS_NOTIFY, %WS_EX_LEFT OR _
                    %WS_EX_LTRREADING
                  CONTROL ADD BUTTON,  hDlg, %IDC_DECREASEANGLE, "Decrease Angle", 240, 240, 150, 24
                
                  FONT NEW "MS Sans Serif", 12, 0, %ANSI_CHARSET TO hFont1
                
                  CONTROL SET FONT hDlg, %IDC_GRAPHIC1, hFont1
                  CONTROL SET FONT hDlg, %IDC_INCREASEANGLE, hFont1
                  CONTROL SET FONT hDlg, %IDC_ANGLE, hFont1
                  CONTROL SET FONT hDlg, %IDC_DECREASEANGLE, hFont1
                
                  DIALOG SHOW MODAL hDlg, CALL ShowDIALOG1Proc TO lRslt
                
                  FONT END hFont1
                
                  FUNCTION = lRslt
                END FUNCTION
                '--------------------------------------------------------------------------------------------------------------------------------------------------------------
                Rod
                "To every unsung hero in the universe
                To those who roam the skies and those who roam the earth
                To all good men of reason may they never thirst " - from "Heaven Help the Devil" by G. Lightfoot

                Comment


                • #9
                  Rodney,
                  That seems to be what I'm looking for.
                  Now I've got something I can work on.
                  Many thanks.
                  Fim W.
                  Fim Wästberg

                  Comment


                  • #10
                    Rod, you could move
                    Code:
                    GRAPHIC ATTACH CB.HNDL, %IDC_GRAPHIC1, REDRAW
                    So it is only executed once.

                    Comment


                    • #11
                      Originally posted by Rodney Hicks View Post
                      Well, with more information, such as the length of the line, what else is near the line, ( a couple of images would even suffice) someone might be able to give a workable solution. For example IMAGEX, GRAPHIC CONTROL. Draw the line on a bitmap at the angle you want, using the same colour for the background that is the background colour of the Window. Z-order control might be required to place the image behind other features as was done in the accompanying *.gif whic is really a regular rectangular graphic, sans border of course.
                      You can also copy a background bitmap and just draw on top of it, GRAPHIC LINE only draws a line.. For example (PBWIN10)

                      Code:
                      #compile exe
                      #dim all
                      #debug display
                      
                      #include once "WIN32API.INC"
                      
                      global ghw, ghbk   as dword
                      global gDegreesToRadians as double
                      
                      '--------------------------------------------------------------------------------------------------------------------------------------------------------------
                      function pbmain as long
                        local hFont1 as dword
                        local startX, startY, end_X, end_Y, hypo as single
                        local angle, distance, lwidth, lheight  as long
                        local DegreesToRadians as double
                        '
                        desktop get client to lwidth, lheight
                        lwidth/= 2: lheight = lwidth
                        graphic window new "press a key to finish", _
                                    lwidth/4, lheight/4, lwidth, lheight to gHW
                        graphic attach gHW, 0, redraw
                        font new "MS Sans Serif", 12, 0, %ANSI_CHARSET to hFont1
                        graphic set font hfont1
                        graphic bitmap new lwidth, lheight to ghbk
                        ' any bitmap you like for a background
                        graphic bitmap load "c:\pics\patti.bmp", lwidth, lheight, %halftone to ghbk
                        gDegreesToRadians = 3.14159265358979/180
                        graphic scale(-181,-181)-(181,181)
                        distance = 180
                        graphic width 3
                        for angle = 0 to 360 step 5
                            graphic copy ghbk, 0
                            startX = lwidth/4: starty = lheight/4
                            end_X = startX + distance*cos(angle * gDegreesToRadians)
                            end_Y = startY + distance*sin(angle * gDegreesToRadians)
                            graphic line (-end_X, -end_Y)-(end_X, end_Y), %rgb_red
                            graphic redraw
                            dialog doevents 50
                        next
                        graphic waitkey$
                        graphic set font 0
                        graphic detach
                        font end hFont1
                        graphic attach ghBK, 0
                        graphic bitmap end
                      end function

                      Comment


                      • #12
                        Originally posted by Chris Holbrook View Post
                        Rod, you could move
                        Code:
                        GRAPHIC ATTACH CB.HNDL, %IDC_GRAPHIC1, REDRAW
                        So it is only executed once.
                        Well, the REDRAW would have to happen with each change so it might as well be with the change itself. What should not be in the sub is the GRAPHIC SCALE, but I put it there so it would be noticed.
                        I used a graphic control because it seemed that something that worked with DDT(controls etc.) was desired by the OP.
                        Rod
                        "To every unsung hero in the universe
                        To those who roam the skies and those who roam the earth
                        To all good men of reason may they never thirst " - from "Heaven Help the Devil" by G. Lightfoot

                        Comment


                        • #13
                          The key word REDRAW in the GRAPHIC ATTACH statement does not cause a redraw. It sets things so you must use GRAPHIC REDRAW to force a redraw instead of automatic redraws after each change.

                          Unless you do a GRAPHIC DETACH, or GRAPHIC ATTACH to a different graphic target (window or control), you "should" only need one GRAPHIC ATTACH statement.

                          GRAPHIC REDRAW should be in the loop.

                          Cheers,
                          Dale

                          Comment


                          • #14
                            Originally posted by Dale Yarker View Post
                            GRAPHIC REDRAW should be in the loop.
                            yes, just once.

                            In Rod's example the GRAPHIC ATTACH statement, which has some overhead, is executed every time a message is processed by the wndproc.

                            Comment


                            • #15
                              My bad. Yes, the GRAPHIC ATTACH statement could/should have gone in the ShowDIALOG1 function, and I thought of putting it there, but didn't. I didn't really need it or the GRAPHIC REDRAW since I was only doing one graphic operation per button click.
                              Rod
                              "To every unsung hero in the universe
                              To those who roam the skies and those who roam the earth
                              To all good men of reason may they never thirst " - from "Heaven Help the Devil" by G. Lightfoot

                              Comment


                              • #16
                                .. didn't really need it or the GRAPHIC REDRAW ...
                                Rodney, I could be wrong, but it sounds like you still don't "have it". GRAPHIC ATTACH hWin, ID, REDRAW actually stops automatic redraws. If only doing one graphic operation then do not put "coma REDRAW" on the end; just GRAPHIC ATTACH hWin, ID. Then no need GRAPHIC REDRAW at all! Sorry if I read too much into your wording.
                                Dale

                                Comment


                                • #17
                                  Dale, I do understand the significance. I had originally been going to make the program loop through the degrees similar to Paul's and Chris's and never bothered to take it out when I changed my mind. It was placed there so someone without much 'graphic' experience would become familiar with it. Had I made the little program all in one sitting, it probably wouldn't have made the cut, but interruptions have been the order of the day for a week. This further discussion will only further enhance understanding.
                                  Rod
                                  "To every unsung hero in the universe
                                  To those who roam the skies and those who roam the earth
                                  To all good men of reason may they never thirst " - from "Heaven Help the Devil" by G. Lightfoot

                                  Comment


                                  • #18
                                    Dale

                                    Comment


                                    • #19
                                      There's a good example of drawing lines in any angle in - Samples\DDT\Graphic\PwrClock\Pwrclock.bas

                                      Comment


                                      • #20
                                        Börje,
                                        Dont have it in my Samples. Pls can you mail it to me? fim@fim.nu
                                        /Fim
                                        Fim Wästberg

                                        Comment

                                        Working...
                                        X