具有固定原点的图形
我正在用 swing 绘制一个图表,我想设置一个固定的原点,即当我缩放图表时,仅更新轴的水平和垂直标签。 这是我到目前为止所拥有的:
protected void drawAxis(Graphics2D g2) {
double axisH = yPositionToPixel(originY);
double axisV = xPositionToPixel(originX);
g2.drawLine(0, (int) axisH, getWidth(), (int) axisH);
g2.drawLine((int) axisV, 0, (int) axisV, getHeight());
}
protected double yPositionToPixel(double position) {
double height = (double) getHeight();
return height - ((position) * (height) / (maxY));
}
protected double xPositionToPixel(double position) {
return (position) * (double) getWidth() / (maxX);
}
例如,当我缩放时,会重新计算原点,因此图形正在从其原始位置移动。 感谢您的帮助。
I m drawing a graph with swing, and i would like to set a fixed origin, that is when i scaled the graph, only the horizontal and vertical label from the axis are updated.
Here is what i have until now:
protected void drawAxis(Graphics2D g2) {
double axisH = yPositionToPixel(originY);
double axisV = xPositionToPixel(originX);
g2.drawLine(0, (int) axisH, getWidth(), (int) axisH);
g2.drawLine((int) axisV, 0, (int) axisV, getHeight());
}
protected double yPositionToPixel(double position) {
double height = (double) getHeight();
return height - ((position) * (height) / (maxY));
}
protected double xPositionToPixel(double position) {
return (position) * (double) getWidth() / (maxX);
}
When i zoom for exemple the origin is recalculated, so the graph is moving from its original place.
Thanks for help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您的原点位于图形空间中的点
(originX,originY)
,但位于屏幕空间中的点(pixelOriginY,pixelOriginX)
。然后进行以下转换:如果您在屏幕上将原点设置为
(0,0)
和(0,height)
,您将得到方程。因此,我假设您的原点不在(0,0)
处,因此您必须定义这个附加点,您的原点应位于屏幕空间中。Assume your origin is at point
(originX,originY)
in your graph space but(pixelOriginY,pixelOriginX)
in the screen space. Then the following transformations apply:In case you set the origin to
(0,0)
and(0,height)
on your screen you'll get back your equations. Therefore I assume your origin is not at(0,0)
and thus you have to define this additional point where your origin should be in the screen space.