线穿过矩形-如何找到交点?

7
我正在向画布上绘制一个矩形和从矩形中心到坐标空间的某个随机点的线。
现在,我想缩短线段,使其长度在矩形内,以便线段从矩形边缘开始。
如何实现?
示例:
- 矩形由2个点定义:Pstart(1, 3), Pend(3, 1) - 中心点可以计算为: P(2, 2) - 现在从P(2,2)Q(10,2)画线
由于我知道矩形的宽度为2,我可以让线段从P(4,2)而不是P(2,2)开始。
当点不平行于XY轴时,这变得更加复杂。此外,对角线的长度在矩形内部也会有所不同。
如何计算线段起始点相对于矩形中心和线段终点的偏移量?
可能需要找到线段穿过矩形的点,然后让线段从交点开始。但是如何获取这个点呢?

1
这个页面上有很多解决方案:https://dev59.com/nXI-5IYBdhLWcg3w-9sK选择那个对你来说最有意义的解决方案并开始实施。 - Michael
3个回答

14

说实话,我不理解这个数学问题,但是...

本质上,你有5条线。原始线和矩形的4条线。因此,如果将其简化为一道简单的线相交问题,它应该会变得更容易...

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class IntersectPoint {

    public static void main(String[] args) {
        new IntersectPoint();
    }

    public IntersectPoint() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            int x = (int) (getWidth() * 0.2f);
            int y = (int) (getHeight() * 0.2f);
            int width = (int) (getWidth() * 0.6f);
            int height = (int) (getHeight() * 0.6f);

            int x1 = x;
            int y1 = 0;
            int x2 = x + width;
            int y2 = getHeight();

            Line2D line = new Line2D.Double(x1, y1, x2, y2);
            Rectangle2D rect = new Rectangle2D.Double(x, y, width, height);

            Graphics2D g2d = (Graphics2D) g.create();
            g2d.draw(rect);
            g2d.draw(line);

            g2d.setColor(Color.RED);
            Point2D[] ps = getIntersectionPoint(line, rect);
            for (Point2D p : ps) {
                if (p != null) {
                    g2d.fill(new Ellipse2D.Double(p.getX() - 4, p.getY() - 4, 8, 8));
                }
            }
            g2d.dispose();

        }

        public Point2D[] getIntersectionPoint(Line2D line, Rectangle2D rectangle) {

            Point2D[] p = new Point2D[4];

            // Top line
            p[0] = getIntersectionPoint(line,
                            new Line2D.Double(
                            rectangle.getX(),
                            rectangle.getY(),
                            rectangle.getX() + rectangle.getWidth(),
                            rectangle.getY()));
            // Bottom line
            p[1] = getIntersectionPoint(line,
                            new Line2D.Double(
                            rectangle.getX(),
                            rectangle.getY() + rectangle.getHeight(),
                            rectangle.getX() + rectangle.getWidth(),
                            rectangle.getY() + rectangle.getHeight()));
            // Left side...
            p[2] = getIntersectionPoint(line,
                            new Line2D.Double(
                            rectangle.getX(),
                            rectangle.getY(),
                            rectangle.getX(),
                            rectangle.getY() + rectangle.getHeight()));
            // Right side
            p[3] = getIntersectionPoint(line,
                            new Line2D.Double(
                            rectangle.getX() + rectangle.getWidth(),
                            rectangle.getY(),
                            rectangle.getX() + rectangle.getWidth(),
                            rectangle.getY() + rectangle.getHeight()));

            return p;

        }

        public Point2D getIntersectionPoint(Line2D lineA, Line2D lineB) {

            double x1 = lineA.getX1();
            double y1 = lineA.getY1();
            double x2 = lineA.getX2();
            double y2 = lineA.getY2();

            double x3 = lineB.getX1();
            double y3 = lineB.getY1();
            double x4 = lineB.getX2();
            double y4 = lineB.getY2();

            Point2D p = null;

            double d = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
            if (d != 0) {
                double xi = ((x3 - x4) * (x1 * y2 - y1 * x2) - (x1 - x2) * (x3 * y4 - y3 * x4)) / d;
                double yi = ((y3 - y4) * (x1 * y2 - y1 * x2) - (y1 - y2) * (x3 * y4 - y3 * x4)) / d;

                p = new Point2D.Double(xi, yi);

            }
            return p;
        }
    }
}

如果你知道其中一条线是垂直或水平的,那么getIntersectionPoint()函数可以大大简化。 - Edward Falk

3

0

矩形的顶点:a,b,c,d。将每个顶点的x和y坐标表示为ax,ay等。

线段的端点:x,y

该线段遵循y = mx + b,并向上或向下、向右或向左延伸。这将缩小你可能与之相交的矩形边缘为2个。

使用y = mx + b确定它穿过水平线的垂直坐标和穿过垂直线的水平分量。其中只有一个实际上位于你的矩形内(即包含在矩形的一条边缘内),或者它将在一个角落处相交。


实际上,您可以只使用 y=mx,因为该线从矩形中心开始。 - Breavyn
我还没有完全算出数学问题,但我不认为那会起作用。'b'偏移量告诉您x=0时的'y'坐标,即具有斜率'm'的线的垂直位置。这肯定会影响一条线与矩形边相交的情况,在一般情况下。我不假设任何这些点在原点上。 - arcy
是的,如果矩形以原点为中心,则可以假定b=0。 - Edward Falk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接