Announcement

Collapse
No announcement yet.

Drawing Bezier in PB

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

  • Drawing Bezier in PB

    I modified Gary's Bezier curves program from https://forum.powerbasic.com/forum/u...956#post657956

    I gotten it to draw the hockey stick, but the graphics is upside as below shown, how do I make it right side up with the Y-axis pointing upwards?

    Click image for larger version

Name:	Bezier Hockey.png
Views:	181
Size:	3.2 KB
ID:	821070


    Listing is as below
    Code:
    ' Draw Bezier.bas
    ' https://forum.powerbasic.com/forum/user-to-user-discussions/powerbasic-for-windows/55060-draw-smooth-curve-from-series-of-points-discussion?p=657956#post657956
    
            
    
    
    #COMPILE EXE
    #DIM ALL
    %Unicode = 1
    #INCLUDE "Win32API.inc"
    
    %IDC_Graphic = 500
    
    
    GLOBAL hDlg AS DWORD
    GLOBAL Gp() AS POINT
    GLOBAL GpCount AS LONG
    
    
    FUNCTION PBMAIN() AS LONG
       DIALOG NEW PIXELS, 0, "Click on graphics to display another set of curves",_
             60,100,500,500, %WS_OVERLAPPEDWINDOW TO hDlg
       CONTROL ADD GRAPHIC, hDlg, %IDC_Graphic, "", 0, 0, 500,500, %SS_NOTIFY
       GRAPHIC ATTACH hDlg, %IDC_Graphic, REDRAW
       DIALOG SHOW MODAL hDlg CALL DlgProc
    END FUNCTION
    
    
    
    CALLBACK FUNCTION DlgProc() AS LONG
       SELECT CASE CB.MSG
    
          CASE %WM_INITDIALOG
             RANDOMIZE TIMER
           ' create the hockey stick image
             CrPoints_Hockey
           ' varying these values of dt and Tension
           ' can smoothen the curves
            ' DrawCurves 0.75,0.01 ' 0.5   ' 10,1
             DrawCurves  0.5 ,0.25
    
          CASE %WM_COMMAND
             SELECT CASE CB.CTL
                CASE %IDC_Graphic
            '        when the graphics is clicked the
            '        curves is redrawn randomly
            '        initialize the points randomly
                     MovePoints(1)
                     DrawCurves 0.01, 0.5
    
             END SELECT
    
    
       END SELECT
    END FUNCTION
    
    
    '  Initialize the points with random values
    SUB MovePoints(Initialize AS  LONG)
       LOCAL i AS LONG
       IF Initialize THEN
          'initialize points
          GpCount = 20 : REDIM Gp(19)
          FOR i = 0 TO 18
               Gp(i).x = RND(100,300) : Gp(i).y = RND(100,300)
           NEXT i : Gp(19) = Gp(0)
       ELSE
          'move points
          FOR i = 0 TO 18
             Gp(i).x += RND(-1,1) : Gp(i).y += RND(-1,1)
          NEXT i
          Gp(19) = Gp(0)
       END IF
    END SUB
    
    
    
     ' create the hockey stick image
     ' allocate the points in a clockwise manner
     ' to form the close loop image
    SUB CrPoints_Hockey
        LOCAL scaleXY AS DOUBLE
     ' scale to fit the graph
        scaleXY  = 20
     ' note that points start from sequence = 0
       REDIM Gp(17)
    
       Gp(0).x  = 5.25 * scaleXY
       Gp(0).y  = 20  * scaleXY
       Gp(1).x  = 5.25 * scaleXY
       Gp(1).y  = 20  * scaleXY
       Gp(2).x  = 5.75* scaleXY
       Gp(2).y  = 20* scaleXY
       Gp(3).x  = 6 * scaleXY
       Gp(3).y  = 17 * scaleXY
    
       Gp(4).x  = 6.5 * scaleXY
       Gp(4).y  = 5 * scaleXY
    
      ' trough before the uphook
       Gp(5).x  = 7.15 * scaleXY
       Gp(5).y  = 4.3 * scaleXY    '4.3
    
       Gp(6).x  = 8* scaleXY
       Gp(6).y  = 5 * scaleXY
    
       Gp(7).x  = 8.8* scaleXY
       Gp(7).y  = 6.8* scaleXY
    
      ' tip of the uphook
       Gp(8).x  = 9.5* scaleXY
       Gp(8).y  = 7.* scaleXY
    
       Gp(9).x  = 10 * scaleXY
       Gp(9).y  = 6.3 * scaleXY
       Gp(10).x  = 8.8 * scaleXY
       Gp(10).y  = 3.1 * scaleXY
    
       Gp(11).x  = 8.5 * scaleXY
       Gp(11).y  = 3 * scaleXY
       Gp(12).x  = 8 * scaleXY
       Gp(12).y  = 2.8 * scaleXY
    
       Gp(13).x  = 6.1 * scaleXY
       Gp(13).y  = 2.8 * scaleXY
       Gp(14).x  = 5.5 * scaleXY
       Gp(14).y  = 3 * scaleXY
       Gp(15).x  = 5 * scaleXY
       Gp(15).y  = 4 * scaleXY
       Gp(16).x  = 5 * scaleXY
       Gp(16).y  = 17* scaleXY
       Gp(17).x  = 5.25 * scaleXY
       Gp(17).y  = 20* scaleXY
    
    END SUB
    
    
    
    
    SUB DrawBezier(BYVAL dt AS SINGLE, pt0 AS POINT, _
             pt1 AS POINT, pt2 AS POINT, pt3 AS POINT)
        LOCAL t,x0,y0,x1,y1 AS SINGLE
        t = 0
        x1 = pt0.x * (1 - t) ^ 3  + pt1.x * 3 * t * (1 - t) ^ 2 + _
               pt2.x * 3 * t ^ 2 * (1 - t) + pt3.x * t ^ 3
        y1 = pt0.y * (1 - t) ^ 3  + pt1.y * 3 * t * (1 - t) ^ 2 + _
               pt2.y * 3 * t ^ 2 * (1 - t) + pt3.y * t ^ 3
        t = t + dt
        DO WHILE t < 1
            x0 = x1 : y0 = y1
            x1 = pt0.x * (1 - t) ^ 3  + pt1.x * 3 * t * (1 - t) ^ 2 + _
                     pt2.x * 3 * t ^ 2 * (1 - t) + pt3.x * t ^ 3
            y1 = pt0.y * (1 - t) ^ 3  + pt1.y * 3 * t * (1 - t) ^ 2 + _
                    pt2.y * 3 * t ^ 2 * (1 - t) + pt3.y * t ^ 3
            GRAPHIC LINE  (x0, y0)-(x1, y1), %BLUE
            t = t + dt
        LOOP
        ' Connect to the final point.
        t = 1
        x0 = x1 : y0 = y1 : x1 = pt3.x : y1 = pt3.y
        GRAPHIC LINE (x0, y0)-(x1, y1), %BLUE
    END SUB
    
    
    
    
    ' varying these values of dt and Tension
    ' can smoothen the curves
    SUB DrawCurves(BYVAL dt AS SINGLE, BYVAL tension AS SINGLE)
       LOCAL control_scale AS SINGLE, i AS INTEGER
       LOCAL pt,pt_before, pt_after, pt_after2 AS POINT
       LOCAL Di, DiPlus1, p1, p2, p3, p4 AS POINT
    
       GRAPHIC CLEAR
        control_scale = CSNG(tension / 0.5 * 0.175)
        FOR i = 0 TO UBOUND(Gp) - 1
            pt_before = Gp(MAX(i - 1, 0))
            pt = Gp(i)
            pt_after = Gp(i + 1)
            pt_after2 = Gp(MIN(i + 2, UBOUND(Gp)))
    
            p1 = Gp(i)
            p4 = Gp(i + 1)
    
            Di.X = pt_after.X - pt_before.X
            Di.Y = pt_after.Y - pt_before.Y
            p2.X = pt.X + control_scale * Di.X
            p2.Y = pt.Y + control_scale * Di.Y
    
            DiPlus1.X = pt_after2.X - Gp(i).X
            DiPlus1.Y = pt_after2.Y - Gp(i).Y
            p3.X = pt_after.X - control_scale * DiPlus1.X
            p3.Y = pt_after.Y - control_scale * DiPlus1.Y
    
            DrawBezier dt, p1, p2, p3, p4
        NEXT i
        GRAPHIC REDRAW
    END SUB​

  • #2
    Also how do I color fill the hockey stick?

    Comment


    • #3
      From Help for Graphics -
      When a Graphic Window or Graphic Bitmap is created, the default POS is set to (0,0), which is the upper left corner.​
      Subtract wanted Y position from the Y size, use the difference.

      Also how do I color fill the hockey stick?
      See Help for GRAPHIC PAINT.

      Cheers,
      Dale

      Comment


      • #4
        easiest way use Graphic Scale

        FUNCTION PBMAIN() AS LONG
        DIALOG NEW PIXELS, 0, "Click on graphics to display another set of curves",_
        60,100,500,500, %WS_OVERLAPPEDWINDOW TO hDlg
        CONTROL ADD GRAPHIC, hDlg, %IDC_Graphic, "", 0, 0, 500,500, %SS_NOTIFY
        GRAPHIC ATTACH hDlg, %IDC_Graphic, REDRAW
        GRAPHIC SCALE (0, 500) - (500, 0)
        DIALOG SHOW MODAL hDlg CALL DlgProc
        END FUNCTION​

        Comment


        • #5
          Thank you Sir Rod , that was short and good

          Comment


          • #6
            Tim
            To color fill the hockey stick , add this code


            Code:
              CASE %WM_INITDIALOG
                   ' create the hockey stick image
                     CrPoints_Hockey
                   ' varying these values of dt and Tension
                   ' can smoothen the curves
                    ' DrawCurves 0.75,0.01 ' 0.5   ' 10,1
                     DrawCurves  0.5 ,0.25
                   ' Shade a point inside the hockey stick with green diagonal lines
                   ' within the borders of the stick
                    GRAPHIC PAINT BORDER (110, 80), %GREEN, %BLUE, 4

            Comment


            • #7
              deleted, was a serious error
              Dale

              Comment


              • #8
                Response to post above deleted since no longer relevant

                Comment


                • #9
                  OOPS! (what else to say)
                  erroneous post deleted
                  Dale

                  Comment


                  • #10
                    Thank you Anne

                    Comment

                    Working...
                    X