java 圆的交点_java – 获取线条和形状的交点
理念
您可以使用getPathIterator()方法將GenenralPath解構(gòu)為其段(移動到,行到,四到,立方到,關(guān)閉).現(xiàn)在,您可以搜索每個線段與線的交叉點(diǎn).
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[] {});
}
線/線交叉點(diǎn)
可以直接計算線/線交點(diǎn),例如,使用向量代數(shù):
> 2d點(diǎn)/線由3d矢量(x,y,w)表示
>點(diǎn)(x,y)由(x,y,1)表示
>通過點(diǎn)p1和p2的線由p1 x p2給出(交叉積)
>對于兩條線l1 =(a,b,c)和l2 =(d,e,f),交點(diǎn)由l1 x l2給出(交叉積)
>要將交叉點(diǎn)投影到2d,您必須將x和y坐標(biāo)除以w
>如果w = 0則沒有單點(diǎn)交叉點(diǎn)
線/貝塞爾交叉口
路徑可以包含二次和三次貝塞爾曲線.要查找直線和貝塞爾曲線之間的交點(diǎn),可以使用多種算法,例如:
> de Casteljau細(xì)分
>貝塞爾剪裁
>牛頓的方法
>多項式根發(fā)現(xiàn)
De Casteljau細(xì)分易于實(shí)施,但在相對罕見的情況下存在一些問題.如果您不想使用可以為您計算交叉點(diǎn)的數(shù)學(xué)庫,我建議實(shí)施de Casteljau細(xì)分.
編輯:另一種選擇是通過多個線段來近似路徑的貝塞爾曲線段.然后你只需要找到線/線交叉點(diǎn).
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的java 圆的交点_java – 获取线条和形状的交点的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 兄弟7895dw粉盒清零_兄弟打印机22
- 下一篇: ASCII 在线转换器