GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 18-Mar-2004, 10:45
warny_maelstrom warny_maelstrom is offline
Junior Member
 
Join Date: Jan 2004
Posts: 41
warny_maelstrom will become famous soon enough

Problems with different variables returning the same value


Im trying to create a program to display a curved line which is infact lots of smaller straight lines. The user inputs the corners of a polygon and the "weighting" of each of these. The program then passes the data through a formula and gives us first x,x01, and y,y01, coordinates after which a control variable is changed and the second x,x02, and y,y02, coordinates are returned giving the coordinates for a single line. The problem I go to display the line the values for x01,y01 and x02,y02 are the same giving me just a single dot. When I run the program without the graphics, and i printf after each "bit" of math the x01,x02,y01,y02 ar different but when i put the two programs together I get the same values and I cant see why. I would appreciate any help on this problem.

CPP / C++ / C Code:
#pragma windows 300000,500000,"cwp_ico.rc" //means dosbox is not displayed
#include<stdio.h>
#include<math.h> 
#include<clib.h>
#include <windows.h>
#include <dbos\graphics.h> 

int main(void) 
{   
    float u=0, g, p, s, t, x01=0, y01=0, x02=0, y02=0;
    int x0=1, x1=1, x2=1, x3=1, y0=1, y1=1, y2=1, y3=1, w0=1, w1=1, w2=1, w3=1, np=1, hand1=1, ctrl; 


        winio("%bg%ca[Data Input Screen]&",RGB(100,255,100)); 
        winio("\tEnter in 4 values for the x coordinates: \t%rd\t%rd\t%rd\t%rd\n&", &x0, &x1, &x2, &x3); 
        winio("\tEnter in 4 values for the y coordinates: \t%rd\t%rd\t%rd\t%rd\n&",&y0, &y1, &y2, &y3); 
        winio("\tEnter in 4 values for the weighting points: \t%rd\t%rd\t%rd\t%rd\n&",&w0, &w1, &w2, &w3);         
        winio("\tEnter the number of points you wish to be on the line:\t\t\t\t\t\t%rd\n&",&np);
        winio("%bc[red]%bt[Draw]\n");//button to close the window so the next appears 
        {       
        
                u=1/np;//means the correct number of lines will be created
                for( int c=0 ; c<np ; c++) 
                {
                        p = (pow(1 - u, 3));//=(1-u)(1-u)(1-u),to the power of
                        g = (3 * (pow(1 - u, 2) * u));
                        s = (3 * pow((1 - u) * u, 2));
                        t = pow(u, 3);

                        x01=((p*w0*x0)+(g*w1*x1)+(s*w2*x2)+(t*w3*x3))/((p*w0)+(g*w1)
                        +(s*w2)+(t*w3));// starting x coordinate for the line
                     
                        y01=((p*w0*y0)+(g*w1*y1)+(s*w2*y2)+(t*w3*y3))/((p*w0)+(g*w1)
                        +(s*w2)+(t*w3));// starting y coordinate for the line

                        u=u+(1/np);
                        
                        p = (pow(1 - u, 3));//=(1-u)(1-u)(1-u),to the power of
                        g = (3 * (pow(1 - u, 2) * u));
                        s = (3 * pow((1 - u) * u, 2));
                        t = pow(u, 3);
                         
                        x02=((p*w0*x0)+(g*w1*x1)+(s*w2*x2)+(t*w3*x3))/((p*w0)+(g*w1)
                        +(s*w2)+(t*w3));// ending x coordinate for the line
                        y02 = ((p*w0*y0)+(g*w1*y1)+(s*w2*y2)+(t*w3*y3))/((p*w0)+(g*w1)
                        +(s*w2)+(t*w3));// ending x coordinate for the line
 
                        winio("%bg%ca[Graphics Tablet]&",RGB(0,0,0));
                        winio("%`gr&",400,300,hand1);//graphics tablet of size 400*300,reference name hand1
                        winio("%ww%lw",&ctrl);//this must b present with the line above
                        select_graphics_object(hand1);//makes sure the next lines refer to the tablet hand1
                        set_line_width(5);
                        draw_line(x01,y01,x02,y02,80);
                }         
        }        

}
int Draw()
        {
        return 0;//clears screen so graphics tablet can be displayed 
        }
  #2  
Old 20-Mar-2004, 05:23
warny_maelstrom warny_maelstrom is offline
Junior Member
 
Join Date: Jan 2004
Posts: 41
warny_maelstrom will become famous soon enough
After playing around with the program and trying to rearrange pretty much everything to get it to work Iv found out that for some strange reason if i put,for instance(spelling), np=4 so now it would go through the loop 4 times and give me 4 graphics windows. The thing is every single graphics window is exactly the same, with just a single dot at(im guessing) 10,200 which is the first values i put in for x,y, and maybe the first value returned for x01,y01 but i cant b sure. I have no idea why the program would return me the values for x01,x02 equal to x0 that i input, when i have calculations which i know change these values.
  #3  
Old 20-Mar-2004, 08:37
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
You declare np as an int, so the expression 1/np has value of 1 when np = 1, and has a value of 0 when np > 1.

Try 1.0/np instead of 1/np.



Dave
  #4  
Old 20-Mar-2004, 09:49
warny_maelstrom warny_maelstrom is offline
Junior Member
 
Join Date: Jan 2004
Posts: 41
warny_maelstrom will become famous soon enough
It doesnt make any difference sadly, i thiNk whats wrong is its passing the values for x01 and y01 when u=0 which is the only value which would return the results i get. But i dont understand why it would pass this result np times when the value should change each time it goes through the loop.
  #5  
Old 20-Mar-2004, 13:05
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
There is no way that I can test your program.

Here's a suggestion: put printf() statements after each calculation. For example:

CPP / C++ / C Code:
printf(" np = %d, c = %d u = %f\n p = %f, g = %f, s = %f, t = %f\n", 
                                np, c, u, p, g, s, t);

Look at what happens when you use
CPP / C++ / C Code:
pow(u, 3)
when u = 0


Of course, if you do the calculations correctly you will never have u = 0, but look at what happens when you use

CPP / C++ / C Code:
pow(1-u, 3)
when u = 1

When you have the correct values for all calculated values, then you can debug your display routines.


printf() is your friend.


Good luck!

Dave
  #6  
Old 20-Mar-2004, 16:06
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Here's a suggestion: put printf() statements after each calculation. For example:

CPP / C++ / C Code:
printf(" np = %d, c = %d u = %f\n p = %f, g = %f, s = %f, t = %f\n", 
                                np, c, u, p, g, s, t);

Dave

Of course, I didn't mean that you should put printf() statements in your program as it stands.

I commented out the windows stuff and made a console program that uses your calculations and displays the results of all calculations with printf() statements. The idea is that I think it's vital that you convince yourself that the formulas in your program give expected results without having to worry about whether "the results are OK, but the display is bad," or whatever.

Dave
Last edited by davekw7x : 20-Mar-2004 at 17:12.
  #7  
Old 20-Mar-2004, 20:03
warny_maelstrom warny_maelstrom is offline
Junior Member
 
Join Date: Jan 2004
Posts: 41
warny_maelstrom will become famous soon enough
My original program doesnt include windows.h just because it not my favourite thing to work with an djust diving straight in is never a good idea. I tested the return of the x01,x02,y01,y02 values compared to the u values and they seemed to work fine but i never thought to check the rest of the values. This is my program as it was:
CPP / C++ / C Code:
 
#include<stdio.h>
#include<math.h> 
#include<clib.h>

int main(void) 
{
    float u=0, g, p, s, t, x01, y01, x02, y02, q, np;
    int x0, x1, x2, x3, y0, y1, y2, y3, w0, w1, w2, w3; 
    char cha;

    printf("Enter in r values for the x coordinates e.g. x0,x1,x2,x3\n");
    scanf("%d,%d,%d,%d", &x0, &x1, &x2, &x3);
    printf("Enter in r values for the y coordinates e.g. y0,y1,y2,y3\n");
    scanf("%d,%d,%d,%d", &y0, &y1, &y2, &y3);
    printf("Enter in w values in the format w0,w1,w2,w3\n");
    scanf("%d,%d,%d,%d", &w0, &w1, &w2, &w3);
    printf("Enter the number of points you wish to be on the line:\n");
    scanf("%f", &np);
    system("cls");
    u=1.0/np;  
    
    for( int c=0 ; c<np ; c++)
        {
                      
        p = (pow(1 - u, 3));//=(1-u)(1-u)(1-u),to the power of
        g = (3 * (pow(1 - u, 2) * u));
        s = (3 * pow((1 - u) * u, 2));
        t = pow(u, 3);

        x01 = ((p * w0 * x0) + (g * w1 * x1) + (s * w2 * x2) + (t * w3 * x3)) / ((p * w0) + (g * w1) + (s * w2) + (t * w3));// starting x coordinate for the line                       
        y01 = ((p * w0 * y0) + (g * w1 * y1) + (s * w2 * y2) + (t * w3 * y3)) / ((p * w0) + (g * w1) + (s * w2) + (t * w3));// starting y coordinate for the line
        printf("\nFor u = %f:   ", u);
        printf("\nx01  =  %f", x01);
        printf("\ty01  =  %f", y01); 
        u=u+(1/np);
                        
        p = (pow(1 - u, 3));//=(1-u)(1-u)(1-u),to the power of
        g = (3 * (pow(1 - u, 2) * u));
        s = (3 * pow((1 - u) * u, 2));
        t = pow(u, 3);
                         
        x02 = ((p * w0 * x0) + (g * w1 * x1) + (s * w2 * x2) + (t * w3 * x3)) / ((p * w0) + (g * w1) + (s * w2) + (t * w3));// ending x coordinate for the line
        y02 = ((p * w0 * y0) + (g * w1 * y1) + (s * w2 * y2) + (t * w3 * y3)) / ((p * w0) + (g * w1) + (s * w2) + (t * w3));// ending x coordinate for the line    
        printf("\nx02  =  %f", x02);
        printf("\ty02  =  %f", y02);                        
        
        
        
        q=c%12;//diplays 12 results at a time 
        
        if(q==0&&c!=0)
               {
               while(cha==13);//displays next 12 results when "enter" is pressed
                        {
                        getch();
                        }
                }
        }
}

I put in the checker to see if the values for np...etc. were correct and found that after a while of calculator abuse they seem all to be correct which really leaves me lost as to why the winio program doesnt return the values that I expect and that are returned perfectly when I pass them without the winio code.
  #8  
Old 20-Mar-2004, 20:42
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by warny_maelstrom
I put in the checker to see if the values for np...etc. were correct and found that after a while of calculator abuse they seem all to be correct which really leaves me lost as to why the winio program doesnt return the values that I expect and that are returned perfectly when I pass them without the winio code.

Can't help you with winio.

You still have an expression u=u+1/np. I can't believe that's right, but if you are satisfied with your calculated values, I'll shut up.

Regards,

Dave
  #9  
Old 20-Mar-2004, 21:19
warny_maelstrom warny_maelstrom is offline
Junior Member
 
Join Date: Jan 2004
Posts: 41
warny_maelstrom will become famous soon enough
Quote:
Originally Posted by davekw7x
Can't help you with winio.

You still have an expression u=u+1/np. I can't believe that's right, but if you are satisfied with your calculated values, I'll shut up.
This is my old program which works fine except for a few warnings, but I can live with that seeing as Im not using it any more, Iv changed the u=u+(1/np) in my winio program but that ones just plain not working. Thanks for trying to help none the less.
  #10  
Old 25-Mar-2004, 15:37
warny_maelstrom warny_maelstrom is offline
Junior Member
 
Join Date: Jan 2004
Posts: 41
warny_maelstrom will become famous soon enough
Quote:
Originally Posted by davekw7x
You declare np as an int, so the expression 1/np has value of 1 when np = 1, and has a value of 0 when np > 1.
Try 1.0/np instead of 1/np.

This was indeed the right answer, I thought this was not the case to start with seeing as winio wouldnt support an int and my first program operated fine, I didnt realise but you can switch an int into a float by sticking another variable of type float in:
CPP / C++ / C Code:
int num;
float np;
np=(float)num;//converts num into a float 
Sorry I ignored this suggestion to start with.

Iv nearly completely finished the problem all i need now is to create a function to find the maximum value of 4 given values,x1,x2,x3,x4, and i was wondering if there was anyway of doing this more easily than constructing multiple if statements? As the code i have at the moment is full of these already. Just in case anyone is slightly interested this is my code so far:
[c]
#pragma windows 300000,500000,"cwp_ico.rc" //means dosbox is not displayed
#include<stdio.h>
#include<math.h>
#include<clib.h>
#include<windows.h>
#include<dbos\graphics.h>

float u=0, g, p, s, t, x0=0, y0=0, np=1;
int b4=0, b3=0, b2=0, b1=1, r1=0, r2=0, r3=1, x1=1, x2=1, x3=1, x4=1, y1=1, y2=1, y3=1, y4=1, w1=1, w2=1, w3=1, w4=1,
hand=0, ctrl, c=0, num=2, col=0, wid=2, Draw();
short int x[100],y[100];//array with a maximum of 100 values
int main(void)
{
{
winio("%bg%ca[Data Input Screen]&",RGB(100,255,100));
winio("%fn[Arial]%tsEnter in 4 values for the x coordinates: \t%rd\t%
rd\t%rd\t%rd\n&",1.2, &x1, &x2, &x3, &x4);
winio("Enter in 4 values for the y coordinates: \t%rd\t%rd\t%rd\t%
rd\n&",&y1, &y2, &y3, &y4);
winio("Enter in 4 values for the weighting: \t%rd\t%rd\t%rd\t%
rd\n&",&w1, &w2, &w3, &w4);
winio("Enter in the number of points to be on the line:\t\t\t\t\t\t%
rd\n&",&num);
winio("Please choose a colour:\t\t\t%4`ga%rb[Red]\t\t%rb[Blue]\t\t%rb
[Green]\t%rb[Purple]\n&",&b1,&b2,&b3,&b4,&b1,&b2,&b3,&b4);
//4 radial buttons return boolean answers only one can be "on"
winio("Please select a line thickness:\t\t%3`ga%`rb[ Thin ]\t\t\t%`rb
[Medium]\t\t%`rb[Thick ]&",&r1,&r2,&r3,&r1,&r2,&r3);
//boolean answers only one can return "1"
winio("\n\t\t %bc[red]%^bt[Draw]",Draw);

np=(float)num;//converts num into a float
{
do
{
p = (pow(1 - u, 3));//=(1-u)(1-u)(1-u),to the power of
g = (3 * (pow(1 - u, 2) * u));
s = (3 * pow((1 - u) * u, 2));
t = pow(u, 3);

x0=((p*w1*x1)+(g*w2*x2)+(s*w3*x3)+(t*w4*x4))/
((p*w1)+(g*w2)+(s*w3)+(t*w4));
//starting x coordinate for a line
y0=((p*w1*y1)+(g*w2*y2)+(s*w3*y3)+(t*w4*y4))/
((p*w1)+(g*w2)+(s*w3)+(t*w4));
//starting y coordinate for a line

u=u+(1/(np-1));
x[c]=x0; y
CPP / C++ / C Code:
=y0;
                        //given number on array,c, is equal to that of x01,y01
                        c++;//increment c so we get np points
                        }
                while(c<np);        
                winio("%bg%ca[Graphics Tablet]&",RGB(0,0,0));
                winio("%`gr&",400,400,hand);
                //graphics tablet of size 400*300,reference name hand1
                winio("%lw",&ctrl);//this must b present with the line above
                select_graphics_object(hand);
                //makes sure the next lines refer to the tablet hand1
                set_line_width(wid);
                polyline(x,y,np,col);
                //displays a polygon of np points, colour=col, x,y taken from array
                set_line_width(1);
                draw_line(x1,y1,x2,y2,29);//displays control polygon 
                draw_line(x2,y2,x3,y3,29);// 
                draw_line(x3,y3,x4,y4,29);//
                draw_text("Control Polygon",x2-10,y2-30,29);  
                } 
        }                
                
}  

int Draw()
        {         
        if (b1==0)//a function to determine the colour selected by the user
                {
                if(b2==0)
                        {
                        if(b3==0)
                                {
                                col=80;
                                }
                         else
                                {
                                col=48;
                                } 
                         }       
                else
                        {
                        col=53;
                        }        
                }
        else 
                {       
                col=40;
                } 
        if (r1==0)//line thickness selected by the user
                {
                if(r2==0)
                        {
                        wid=5;
                        }
                else
                        {
                        wid=3;
                        }
                }
        else
                {
                wid=1;
                }
                        
        return 0;//refreshes screen
        }

So just to reiterate and make my problem clearer. I have 4 values ,x1,x2,x3,x4 and i need to find out the maximum and minimum values so that i can scale my graphical box accordingly but Id prefer not to use a large number of if statements and was wondering if there was any other way of doing this.
Again sry Dave, i would have done better to state why I ignored your suggestion.
 
 

Recent GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
passing variables out of an iframe by url JUNK KED MySQL / PHP Forum 5 31-Jul-2007 10:33
GeForce FX 5600 problems Peacemaker Computer Hardware Forum 24 18-Jul-2004 06:50
Chaintech Geforce 5600 FX problems bartster74 Computer Hardware Forum 8 04-May-2004 14:16
Apache on Windows XP and passing variables Jos Elkink Apache Web Server Forum 4 21-Nov-2003 03:21

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:37.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.