Reputation: 1892
I have legacy code which use cairo library to draw png and calling following function. I could not understand the following code. I know it is weired question that took some piece of code and ask question. But great if anyone provide.
#define IMAGE_SIZE_W 1024
#define IMAGE_SIZE_H 768
#define GRAPH_MARGIN_L 48
#define GRAPH_MARGIN_R 21
#define GRAPH_MARGIN_B 27
#define GRAPH_MARGIN_T 22
#define GRAPH_SIZE_W (IMAGE_SIZE_W-(GRAPH_MARGIN_L+GRAPH_MARGIN_R))
#define GRAPH_SIZE_H (IMAGE_SIZE_H-(GRAPH_MARGIN_B+GRAPH_MARGIN_T))
#define SCALE_TO_CANVAS(v,low,high,fws,margin,a,b) (((a+b*((v-low)/(high-low)))*fws)+margin)
#define SCALE_TO_CANVAS_Y(v,low,high) SCALE_TO_CANVAS(v,low,high,GRAPH_SIZE_H,GRAPH_MARGIN_T,1,-1)
#define SCALE_TO_CANVAS_X(v,low,high) SCALE_TO_CANVAS(v,low,high,GRAPH_SIZE_W,GRAPH_MARGIN_L,0,1)
void line_to_point(cairo_t *cr,float x, float y){
cairo_line_to(cr,x,y);
}
void move_to_point(cairo_t *cr,float x, float y){
cairo_move_to(cr,x,y);
}
We have data in x and y and ploting x and y. The caller function is /* counter number of x and y row
for(i = 0; i< counter; i++)
{
move_to_point(cr,SCALE_TO_CANVAS_X(xvals[i-1],lowX,highX),SCALE_TO_CANVAS_Y(yvals[i-1],lowY,highY));
line_to_point(cr,SCALE_TO_CANVAS_X(x,lowX,highX),SCALE_TO_CANVAS_Y(y,lowY,highY));
}
Than after it calls write_png which is kind of straight forward function.
if you look, SCALE_TO_CANVAS done lot of calculation which I am not able to figure out. cairo_line_to x and y have modified value and plot it.
Upvotes: -1
Views: 243
Reputation: 9877
I haven't looked too closely, but I would guess that your x data is between lowX and highX and your y data between lowY and highY. This data is scaled so that it it fits into the image surface (which means between 0x0 and 1024x768).
Also, this seems to add a margin on all sides (L/R/B/T would mean left, right, bottom, top).
If you want an explanation of the math used instead of just its meaning, just ask.
Edit:
Ok, we have data that describes points in a graph. The top-left corner of our input data is at position (lowX, lowY), the bottom right corner is at (highX, highY).
The area that we are drawing to has a size of 1024x768. Thus, we want to scale the input data so that it fits into this space. To make things a little more complicated, we want to keep an empty margin around the graph. This takes GRAPH_MARGIN_L pixels on the left, GRAPH_MARGIN_T pixels on the top, etc.
So the drawing area has its top-left corner at (GRAPH_MARGIN_L, GRAPH_MARGIN_T) and its bottom right corner at (1024-GRAPH_MARGIN_R, 768-GRAPH_MARGIN_B). So we are looking for a formula that maps (lowX, lowY) to (GRAPH_MARGIN_L, GRAPH_MARGIN_T) and (highX, highY) to (1024-GRAPH_MARGIN_R, 768-GRAPH_MARGIN_B).
In the following, let's just look at one of the two coordinates of the point, e.g. the x coordinate. For a point x, we first calculate a percentage that describes how far along the axis it is. 0% should mean "at the top/left end", 100% is "at the bottom/right end". The available space begins at lowX and goes to highX, so there are highX-lowX possible values. Thus, x-lowX (so that "top/left" really is at zero) falls somewhere into this range and (x-lowX)*100/(highX-lowX) is the percentage that we are looking for.
This percentage should now be mapped into the target area. First we multiple this by the width of the target space (and dividing by 100, so that the percentge goes away), so that we have a value that spans the possible range. Then we have to add the lowest possible value, so that instead of starting at zero and going up to the possible width (which is high-low), we have a value that is between low and high.
All together, the formula becomes smallestTargetX+(x-lowX)/(highX-lowX)*widthOfTheTargetArea, or in terms of your defines: GRAPH_MARGIN_L+(x-lowX)/(highX-lowX)*GRAPH_SIZE_W.
The additional variables a and b that are used in GRAPH_TO_CANVAS are used for "swapping directions". It seems like instead of having (0, 0) in the top-left corner and positive coordinates going to the right/bottom, your coordinate system has (0, 0) in the bottom-left corner and positive coordinates go to the right/top. To handle this, the percentages calculated above are multiplied with -1 and 1 is added to the coordinate, to make the mapping really map the points to each others that it should. These are the last two arguments for the GRAPH_TO_CANVAS macro.
I hope this helps you understand that macros.
Upvotes: 0