0

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));
  • What is the actual problem? what are you getting and how is it wrong? – RustyH Jan 25 '14 at 00:03
  • I have points less than zero, so i must divide my window into 4 parts. – user3233915 Jan 25 '14 at 00:06
  • I still really don't understand the question but if you have points that for whatever reason are less than zero if they only go negative down to half the screen size (im guessing that since you said 4 parts) you could technically add half the screen width to x and half the screen height to Y. Still really don't understand the question or why you would have negative numbers to begin with – RustyH Jan 25 '14 at 00:08
  • Sorry for this comment above, im new here.The problem is I get for example point (-1,4) at the place of (-8,7). I need appropriate scaling functions and declaring variables as xmax, xmin etc. I have to remember, that to every value of x i must add 400 pixels, cos i divide it into 4 parts, and to every y +300 pixels. But i dont know where to add them :c Why I have negative points? I have txt file with two columns and n points, and coordinates of these points can be less or greater then zero – user3233915 Jan 25 '14 at 00:13

1 Answers1

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...

RustyH
  • 473
  • 7
  • 22
  • I've changed it like that, but look at my declaration of scaling, it gives for example a value equal 40, and then I mutliply xpn and ypn by scale. In xpn I have sth + 400, if I multiply it by 40 it is too much for my window.. When it comes to scaling, I just want to adjust max and min values of x and y to the size of my window, hence i divide height and width by sth ;) – user3233915 Jan 25 '14 at 00:40
  • I have no problem with displaying points with positive cooridinates. The problem appears when it comes to display points with positive and negative coordinates.. – user3233915 Jan 25 '14 at 01:17
  • I did it! I just some pixels in putpixel function, before i had divided parameters of the window (height and width) by 4 (so for scaling window is 200x150);) But in putpixel function i put (xpoint+400), (300-ypoint) :) problem solved :D im so proud of myself :d – user3233915 Jan 25 '14 at 01:46