【问题标题】:Calculating Points on a Circle [Java / Processing]计算圆上的点 [Java / 处理]
【发布时间】:2014-05-21 08:27:11
【问题描述】:

我需要计算红线(在下图中)与圆的圆周相交的位置。问题是我不知道它们会以什么角度(从中心)穿过圆周。

我只知道圆的半径(用蓝线表示)和红线的 x 位置(每条偏移半径/4,用绿线表示)。

任何类型的数学解决方案都将受到赞赏,但 Java / 处理的奖励积分。

【问题讨论】:

  • 不应该迁移到math.stackexchange.com吗?
  • 可能是 math.stackexchange.com ?
  • 最后不是问cosinus函数和sinus函数的定义吗?
  • 基本上您知道直角三角形的一侧(半径的 1/4 或 1/2 或 3/4)和斜边(=半径),因此您可以轻松晒黑求角度。
  • @arynaq 从圆心到红线与圆周相交点的线就是半径。

标签: java processing geometry trigonometry


【解决方案1】:

您知道水平值,即从红线到中心的距离。我们称之为horz

你已经知道半径,所以你可以得到角度

Math.acos(horz / radius)

(已解决,未测试)

【讨论】:

    【解决方案2】:

    对于归一化坐标,y坐标的计算是

    private static double computeY(double x)
    {
        return Math.sin(Math.acos(x));
    }
    

    “归一化”是指

    • 参数x是一个介于0.0和1.0之间的值,由绝对坐标除以半径计算得到
    • 结果,y,是一个介于 0.0 和 1.0 之间的值,可以通过乘以半径转换为绝对坐标

    如果你只需要角度,这可以简单地计算为Math.acos(x)

    结果如下:

    代码:

    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionListener;
    import java.awt.geom.Ellipse2D;
    import java.awt.geom.Line2D;
    import java.io.IOException;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    
    public class CircleIntersectionTest
    {
        public static void main(String[] args) throws IOException
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                @Override
                public void run()
                {
                    createAndShowGUI();
                }
            });
        }
    
        private static void createAndShowGUI()
        {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(new CircleIntersectionPanel());
            f.setSize(500,500);
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    }
    
    class CircleIntersectionPanel extends JPanel
        implements MouseMotionListener
    {
        private Point mousePosition = null;
    
        CircleIntersectionPanel()
        {
            addMouseMotionListener(this);
        }
    
        @Override
        protected void paintComponent(Graphics gr)
        {
            super.paintComponent(gr);
            Graphics2D g = (Graphics2D)gr;
    
            double centerX = getWidth() / 2;
            double centerY = getHeight() / 2;
            double radius = 200;
    
            g.setStroke(new BasicStroke(2));
            g.setColor(Color.BLACK);;
            g.draw(new Ellipse2D.Double(
                centerX-radius, centerY-radius, 
                radius+radius, radius+radius));
            if (mousePosition == null)
            {
                return;
            }
    
            g.setColor(Color.RED);
            g.draw(new Line2D.Double(
                mousePosition.x, centerY, mousePosition.x, 0));
    
            g.setColor(Color.BLUE);
    
            double x = (mousePosition.x - centerX) / radius;
            double y = computeY(x);
    
            double cx = centerX + radius * x;
            double cy = centerY - radius * y;
            g.fill(new Ellipse2D.Double(cx-8, cy-8, 16, 16));
    
            g.setColor(Color.BLACK);
            g.drawString("x = "+x, 10, 30);
            g.drawString("y = "+y, 10, 46);
            g.drawString("angle: "+Math.toDegrees(Math.acos(x)), 10, 62);
    
        }
    
        private static double computeY(double x)
        {
            return Math.sin(Math.acos(x));
        }
    
    
        @Override
        public void mouseMoved(MouseEvent e)
        {
            mousePosition = e.getPoint();
            repaint();
        }
    
        @Override
        public void mouseDragged(MouseEvent e)
        {
        }
    
    }
    

    【讨论】:

    • 您的解决方案似乎没有使用您传入的半径?
    • @EvanKnowles 谢谢,已修复(我最初在方法中进行了标准化,但在调用方法之前进行标准化更有意义)
    猜你喜欢
    • 1970-01-01
    • 2015-07-11
    • 1970-01-01
    • 2018-11-20
    • 1970-01-01
    • 1970-01-01
    • 2021-04-09
    • 1970-01-01
    • 2014-12-03
    相关资源
    最近更新 更多