2 Replies - 12041 Views - Last Post: 29 March 2010 - 07:20 AM Rate Topic: -----

#1 Chidori-chan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 06-December 09

Pseudo Code for B Spline implementation

Posted 28 March 2010 - 06:33 PM

Hi all--

I've got a program where you click and add points to make a polygon using a linked list, and now I want it where by pressing a button it will take the points on the polygon and make it a cubic b spline curve. However I have no experience with the b spline curves and am having trouble with were to start. So I was wondering if you would be able to help me with the pseudo code for this, using C so that I can better understand what I'm trying to do. The program is attached and here is what I have come up with so far (which isn't much unfortunately):

Create a drawCurve function that traverses the linked list in order.

Take the first four points to draw the spline curve, then the next one over each time.

[insert how to do the math here (of which I am confused about)]

Use a state machine to implement. So pressing a button of my choice will change between the drawing polygon state and the draw curve state.


Thank you very much for your help.

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Pseudo Code for B Spline implementation

#2 Guest_gregoryH@work*


Reputation:

Re: Pseudo Code for B Spline implementation

Posted 28 March 2010 - 07:48 PM

Hi

Do you have any formula that does the transformation? That is the starting point.

From there, we can look at developing the code with you.
Was This Post Helpful? 0

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Pseudo Code for B Spline implementation

Posted 29 March 2010 - 07:20 AM

well I can give you the code to a qbasic program I wrote when I was in high school... I have several versions of Bézier curves though I don't think I ever did the B-Spline itself though the B-Spline is just a generalization of the Bézier curve (I always called them Bézier loops -- though I pronounced that "bizarre curve" and "bizarre loop").

Here is the first Bézier curve program I ever did.
DECLARE FUNCTION bernstine! (i%, u AS SINGLE)
TYPE Tpoint2D
    x AS SINGLE
    y AS SINGLE
END TYPE
DEFINT A-Z
DIM ctrl(3) AS Tpoint2D
DIM pt AS Tpoint2D, oldpt AS Tpoint2D
ctrl(0).x = 0
ctrl(0).y = 200
ctrl(1).x = 240
ctrl(1).y = 100
ctrl(2).x = 320
ctrl(2).y = 400
ctrl(3).x = 640
ctrl(3).y = 240

SCREEN 12
'WINDOW (-3, -3)-(3, 3)
stp = 20

FOR i = 0 TO 3
    IF i = 0 THEN PSET (ctrl(i).x, ctrl(i).y), 14 ELSE LINE -(ctrl(i).x, ctrl(i).y), 14
    CIRCLE (ctrl(i).x, ctrl(i).y), 2, 14
NEXT i
FOR t = 0 TO stp
    pt.x = 0: pt.y = 0: u! = t / stp
    FOR i = 0 TO 3
        b! = bernstine(i, u!)
        pt.x = pt.x + ctrl(i).x * b!
        pt.y = pt.y + ctrl(i).y * b!
    NEXT i
    IF t = 0 THEN oldpt = pt
    LINE (oldpt.x, oldpt.y)-(pt.x, pt.y), 15
    CIRCLE (pt.x, pt.y), 2, 4
    oldpt = pt
NEXT t
SLEEP

FUNCTION bernstine! (i, u AS SINGLE)
    SELECT CASE i
        CASE 0
            bernstine! = (1 - u) ^ 3
        CASE 1
            bernstine! = 3 * u * (1 - u) ^ 2
        CASE 2
            bernstine! = 3 * u ^ 2 * (1 - u)
        CASE ELSE
            bernstine! = u ^ 3
END SELECT
END FUNCTION


The key is really the "bernstine" function. The function takes two arguments, the first one i determines which bernstine function to return the result of, 0-4. The next argument u is a value between 0 and 1, the result is a "weight" to give each control point.

you can find a description of the math here.

of course this was just my first program where I was trying to understand how things worked. I have much more sophisticated and optimized versions from later on, but they are quite a jumble of mathematics and not quite to easy to see.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1