如何将圆圈放在线的中心?

发布于 2024-12-08 21:44:15 字数 2000 浏览 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);

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

    int interval = 100;

    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 = convertToPixelX(distance_x, x_ratio);
        int y_circle_pixel = convertToPixelY(y1_pixel, distance_y, y_ratio);

        g2d.drawOval(x_circle_pixel, y_circle_pixel, 50, 50);

        g2d.setColor(Color.BLUE);

    }

    Toolkit.getDefaultToolkit().

            sync();

    g2d.dispose();

}

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

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

I have a diagonal line and I have also a circles having a 100 meters in distance. The problem is that the circles are not really to the center of the line. I know this is quiet easy but I'm just confused on how to do it.. Could someone help me how to put the circles at the center of the line?

Here's what I've tried so far :

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);

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

    int interval = 100;

    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 = convertToPixelX(distance_x, x_ratio);
        int y_circle_pixel = convertToPixelY(y1_pixel, distance_y, y_ratio);

        g2d.drawOval(x_circle_pixel, y_circle_pixel, 50, 50);

        g2d.setColor(Color.BLUE);

    }

    Toolkit.getDefaultToolkit().

            sync();

    g2d.dispose();

}

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

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

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

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

发布评论

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

评论(1

凉薄对峙 2024-12-15 21:44:15

当您绘制椭圆形时,前两个参数是包含椭圆形的矩形的左上角。接下来的两个参数是同一边界矩形的宽度和高度。您当前的代码将左上角放置在线条本身上,但您实际想要的是边界矩形的中心放置在线条上。解决问题的方法是简单地将左上角移动直径的 1/2。你的代码应该是这样的:

public class GraphicsFoo extends JPanel {
   // avoid using magic numbers:
   private static final int CIRCLE_DIAMETER = 50; 

   //....

   // override a JComponent's paintComponent method, not its paint method
   @Override
   protected void paintComponent(Graphics g) {
       super.paintComponent(g);

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

       // make your graphics smooth
       g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
             RenderingHints.VALUE_ANTIALIAS_ON);

       // ...

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

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

           //....

           // *** here's the key: ***    
           g2d.drawOval(
              x_circle_pixel - CIRCLE_DIAMETER / 2, 
              y_circle_pixel - CIRCLE_DIAMETER / 2, 
              CIRCLE_DIAMETER, CIRCLE_DIAMETER);

           g2d.setColor(Color.BLUE);

       }

When you draw an oval, the first two parameters are the upper-left corner of the rectangle that holds the oval. The next two parameters are the width and height of this same bounding rectangle. Your current code places the upper-left corner on the line itself, but what you actually want is that the center of the bounding rectangle be placed on the line. The solution to your problem is to simply shift the upper-left corner over by 1/2 the diameter. Your code should have something like so:

public class GraphicsFoo extends JPanel {
   // avoid using magic numbers:
   private static final int CIRCLE_DIAMETER = 50; 

   //....

   // override a JComponent's paintComponent method, not its paint method
   @Override
   protected void paintComponent(Graphics g) {
       super.paintComponent(g);

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

       // make your graphics smooth
       g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
             RenderingHints.VALUE_ANTIALIAS_ON);

       // ...

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

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

           //....

           // *** here's the key: ***    
           g2d.drawOval(
              x_circle_pixel - CIRCLE_DIAMETER / 2, 
              y_circle_pixel - CIRCLE_DIAMETER / 2, 
              CIRCLE_DIAMETER, CIRCLE_DIAMETER);

           g2d.setColor(Color.BLUE);

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