Solved

GDI Draw curve

Posted on 2004-09-02
18
733 Views
Last Modified: 2008-01-09
Hi experts,

I have an Array that contains x and y coordinates. I need to draw a curve
through those coordinates. But I have no idea how to do that , I tried using bezier curves but that didn't seem to work.

I'm a vb.net newbe so be gentle

K.
0
Comment
Question by:Strategus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 4
18 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11965261
Did you look at the DrawArc method?

Bob
0
 

Author Comment

by:Strategus
ID: 11965323
Yes I did, but how do I determine the arc for the rest of the coordinates because they all depend upon each other
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11965376
I'm sorry (senior moment).  Look at the Graphics.DrawCurve method--one of the overloaded methods takes an array of points:

Overloads Public Sub DrawCurve(Pen, Point())

Bob
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

Author Comment

by:Strategus
ID: 11965463
I'll give it a try, thanks TheLearnedOne.
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11965488
Hi Strategus, do you want to draw a S-Curve, if so drawCurve won't do the trick because it is a cardinal spline,
for an s -curve you need a cubic spline.
0
 

Author Comment

by:Strategus
ID: 11965519
Ummm, I have no idea what you mean RonaldBiemans
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11965576
Do you want the curve to look smooth or chunky?

Bob
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11965594
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11965615
TheLearnedOne said it (Smooth or Chuncky)
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11965624
I like my peanut butter chunky, but I like my curves smooth :)

Bob
0
 

Author Comment

by:Strategus
ID: 11965639
Ok, Smooth then because the drawcurve didn't give me the required result. Well it gave Me the required result but not for the one I'm making it for :-)
0
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 500 total points
ID: 11965671
Ok, try this, I needed something simular a while back and this worked (I'm drawing on a panel here)

 Dim x(4) As Single
    Dim y(4) As Single
    Dim y2() As Single
    Dim yout As Single
    Dim ron As Graphics

    Private Sub spline(ByVal x() As Single, ByVal y() As Single, ByVal n As Integer, ByVal yp1 As Single, ByVal ypn As Single)
        Dim i, k As Integer
        Dim p, qn, sig, un, u() As Single

        ReDim y2(n)
        ReDim u(n)

        If yp1 > 9.9E+29 Then
            y2(0) = u(0) = 0.0
        Else
            y2(0) = -0.5
            u(0) = (3.0 / (x(1) - x(0))) * ((y(1) - y(0)) / (x(1) - x(0)) - yp1)
        End If

        For i = 1 To n - 1
            sig = (x(i) - x(i - 1)) / (x(i + 1) - x(i - 1))
            p = sig * y2(i - 1) + 2
            y2(i) = (sig - 1) / p
            u(i) = (y(i + 1) - y(i)) / (x(i + 1) - x(i)) - (y(i) - y(i - 1)) / (x(i) - x(i - 1))
            u(i) = (6 * u(i) / (x(i + 1) - x(i - 1)) - sig * u(i - 1)) / p
        Next i

        If ypn > 9.9E+29 Then
            qn = un = 0.0
        Else
            qn = 0.5
            un = (3.0 / (x(n) - x(n - 1))) * (ypn - (y(n) - y(n - 1)) / (x(n) - x(n - 1)))
        End If

        y2(n) = (un - qn * u(n - 1)) / (qn * y2(n - 1) + 1)
        For k = n - 1 To k >= 1 Step -1
            y2(k) = y2(k) * y2(k + 1) + u(k)

        Next


    End Sub

    Private Function splint(ByVal xa() As Single, ByVal ya() As Single, ByVal y2a() As Single, ByVal n As Integer, ByVal x As Single)
        Dim klo, khi, k As Integer
        Dim h, b, a As Single

        klo = 0
        khi = n
        Do While khi - klo > 1
            k = khi + klo >> 1

            If xa(k) > x Then
                khi = k
            Else
                klo = k
            End If
        Loop
        h = xa(khi) - xa(klo)
        a = (xa(khi) - x) / h
        b = (x - xa(klo)) / h
        yout = a * ya(klo) + b * ya(khi) + ((a ^ 3 - a) * y2a(klo) + (b ^ 3 - b) * y2a(khi)) * (h ^ 2) / 6

        Return yout
    End Function

    Private Sub spline_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        x(0) = 100
        x(1) = 200
        x(2) = 300
        x(3) = 400
        x(4) = 500

        y(0) = 300
        y(1) = 330
        y(2) = 200
        y(3) = 300
        y(4) = 200

        spline(x, y, 4, 0, 0)

    End Sub

    Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
        Dim Xrange As Single
        Dim Yrange As Single

        Xrange = Panel1.Width / (x(4) + 100)
        Yrange = Panel1.Height / (y(1) + 100)

        ron = Panel1.CreateGraphics
        For br As Single = x(0) To 500 Step 0.1
            ron.DrawRectangle(New Pen(Color.Black), br * Xrange, splint(x, y, y2, 4, br) * Yrange, 1, 1)
            For bf As Integer = 0 To x.GetUpperBound(0)
                ron.FillEllipse(New System.Drawing.SolidBrush(Color.Black), x(bf) * Xrange, y(bf) * Yrange, 5, 5)
            Next
        Next
    End Sub
0
 

Author Comment

by:Strategus
ID: 11965718
GULP, I though I asked to be gentle :-).

Can I just cut and paste this ? Because I don't think I have the brainpower to figure this out.

Do you have any friends ? ;-)
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11965768
Yes, you can cut and paste this, you just need a panel on your form (panel1)

and you ofcourse you need to adjust the x and y declarations to the number of point you have

Dim x(4) As Single
Dim y(4) As Single

and fill them with your own coordinates.

Want do you mean if I have any friends
0
 

Author Comment

by:Strategus
ID: 11965802
Ok I'll give it a try.

And what I meant was, if you can figure out these things you won't have much spare time or your brilliant . ;-)
0
 

Author Comment

by:Strategus
ID: 11965923
I don't understand it, but it works.

Thanks RonaldBiemans, you are truely an expert.

And thanks to TheLearnedOne too.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11966123
You know, if you really want to thank, then you can think about splitting points.  I am working stiff, too:)

Bob
0
 

Author Comment

by:Strategus
ID: 11970616
Sorry TheLearnedOne, this was my first question here on EE and I'm not quite familiar with the way things work here. I will do better next time :)
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question