I'm stuck on drawing a bezier curve in visual basic












0















https://i.snag.gy/iQUyxz.jpg



I'm working on windows application form in VS 2017. The requirement is to draw a bezier curve by using three input numbers like (56, 150, 400).



I'm really confused as how to draw this.



Private Sub DrawCurve_Click(sender As Object, e As EventArgs) Handles DrawCurve.Click
Try
'Declared myColor earlier
Dim color = myColor
Dim myPen As Pen = New Pen(myColor, 8)
Dim myGraphics As Graphics = Me.CreateGraphics
Dim pt1 As Point = New Point(CInt(curvePT1.Text),CInt(curvePT1.Text))
Dim pt2 As Point = New Point(CInt(curvePT2.Text),CInt(curvePT2.Text))
Dim pt3 As Point = New Point(CInt(curvePT2.Text),CInt(curvePT2.Text))
Dim curvedPoints As Point() = {pt1, pt2, pt3}
'Draw Bezier Curve
CreateGraphics().Clear(Form.ActiveForm.BackColor)
myGraphics.DrawBezier(myPen, curvedPoints)
Catch
MsgBox("Please enter numerical value!")
End Try
End Sub









share|improve this question

























  • I've tried this code but the bezier curve in visual basic requires 4 points. With four points it's working fine. Maybe I'm not getting the requirement as shown in the picture, because I never studies bezier curve.

    – Muhammad Umer Farooq
    Nov 26 '18 at 8:53











  • Dim myGraphics As Graphics = Me.CreateGraphics | myGraphics.DrawBezier(myPen, New Point(56, 150), New Point(150, 300), New Point(300, 400))

    – Muhammad Umer Farooq
    Nov 26 '18 at 8:54








  • 1





    The relevant Wikipedia page refers to quadratic and cubic Bezier curves but, the .NET DrawBezier method apparently only does the cubic ones. There is a DrawCurve method that will work with three points.

    – jmcilhinney
    Nov 26 '18 at 9:09






  • 1





    As for your code, don't EVER call CreatGraphics and especially don't call it twice. If you ever do create a disposable object though, make sure that you dispose it. If you want to draw using GDI+ and you want to draw on a control rather than on an image, handle the Paint event and use the Graphics object provided.

    – jmcilhinney
    Nov 26 '18 at 9:17






  • 1





    Consider what @jmcilhinney said about CreateGraphics. This is very important. You must use the a Control's Paint() event's PaintEventArgs Graphics object to draw your shapes. You'll find out why soon enough (read the Docs, anyway). I suggest to use the GraphicsPath.AddBeziers method to draw the Beziers curves. After you add the Points required to describe the Bezier, you can then inspect the GraphicsPath object and see how the splines have been constructed: what control points are created.

    – Jimi
    Nov 26 '18 at 15:47
















0















https://i.snag.gy/iQUyxz.jpg



I'm working on windows application form in VS 2017. The requirement is to draw a bezier curve by using three input numbers like (56, 150, 400).



I'm really confused as how to draw this.



Private Sub DrawCurve_Click(sender As Object, e As EventArgs) Handles DrawCurve.Click
Try
'Declared myColor earlier
Dim color = myColor
Dim myPen As Pen = New Pen(myColor, 8)
Dim myGraphics As Graphics = Me.CreateGraphics
Dim pt1 As Point = New Point(CInt(curvePT1.Text),CInt(curvePT1.Text))
Dim pt2 As Point = New Point(CInt(curvePT2.Text),CInt(curvePT2.Text))
Dim pt3 As Point = New Point(CInt(curvePT2.Text),CInt(curvePT2.Text))
Dim curvedPoints As Point() = {pt1, pt2, pt3}
'Draw Bezier Curve
CreateGraphics().Clear(Form.ActiveForm.BackColor)
myGraphics.DrawBezier(myPen, curvedPoints)
Catch
MsgBox("Please enter numerical value!")
End Try
End Sub









share|improve this question

























  • I've tried this code but the bezier curve in visual basic requires 4 points. With four points it's working fine. Maybe I'm not getting the requirement as shown in the picture, because I never studies bezier curve.

    – Muhammad Umer Farooq
    Nov 26 '18 at 8:53











  • Dim myGraphics As Graphics = Me.CreateGraphics | myGraphics.DrawBezier(myPen, New Point(56, 150), New Point(150, 300), New Point(300, 400))

    – Muhammad Umer Farooq
    Nov 26 '18 at 8:54








  • 1





    The relevant Wikipedia page refers to quadratic and cubic Bezier curves but, the .NET DrawBezier method apparently only does the cubic ones. There is a DrawCurve method that will work with three points.

    – jmcilhinney
    Nov 26 '18 at 9:09






  • 1





    As for your code, don't EVER call CreatGraphics and especially don't call it twice. If you ever do create a disposable object though, make sure that you dispose it. If you want to draw using GDI+ and you want to draw on a control rather than on an image, handle the Paint event and use the Graphics object provided.

    – jmcilhinney
    Nov 26 '18 at 9:17






  • 1





    Consider what @jmcilhinney said about CreateGraphics. This is very important. You must use the a Control's Paint() event's PaintEventArgs Graphics object to draw your shapes. You'll find out why soon enough (read the Docs, anyway). I suggest to use the GraphicsPath.AddBeziers method to draw the Beziers curves. After you add the Points required to describe the Bezier, you can then inspect the GraphicsPath object and see how the splines have been constructed: what control points are created.

    – Jimi
    Nov 26 '18 at 15:47














0












0








0








https://i.snag.gy/iQUyxz.jpg



I'm working on windows application form in VS 2017. The requirement is to draw a bezier curve by using three input numbers like (56, 150, 400).



I'm really confused as how to draw this.



Private Sub DrawCurve_Click(sender As Object, e As EventArgs) Handles DrawCurve.Click
Try
'Declared myColor earlier
Dim color = myColor
Dim myPen As Pen = New Pen(myColor, 8)
Dim myGraphics As Graphics = Me.CreateGraphics
Dim pt1 As Point = New Point(CInt(curvePT1.Text),CInt(curvePT1.Text))
Dim pt2 As Point = New Point(CInt(curvePT2.Text),CInt(curvePT2.Text))
Dim pt3 As Point = New Point(CInt(curvePT2.Text),CInt(curvePT2.Text))
Dim curvedPoints As Point() = {pt1, pt2, pt3}
'Draw Bezier Curve
CreateGraphics().Clear(Form.ActiveForm.BackColor)
myGraphics.DrawBezier(myPen, curvedPoints)
Catch
MsgBox("Please enter numerical value!")
End Try
End Sub









share|improve this question
















https://i.snag.gy/iQUyxz.jpg



I'm working on windows application form in VS 2017. The requirement is to draw a bezier curve by using three input numbers like (56, 150, 400).



I'm really confused as how to draw this.



Private Sub DrawCurve_Click(sender As Object, e As EventArgs) Handles DrawCurve.Click
Try
'Declared myColor earlier
Dim color = myColor
Dim myPen As Pen = New Pen(myColor, 8)
Dim myGraphics As Graphics = Me.CreateGraphics
Dim pt1 As Point = New Point(CInt(curvePT1.Text),CInt(curvePT1.Text))
Dim pt2 As Point = New Point(CInt(curvePT2.Text),CInt(curvePT2.Text))
Dim pt3 As Point = New Point(CInt(curvePT2.Text),CInt(curvePT2.Text))
Dim curvedPoints As Point() = {pt1, pt2, pt3}
'Draw Bezier Curve
CreateGraphics().Clear(Form.ActiveForm.BackColor)
myGraphics.DrawBezier(myPen, curvedPoints)
Catch
MsgBox("Please enter numerical value!")
End Try
End Sub






vb.net






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 9:10







Muhammad Umer Farooq

















asked Nov 26 '18 at 8:50









Muhammad Umer FarooqMuhammad Umer Farooq

63




63













  • I've tried this code but the bezier curve in visual basic requires 4 points. With four points it's working fine. Maybe I'm not getting the requirement as shown in the picture, because I never studies bezier curve.

    – Muhammad Umer Farooq
    Nov 26 '18 at 8:53











  • Dim myGraphics As Graphics = Me.CreateGraphics | myGraphics.DrawBezier(myPen, New Point(56, 150), New Point(150, 300), New Point(300, 400))

    – Muhammad Umer Farooq
    Nov 26 '18 at 8:54








  • 1





    The relevant Wikipedia page refers to quadratic and cubic Bezier curves but, the .NET DrawBezier method apparently only does the cubic ones. There is a DrawCurve method that will work with three points.

    – jmcilhinney
    Nov 26 '18 at 9:09






  • 1





    As for your code, don't EVER call CreatGraphics and especially don't call it twice. If you ever do create a disposable object though, make sure that you dispose it. If you want to draw using GDI+ and you want to draw on a control rather than on an image, handle the Paint event and use the Graphics object provided.

    – jmcilhinney
    Nov 26 '18 at 9:17






  • 1





    Consider what @jmcilhinney said about CreateGraphics. This is very important. You must use the a Control's Paint() event's PaintEventArgs Graphics object to draw your shapes. You'll find out why soon enough (read the Docs, anyway). I suggest to use the GraphicsPath.AddBeziers method to draw the Beziers curves. After you add the Points required to describe the Bezier, you can then inspect the GraphicsPath object and see how the splines have been constructed: what control points are created.

    – Jimi
    Nov 26 '18 at 15:47



















  • I've tried this code but the bezier curve in visual basic requires 4 points. With four points it's working fine. Maybe I'm not getting the requirement as shown in the picture, because I never studies bezier curve.

    – Muhammad Umer Farooq
    Nov 26 '18 at 8:53











  • Dim myGraphics As Graphics = Me.CreateGraphics | myGraphics.DrawBezier(myPen, New Point(56, 150), New Point(150, 300), New Point(300, 400))

    – Muhammad Umer Farooq
    Nov 26 '18 at 8:54








  • 1





    The relevant Wikipedia page refers to quadratic and cubic Bezier curves but, the .NET DrawBezier method apparently only does the cubic ones. There is a DrawCurve method that will work with three points.

    – jmcilhinney
    Nov 26 '18 at 9:09






  • 1





    As for your code, don't EVER call CreatGraphics and especially don't call it twice. If you ever do create a disposable object though, make sure that you dispose it. If you want to draw using GDI+ and you want to draw on a control rather than on an image, handle the Paint event and use the Graphics object provided.

    – jmcilhinney
    Nov 26 '18 at 9:17






  • 1





    Consider what @jmcilhinney said about CreateGraphics. This is very important. You must use the a Control's Paint() event's PaintEventArgs Graphics object to draw your shapes. You'll find out why soon enough (read the Docs, anyway). I suggest to use the GraphicsPath.AddBeziers method to draw the Beziers curves. After you add the Points required to describe the Bezier, you can then inspect the GraphicsPath object and see how the splines have been constructed: what control points are created.

    – Jimi
    Nov 26 '18 at 15:47

















I've tried this code but the bezier curve in visual basic requires 4 points. With four points it's working fine. Maybe I'm not getting the requirement as shown in the picture, because I never studies bezier curve.

– Muhammad Umer Farooq
Nov 26 '18 at 8:53





I've tried this code but the bezier curve in visual basic requires 4 points. With four points it's working fine. Maybe I'm not getting the requirement as shown in the picture, because I never studies bezier curve.

– Muhammad Umer Farooq
Nov 26 '18 at 8:53













Dim myGraphics As Graphics = Me.CreateGraphics | myGraphics.DrawBezier(myPen, New Point(56, 150), New Point(150, 300), New Point(300, 400))

– Muhammad Umer Farooq
Nov 26 '18 at 8:54







Dim myGraphics As Graphics = Me.CreateGraphics | myGraphics.DrawBezier(myPen, New Point(56, 150), New Point(150, 300), New Point(300, 400))

– Muhammad Umer Farooq
Nov 26 '18 at 8:54






1




1





The relevant Wikipedia page refers to quadratic and cubic Bezier curves but, the .NET DrawBezier method apparently only does the cubic ones. There is a DrawCurve method that will work with three points.

– jmcilhinney
Nov 26 '18 at 9:09





The relevant Wikipedia page refers to quadratic and cubic Bezier curves but, the .NET DrawBezier method apparently only does the cubic ones. There is a DrawCurve method that will work with three points.

– jmcilhinney
Nov 26 '18 at 9:09




1




1





As for your code, don't EVER call CreatGraphics and especially don't call it twice. If you ever do create a disposable object though, make sure that you dispose it. If you want to draw using GDI+ and you want to draw on a control rather than on an image, handle the Paint event and use the Graphics object provided.

– jmcilhinney
Nov 26 '18 at 9:17





As for your code, don't EVER call CreatGraphics and especially don't call it twice. If you ever do create a disposable object though, make sure that you dispose it. If you want to draw using GDI+ and you want to draw on a control rather than on an image, handle the Paint event and use the Graphics object provided.

– jmcilhinney
Nov 26 '18 at 9:17




1




1





Consider what @jmcilhinney said about CreateGraphics. This is very important. You must use the a Control's Paint() event's PaintEventArgs Graphics object to draw your shapes. You'll find out why soon enough (read the Docs, anyway). I suggest to use the GraphicsPath.AddBeziers method to draw the Beziers curves. After you add the Points required to describe the Bezier, you can then inspect the GraphicsPath object and see how the splines have been constructed: what control points are created.

– Jimi
Nov 26 '18 at 15:47





Consider what @jmcilhinney said about CreateGraphics. This is very important. You must use the a Control's Paint() event's PaintEventArgs Graphics object to draw your shapes. You'll find out why soon enough (read the Docs, anyway). I suggest to use the GraphicsPath.AddBeziers method to draw the Beziers curves. After you add the Points required to describe the Bezier, you can then inspect the GraphicsPath object and see how the splines have been constructed: what control points are created.

– Jimi
Nov 26 '18 at 15:47












2 Answers
2






active

oldest

votes


















0














Drawing a spline through three points doesn't produce the same result as drawing a cubic Bezier curve. A spline curve tends to have sharper bends (think of a wire coathanger).



Actually the .Net framework does include quadratic Beziers: the QuadraticBezierSegment in WPF. With suitable references, it wouldn't be hard to make a function to use it in a System.Drawing context.



But it's even easier to draw a quadratic Bezier curve using plain arithmetic. You start by choosing the number of points to plot - 5 points might be enough, or more if you want a smoother result. Here's a function (plus 2 supporting functions) which, given 3 control points and the number of points you want to plot, returns a list of PointFs representing the quadratic Bezier:



        Public Function QuadraticBezier(controlPoint1 As PointF, controlPoint2 As PointF, 
controlPoint3 As PointF, numberOfPoints As Integer) As PointF()

Dim lst As New List(Of PointF)
For t As Double = 0 To 1 Step 1 / (numberOfPoints - 1)
lst.Add(QuadBezierPoint(controlPoint1, controlPoint2, controlPoint3, t))
Next
Return lst.ToArray

End Function

Private Function QuadBezierPoint(p1 As PointF, p2 As PointF, p3 As PointF, t As Double)
As PointF

Dim q1 As PointF = LinearInterpolate(p1, p2, t)
Dim q2 As PointF = LinearInterpolate(p2, p3, t)
Return LinearInterpolate(q1, q2, t)

End Function

Private Function LinearInterpolate(p1 As PointF, p2 As PointF, t As Double)
As PointF

Dim x As Double = p1.X + (p2.X - p1.X) * t
Dim y As Double = p1.Y + (p2.Y - p1.Y) * t
Return New PointF(CSng(x), CSng(y))

End Function


Once you have the points, you can draw the curve however you like, for example using Graphics.DrawCurve or as straight lines. Even with just 5 points, it will look a lot more like a Bezier than drawing through just 3 points.






share|improve this answer

































    0














    Afterthought. The simplest possible answer to the original question is just to duplicate one of the available points. For example p2 as here:



        Dim curvedPoints As Point() = {pt1, pt2, pt2, pt3}
    '....
    'Draw Bezier Curve
    myGraphics.DrawBezier(myPen, curvedPoints)


    The result won't be the same as a quadratic Bezier, because the doubled point 'pulls' harder on the curve. But it will still be a Bezier curve.



    BB






    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53477481%2fim-stuck-on-drawing-a-bezier-curve-in-visual-basic%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Drawing a spline through three points doesn't produce the same result as drawing a cubic Bezier curve. A spline curve tends to have sharper bends (think of a wire coathanger).



      Actually the .Net framework does include quadratic Beziers: the QuadraticBezierSegment in WPF. With suitable references, it wouldn't be hard to make a function to use it in a System.Drawing context.



      But it's even easier to draw a quadratic Bezier curve using plain arithmetic. You start by choosing the number of points to plot - 5 points might be enough, or more if you want a smoother result. Here's a function (plus 2 supporting functions) which, given 3 control points and the number of points you want to plot, returns a list of PointFs representing the quadratic Bezier:



              Public Function QuadraticBezier(controlPoint1 As PointF, controlPoint2 As PointF, 
      controlPoint3 As PointF, numberOfPoints As Integer) As PointF()

      Dim lst As New List(Of PointF)
      For t As Double = 0 To 1 Step 1 / (numberOfPoints - 1)
      lst.Add(QuadBezierPoint(controlPoint1, controlPoint2, controlPoint3, t))
      Next
      Return lst.ToArray

      End Function

      Private Function QuadBezierPoint(p1 As PointF, p2 As PointF, p3 As PointF, t As Double)
      As PointF

      Dim q1 As PointF = LinearInterpolate(p1, p2, t)
      Dim q2 As PointF = LinearInterpolate(p2, p3, t)
      Return LinearInterpolate(q1, q2, t)

      End Function

      Private Function LinearInterpolate(p1 As PointF, p2 As PointF, t As Double)
      As PointF

      Dim x As Double = p1.X + (p2.X - p1.X) * t
      Dim y As Double = p1.Y + (p2.Y - p1.Y) * t
      Return New PointF(CSng(x), CSng(y))

      End Function


      Once you have the points, you can draw the curve however you like, for example using Graphics.DrawCurve or as straight lines. Even with just 5 points, it will look a lot more like a Bezier than drawing through just 3 points.






      share|improve this answer






























        0














        Drawing a spline through three points doesn't produce the same result as drawing a cubic Bezier curve. A spline curve tends to have sharper bends (think of a wire coathanger).



        Actually the .Net framework does include quadratic Beziers: the QuadraticBezierSegment in WPF. With suitable references, it wouldn't be hard to make a function to use it in a System.Drawing context.



        But it's even easier to draw a quadratic Bezier curve using plain arithmetic. You start by choosing the number of points to plot - 5 points might be enough, or more if you want a smoother result. Here's a function (plus 2 supporting functions) which, given 3 control points and the number of points you want to plot, returns a list of PointFs representing the quadratic Bezier:



                Public Function QuadraticBezier(controlPoint1 As PointF, controlPoint2 As PointF, 
        controlPoint3 As PointF, numberOfPoints As Integer) As PointF()

        Dim lst As New List(Of PointF)
        For t As Double = 0 To 1 Step 1 / (numberOfPoints - 1)
        lst.Add(QuadBezierPoint(controlPoint1, controlPoint2, controlPoint3, t))
        Next
        Return lst.ToArray

        End Function

        Private Function QuadBezierPoint(p1 As PointF, p2 As PointF, p3 As PointF, t As Double)
        As PointF

        Dim q1 As PointF = LinearInterpolate(p1, p2, t)
        Dim q2 As PointF = LinearInterpolate(p2, p3, t)
        Return LinearInterpolate(q1, q2, t)

        End Function

        Private Function LinearInterpolate(p1 As PointF, p2 As PointF, t As Double)
        As PointF

        Dim x As Double = p1.X + (p2.X - p1.X) * t
        Dim y As Double = p1.Y + (p2.Y - p1.Y) * t
        Return New PointF(CSng(x), CSng(y))

        End Function


        Once you have the points, you can draw the curve however you like, for example using Graphics.DrawCurve or as straight lines. Even with just 5 points, it will look a lot more like a Bezier than drawing through just 3 points.






        share|improve this answer




























          0












          0








          0







          Drawing a spline through three points doesn't produce the same result as drawing a cubic Bezier curve. A spline curve tends to have sharper bends (think of a wire coathanger).



          Actually the .Net framework does include quadratic Beziers: the QuadraticBezierSegment in WPF. With suitable references, it wouldn't be hard to make a function to use it in a System.Drawing context.



          But it's even easier to draw a quadratic Bezier curve using plain arithmetic. You start by choosing the number of points to plot - 5 points might be enough, or more if you want a smoother result. Here's a function (plus 2 supporting functions) which, given 3 control points and the number of points you want to plot, returns a list of PointFs representing the quadratic Bezier:



                  Public Function QuadraticBezier(controlPoint1 As PointF, controlPoint2 As PointF, 
          controlPoint3 As PointF, numberOfPoints As Integer) As PointF()

          Dim lst As New List(Of PointF)
          For t As Double = 0 To 1 Step 1 / (numberOfPoints - 1)
          lst.Add(QuadBezierPoint(controlPoint1, controlPoint2, controlPoint3, t))
          Next
          Return lst.ToArray

          End Function

          Private Function QuadBezierPoint(p1 As PointF, p2 As PointF, p3 As PointF, t As Double)
          As PointF

          Dim q1 As PointF = LinearInterpolate(p1, p2, t)
          Dim q2 As PointF = LinearInterpolate(p2, p3, t)
          Return LinearInterpolate(q1, q2, t)

          End Function

          Private Function LinearInterpolate(p1 As PointF, p2 As PointF, t As Double)
          As PointF

          Dim x As Double = p1.X + (p2.X - p1.X) * t
          Dim y As Double = p1.Y + (p2.Y - p1.Y) * t
          Return New PointF(CSng(x), CSng(y))

          End Function


          Once you have the points, you can draw the curve however you like, for example using Graphics.DrawCurve or as straight lines. Even with just 5 points, it will look a lot more like a Bezier than drawing through just 3 points.






          share|improve this answer















          Drawing a spline through three points doesn't produce the same result as drawing a cubic Bezier curve. A spline curve tends to have sharper bends (think of a wire coathanger).



          Actually the .Net framework does include quadratic Beziers: the QuadraticBezierSegment in WPF. With suitable references, it wouldn't be hard to make a function to use it in a System.Drawing context.



          But it's even easier to draw a quadratic Bezier curve using plain arithmetic. You start by choosing the number of points to plot - 5 points might be enough, or more if you want a smoother result. Here's a function (plus 2 supporting functions) which, given 3 control points and the number of points you want to plot, returns a list of PointFs representing the quadratic Bezier:



                  Public Function QuadraticBezier(controlPoint1 As PointF, controlPoint2 As PointF, 
          controlPoint3 As PointF, numberOfPoints As Integer) As PointF()

          Dim lst As New List(Of PointF)
          For t As Double = 0 To 1 Step 1 / (numberOfPoints - 1)
          lst.Add(QuadBezierPoint(controlPoint1, controlPoint2, controlPoint3, t))
          Next
          Return lst.ToArray

          End Function

          Private Function QuadBezierPoint(p1 As PointF, p2 As PointF, p3 As PointF, t As Double)
          As PointF

          Dim q1 As PointF = LinearInterpolate(p1, p2, t)
          Dim q2 As PointF = LinearInterpolate(p2, p3, t)
          Return LinearInterpolate(q1, q2, t)

          End Function

          Private Function LinearInterpolate(p1 As PointF, p2 As PointF, t As Double)
          As PointF

          Dim x As Double = p1.X + (p2.X - p1.X) * t
          Dim y As Double = p1.Y + (p2.Y - p1.Y) * t
          Return New PointF(CSng(x), CSng(y))

          End Function


          Once you have the points, you can draw the curve however you like, for example using Graphics.DrawCurve or as straight lines. Even with just 5 points, it will look a lot more like a Bezier than drawing through just 3 points.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 15 at 23:50









          LarsTech

          70.5k12109161




          70.5k12109161










          answered Jan 15 at 23:19









          user2738137user2738137

          262




          262

























              0














              Afterthought. The simplest possible answer to the original question is just to duplicate one of the available points. For example p2 as here:



                  Dim curvedPoints As Point() = {pt1, pt2, pt2, pt3}
              '....
              'Draw Bezier Curve
              myGraphics.DrawBezier(myPen, curvedPoints)


              The result won't be the same as a quadratic Bezier, because the doubled point 'pulls' harder on the curve. But it will still be a Bezier curve.



              BB






              share|improve this answer




























                0














                Afterthought. The simplest possible answer to the original question is just to duplicate one of the available points. For example p2 as here:



                    Dim curvedPoints As Point() = {pt1, pt2, pt2, pt3}
                '....
                'Draw Bezier Curve
                myGraphics.DrawBezier(myPen, curvedPoints)


                The result won't be the same as a quadratic Bezier, because the doubled point 'pulls' harder on the curve. But it will still be a Bezier curve.



                BB






                share|improve this answer


























                  0












                  0








                  0







                  Afterthought. The simplest possible answer to the original question is just to duplicate one of the available points. For example p2 as here:



                      Dim curvedPoints As Point() = {pt1, pt2, pt2, pt3}
                  '....
                  'Draw Bezier Curve
                  myGraphics.DrawBezier(myPen, curvedPoints)


                  The result won't be the same as a quadratic Bezier, because the doubled point 'pulls' harder on the curve. But it will still be a Bezier curve.



                  BB






                  share|improve this answer













                  Afterthought. The simplest possible answer to the original question is just to duplicate one of the available points. For example p2 as here:



                      Dim curvedPoints As Point() = {pt1, pt2, pt2, pt3}
                  '....
                  'Draw Bezier Curve
                  myGraphics.DrawBezier(myPen, curvedPoints)


                  The result won't be the same as a quadratic Bezier, because the doubled point 'pulls' harder on the curve. But it will still be a Bezier curve.



                  BB







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 17 at 17:09









                  user2738137user2738137

                  262




                  262






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53477481%2fim-stuck-on-drawing-a-bezier-curve-in-visual-basic%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Wiesbaden

                      Marschland

                      Dieringhausen