java中如何在对角线上画一个圆?

发布于 12-09 01:46 字数 2185 浏览 0 评论 0原文

我有一条对角线,从屏幕的左下部分开始到屏幕的右上部分。现在我想在从左下部分开始到右上部分的距离为100米的线上画一个圆,这就是我的问题。你能给我一些关于这件事的想法吗?

任何帮助将不胜感激......

这是代码:

public void paint(Graphics g)
{
    super.paint(g);

    Graphics2D g2d = (Graphics2D) g;
    g2d.setBackground(Color.white);

    int x0_pixel = 0;
    int y0_pixel = 0;

    int x1_pixel = getWidth();
    int y1_pixel = getHeight();

    int x0_world = 0;
    int y0_world = 0;

    double x1_world = 2000; // meters
    double y1_world = 1125; // meters

    double x_ratio = (double) x1_pixel / x1_world;
    double y_ratio = (double) y1_pixel / y1_world;

    int xFrom = 0;
    int yFrom = 0;

    double xTo = x1_world;
    double yTo = y1_world;

    int FromX_pixel = convertToPixelX(xFrom, x_ratio);
    int FromY_pixel = convertToPixelY(y1_pixel, yFrom, y_ratio);

    int ToX_pixel = convertToPixelX((int) xTo, x_ratio);
    int ToY_pixel = convertToPixelY(y1_pixel, (int) yTo, y_ratio);

    g2d.setColor(Color.RED);
    g2d.drawLine(FromX_pixel, FromY_pixel, ToX_pixel, ToY_pixel);

    double theta = Math.atan(yTo / xTo);

    double len = (int) Math.sqrt(xTo * xTo + yTo * yTo);

    int interval = 100;

    for (int distance = xFrom; distance < len; distance += interval)
    {
        double distance_x = (int) Math.cos(theta * distance);
        double distance_y = (int) Math.sin(theta * distance);

        int x_circle_pixel = convertToPixelCircleX(FromX_pixel, distance_x);
        int y_circle_pixel = convertToPixelCircleY(y1_pixel, distance_y, y_ratio);

        g2d.drawOval(x_circle_pixel, y_circle_pixel, 10, 10);
    }

    Toolkit.getDefaultToolkit().sync();
    g2d.dispose();

 }

 private static int convertToPixelY(int y_offset, int y_world, double y_ratio)
 {
     return (int) (y_offset - (y_world * y_ratio));
 }

 private static int convertToPixelX(int x_world, double ratio)
 {
     return (int) (x_world * ratio);
 }

private static int convertToPixelCircleY(int y_offset, double distance, double y_ratio)
{
     return (int) (y_offset - (distance * y_ratio));
}

private static int convertToPixelCircleX(int x_world, double distance_x)
{
     return (int) (x_world * distance_x);
} 

I have a diagonal line starting from the left bottom part of the screen going to the upper right part of the screen. Now I want to draw a circle to the line having 100 meters in distance starting from the left bottom part going to the upper right part and that is my problem. Could you give me some ideas about this matter?

Any help would be much appreciated...

Here's the code :

public void paint(Graphics g)
{
    super.paint(g);

    Graphics2D g2d = (Graphics2D) g;
    g2d.setBackground(Color.white);

    int x0_pixel = 0;
    int y0_pixel = 0;

    int x1_pixel = getWidth();
    int y1_pixel = getHeight();

    int x0_world = 0;
    int y0_world = 0;

    double x1_world = 2000; // meters
    double y1_world = 1125; // meters

    double x_ratio = (double) x1_pixel / x1_world;
    double y_ratio = (double) y1_pixel / y1_world;

    int xFrom = 0;
    int yFrom = 0;

    double xTo = x1_world;
    double yTo = y1_world;

    int FromX_pixel = convertToPixelX(xFrom, x_ratio);
    int FromY_pixel = convertToPixelY(y1_pixel, yFrom, y_ratio);

    int ToX_pixel = convertToPixelX((int) xTo, x_ratio);
    int ToY_pixel = convertToPixelY(y1_pixel, (int) yTo, y_ratio);

    g2d.setColor(Color.RED);
    g2d.drawLine(FromX_pixel, FromY_pixel, ToX_pixel, ToY_pixel);

    double theta = Math.atan(yTo / xTo);

    double len = (int) Math.sqrt(xTo * xTo + yTo * yTo);

    int interval = 100;

    for (int distance = xFrom; distance < len; distance += interval)
    {
        double distance_x = (int) Math.cos(theta * distance);
        double distance_y = (int) Math.sin(theta * distance);

        int x_circle_pixel = convertToPixelCircleX(FromX_pixel, distance_x);
        int y_circle_pixel = convertToPixelCircleY(y1_pixel, distance_y, y_ratio);

        g2d.drawOval(x_circle_pixel, y_circle_pixel, 10, 10);
    }

    Toolkit.getDefaultToolkit().sync();
    g2d.dispose();

 }

 private static int convertToPixelY(int y_offset, int y_world, double y_ratio)
 {
     return (int) (y_offset - (y_world * y_ratio));
 }

 private static int convertToPixelX(int x_world, double ratio)
 {
     return (int) (x_world * ratio);
 }

private static int convertToPixelCircleY(int y_offset, double distance, double y_ratio)
{
     return (int) (y_offset - (distance * y_ratio));
}

private static int convertToPixelCircleX(int x_world, double distance_x)
{
     return (int) (x_world * distance_x);
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

故乡的云2024-12-16 01:46:03

看看你的 FOR 部分,我相信这就是错误所在。

final double cosTheta = Math.cos(theta);
final double sinTheta = Math.sin(theta);

for (int distance = xFrom; distance < len; distance += interval) {

    double distance_x = distance * cosTheta;
    double distance_y = distance * sinTheta;

    //int x_circle_pixel = convertToPixelCircleX(FromX_pixel, distance_x);
    //int y_circle_pixel = convertToPixelCircleY(y1_pixel, distance_y, y_ratio);
    int x_circle_pixel = convertToPixelX((int) distance_x, x_ratio);
    int y_circle_pixel = convertToPixelY(y1_pixel, (int) distance_y, y_ratio);

    g2d.drawOval(x_circle_pixel, y_circle_pixel, 10, 10);
}

Look at your FOR section, I believe that that's where the error is.

final double cosTheta = Math.cos(theta);
final double sinTheta = Math.sin(theta);

for (int distance = xFrom; distance < len; distance += interval) {

    double distance_x = distance * cosTheta;
    double distance_y = distance * sinTheta;

    //int x_circle_pixel = convertToPixelCircleX(FromX_pixel, distance_x);
    //int y_circle_pixel = convertToPixelCircleY(y1_pixel, distance_y, y_ratio);
    int x_circle_pixel = convertToPixelX((int) distance_x, x_ratio);
    int y_circle_pixel = convertToPixelY(y1_pixel, (int) distance_y, y_ratio);

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