具有固定原点的图形

发布于 2024-10-28 00:54:12 字数 663 浏览 2 评论 0原文

我正在用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离不开的别离 2024-11-04 00:54:12

假设您的原点位于图形空间中的点 (originX,originY),但位于屏幕空间中的点 (pixelOriginY,pixelOriginX)。然后进行以下转换:

protected double yPositionToPixel(double position) {
    double height = (double) getHeight();
    return pixelOriginY - (position-originY) / (maxY-originY) * (height-pixelOriginY);
}

protected double xPositionToPixel(double position) {
    double width = (double) getWidth();
    return pixelOriginX + (position-originX) / (maxX-originX) * (width-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:

protected double yPositionToPixel(double position) {
    double height = (double) getHeight();
    return pixelOriginY - (position-originY) / (maxY-originY) * (height-pixelOriginY);
}

protected double xPositionToPixel(double position) {
    double width = (double) getWidth();
    return pixelOriginX + (position-originX) / (maxX-originX) * (width-pixelOriginX);
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文