beziers and arrays

This is a discussion on beziers and arrays within the C Programming forums, part of the General Programming Boards category; well, it seems i am not very good with either. I am having to write a program to draw a ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    6

    beziers and arrays

    well, it seems i am not very good with either. I am having to write a program to draw a bezier curve in a graphics area. i have the graphics area sorted, all the input boxes sorted and the correct variables assigned to each box. However the whole thing falls down when i reach the draw statement that is assigned to a button. After various atempts, this is the 'best'. No errors when compiling, no error messages while running, the program just crashes as soon as i press 'draw' as if the program is not leaving the 'do' loop. also, im not convinced that im getting the program to write the answers into the arrays correctly. it's probably a silly error, but if anyone can help i'll be very gratefull.

    Code:
    int x[100], y[100];
    
    int draw()
            {
             x[0]= x0;
             y[0]= y0;
             
             
             c=1;
             u=1/100;
             gap=u;
    
             do
             {
                    b0=pow((1-u),3);
                    b1=3*pow((1-u),2)*u;
                    b2=3*(1-u)*pow(u,2);
                    b3=pow(u,3);
             
            
                           x[c]=((b0*w0*x0)+(b1*w1*x1)+(b2*w2*x2)+(b3*w3*x3))/((b0*w0)+(b1*w1)+(b2*w2)+(b3*w3));
             
                    y[c]=((b0*w0*y0)+(b1*w1*y1)+(b2*w2*y2)+(b3*w3*y3))/((b0*w0)+(b1*w1)+(b2*w2)+(b3*w3));
                    
                    c+1;
                    u=u+gap;
             }
             while (u<1);        
                      
            draw_bezier (x,y,100,RED);   
            return 1;
            }
    Last edited by simdel1; 04-28-2004 at 11:26 AM.

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Code:
             x[0]= x0; /*where is x0 defined*/
             y[0]= y0; /*where is y0 defined*/
             
             
             c=1;        /*are you implicitly defining c?*/
             u=1/100;/*here too*/
             gap=u;   /*this also???/*
    
             do
             {
                    b0=pow((1-u),3);
                    b1=3*pow((1-u),2)*u;
                    b2=3*(1-u)*pow(u,2);
                    b3=pow(u,3);
    it crashes because you are writing memory you do not own(unless all those are global variables) You should really add -Wall -pedatic if on gcc and increase setting if your on something different like an IDE.
    P.S. If you have all those defined pow returns double. the variable u must be at least a float and it would be 1.0/100.0

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    6
    well, just to make sure theres no confusion as to exactly what i've done ill post the whole code. i noticed one little mistake (c+1 in the do loop was changed to c=c+1) and now i get a very nice error message.

    The instruction at address 004011e9 attempted to write to location 00403000
    the full code:

    Code:
    #pragma windows 500000,500000
    #include <stdio.h>
    #include <math.h>
    #include<dbos\graphics.h>   
    #include<windows.h>
    
    float gap, u;
    int w0, w1, w2, w3, b0, b1, b2, b3, x0, x1, x2, x3, y0, y1, y2, y3, c;
    
    int a=300,b=300;
    int x[100], y[100];
    
    int draw()
            {
             x[0]= x0;
             y[0]= y0;
             
             
             c=1;
             u=1/100;
             gap=u;
             do
             {
                    b0=pow((1-u),3);
                    b1=3*pow((1-u),2)*u;
                    b2=3*(1-u)*pow(u,2);
                    b3=pow(u,3);
             
            
                    x[c]=((b0*w0*x0)+(b1*w1*x1)+(b2*w2*x2)+(b3*w3*x3))/((b0*w0)+(b1*w1)+(b2*w2)+(b3*w3));
             
                    y[c]=((b0*w0*y0)+(b1*w1*y1)+(b2*w2*y2)+(b3*w3*y3))/((b0*w0)+(b1*w1)+(b2*w2)+(b3*w3));
                    
                    c=c+1;
                    u=u+gap;
             }
             while (u<=1);        
                      
            draw_bezier (x,y,100,RED);   
            return 1;
            }
    
    int main()
    {
    
    int hand;
    
    winio("%gr[grey]&", a, b);
    winio("%lw&",&hand);
    winio("x0: %rd\t&",&x0);
    winio("x1: %rd\t&",&x1);
    winio("x2: %rd\t&",&x2);
    winio("x3: %rd\n&",&x3);
    winio("y0: %rd\t&",&y0);
    winio("y1: %rd\t&",&y1);
    winio("y2: %rd\t&",&y2);
    winio("y3: %rd\n&",&y3);
    winio("w0: %rd\t&",&w0);
    winio("w1: %rd\t&",&w1);
    winio("w2: %rd\t&",&w2);
    winio("w3: %rd\n&",&w3);
    winio("%bc[red]%`^bt[Draw]\t",draw); 
    
    }

  4. #4
    Registered User penney's Avatar
    Join Date
    Jan 2003
    Posts
    47
    One thing you should do and this is probably causing the error. Check your index variable 'c' prior to storing to the x and y arrays and make sure it's between 0 and 99. Like so:

    if( c >=0 && c <= 99 ) /* store to x and y */
    {
    x[c] = /*your formula*/;
    y[c] = /*your formula*/;
    }

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    6
    hmm, it has got rid of the error message, but now its just freezing out like before.

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    6
    ok, after a good few hours of debuging i think i have found the source of the problem...but i cant see WHY it is a problem.

    it seems as though the entire thing is woking (with some fudging to remove some bizare variable value swapping that was going on ) except this line:

    Code:
    u=(1/100);
    it should obviously produce a value of 0.01 for u, however it is actually producing 0.0000. u is definied as a float, so i thought it would be ok. have i done something very silly, or is there another way of getting 0.01. it does not seem to like me simply putting the value into the code either. u=0.01; produces an error message.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    1/100 is done using integer arithmetic, which produces 0
    This is converted to 0.0 by the assignment

    Try
    1.0 / 100;

    > produces an error message.
    Nice for you - but tells us nothing
    Paste it

    Perhaps something about double to float truncation?
    If so, then use
    u = 0.01f;

    Is there a good reason why you're using float instead of double?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    6
    Thanks Salem, changing 1 to 1.0 did the trick. Now its just a case of weeding out the few perculiarities and i'll be done.

    Theres no reason why im using floats other than i didnt realise that i should be using double. why is a double superior in this situation?

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    doubles have both a greater range and a greater precision. All in all, a better bet for numerical work.

    Also, when you write 1.0, it is a double floating point constant, not a float floating point constant. To get a true float constant, you need the 'f' suffix, as in 1.0f
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    6
    ahh, ok then. thanks

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21