计算圆上的点[Java / Processing]

时间:2014-05-21 08:27:11

标签: java processing geometry trigonometry

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

我所知道的唯一的事情是圆的半径(由蓝线表示)和红线的x位置(每个偏移半径/ 4,由绿线表示)。

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

Circles and stuff

2 个答案:

答案 0 :(得分:2)

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

您已知道半径,因此可以将角度设为

Math.acos(horz / radius)

(制定出来,未经测试)

答案 1 :(得分: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)

结果如下:

enter image description here

代码:

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)
    {
    }

}