java 圆的交点_java – 获取线条和形状的交点

理念

您可以使用getPathIterator()方法将GenenralPath解构为其段(移动到,行到,四到,立方到,关闭).现在,您可以搜索每个线段与线的交叉点.

public static Point[] getIntersections(Path path, Line line) {

List intersections = new ArrayList();

PathIterator it = path.getPathIterator();

double[] coords = new double[6];

double[] pos = new double[2];

while (!it.isDone()) {

int type = it.currentSegment(coords);

switch (type) {

case PathIterator.SEG_MOVETO:

pos[0] = coords[0];

pos[1] = coords[1];

break;

case PathIterator.SEG_LINETO:

Line l = new Line(pos[0], pos[1], coords[0], coords[1]);

pos[0] = coords[0];

pos[1] = coords[1];

Point intersection = getIntersection(line, l);

if (intersection != null)

intersections.add(intersection);

break;

//...

default:

throw new IllegalStateException("unknown PathIterator segment type: " + type);

}

it.next();

}

return intersections.toArray(new Point[] {});

}

线/线交叉点

可以直接计算线/线交点,例如,使用向量代数:

> 2d点/线由3d矢量(x,y,w)表示

>点(x,y)由(x,y,1)表示

>通过点p1和p2的线由p1 x p2给出(交叉积)

>对于两条线l1 =(a,b,c)和l2 =(d,e,f),交点由l1 x l2给出(交叉积)

>要将交叉点投影到2d,您必须将x和y坐标除以w

>如果w = 0则没有单点交叉点

线/贝塞尔交叉口

路径可以包含二次和三次贝塞尔曲线.要查找直线和贝塞尔曲线之间的交点,可以使用多种算法,例如:

> de Casteljau细分

>贝塞尔剪裁

>牛顿的方法

>多项式根发现

De Casteljau细分易于实施,但在相对罕见的情况下存在一些问题.如果您不想使用可以为您计算交叉点的数学库,我建议实施de Casteljau细分.

编辑:另一种选择是通过多个线段来近似路径的贝塞尔曲线段.然后你只需要找到线/线交叉点.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值