简体   繁体   中英

How to declare values of x and y to display it correctly (scaling also)?

Im writing a program in C (using allegro lib) which display points (i could say some sequence) from txt file(two columns X & Y). These are floating points and they can be less than zero. Size of a window is 800x600. As we know values of x increase from left to right, but y ones increase from up to down. I have a big problem with declaring variables and scaling then. Here is a piece of code I have a problem with:

Array col1 consists of X values, col2 consists of Y values. I sort them with qsort and then get min and max. Then i declare height and width of the window and the scale..

float x_max, x_min, y_max, y_min;
        x_max=(col1[(n-1)]);
        x_min=(col1[0]);
        y_max=(col2[(n-1)]);
        y_min=(col2[0]);

        int height, width;
        height=600;
        width=800;

        float x_scale, y_scale;
        y_scale = height/(fabsf(y_max)+ fabsf(y_min));
        x_scale = width/(fabsf(x_max)+ fabsf(x_min));

Now i reopen txt file and create arrays not sorted arrays col1 and col2. Now i declare xpn and ypn to get the value of x or y from the array, then i mutliply it by scale and eventually i want to display it, but.. not all are displayed and they are they all are displayed wrong. I use abs and fabsf, its bad I know..

Could you help me with this code, declarationa of values of x and y and scaling them? I've tried almost everything and it doesnt work... Thx in advance :)

float xpn, ypn;
int xpoint, ypoint;

hline( screen, 5, 300, 795, makecol( 0, 0, 0 ) );
vline( screen, 400, 5, 595, makecol( 0, 0, 0 ) );

        for(i=0;i<n;i++)
        {
        float xpn, ypn;
        xpn=fabsf(col1[i]);
        ypn=fabsf(col2[i]);

        int xpoint, ypoint;
        xpoint=abs(xpn*x_scale);
        ypoint=abs(ypn*y_scale);

putpixel(screen,xpoint,ypoint,makecol(0,0,0));

Sounds like from your last comment you just need to add +300 and +400?

xpn=fabsf(col1[i]);
ypn=fabsf(col2[i]);

change to

xpn=col1[i]+400;
ypn=col2[i]+300;

and you would not need to get the abs value that way it should be positive.

for the y values being swapped from were you want them try

ypn=-col2[i]+300;

this will switch the negative to positive and vise versa

not exactly sure about the scaling.. not sure how you are trying to scale...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM