Translating x,y while scaling

天大地大妈咪最大 提交于 2019-12-12 05:49:18

问题


I'm trying to graph x,y coordinates where the window size is 600px by 600px. (0,0) would be in the top left. (300,300) middle of the window. (600,600) would be in the bottom right. I am trying to translate latitude/longitude in radians to pixels and then plotting them. I'm calculating 1px = ? lat by

`fabs(lborder+rborder)/600`

I calculated lon by taking the top and bottom borders. Then when I want to find a specific position for a specific lat or lon:

lat/(previous # calculated above)

Problem is my window goes from 0,0 to 600,600 as explained above and I can get negative points and I'm not sure how to move them and I don't know how to center them around 300,300 when the bounds change.

At the moment, as long as I make the center (0,0) in terms of (x,y), not pixels, points get plotted where they're supposed to go. For example, if x is -1 to 1 and y is -3 to 3, (300px,300px) would be (0,0).

If I change the bounds to say x -.5 to 1 and y -3 to .5, (300px,300px) would be (.25, 1.25). However, the calculations above with these numbers.

1.5/600 = .0025 ----> 1px = .0025lat.
3.5/600 = .0058 ----> 1px = .0058lon.

Then taking the midpoint (.25,1.25):

.25/.0025 = 100px
1.25/.0058 = 215px

which is clearly not 300px,300px despite being the center of the graph.

Any ideas would be extremely helpful.


回答1:


If you would like to adjust the coordinates to the center, then maybe somthing like this?

Coord coordsFromCenter(Coord old_coord, float height, float width)
{
    Coord new_center;

    float new_x_center = width/2.0;
    float new_y_center = height/2.0;

    new_center.set_x(old_coord.get_x() - new_x_center);
    new_center.set_y(old_coord.get_y() - new_y_center);

    return new_center;
}


来源:https://stackoverflow.com/questions/31953674/translating-x-y-while-scaling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!