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細分.
編輯:另一種選擇是通過多個線段來近似路徑的貝塞爾曲線段.然后你只需要找到線/線交叉點.
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的java 圆的交点_java – 获取线条和形状的交点的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 兄弟7895dw粉盒清零_兄弟打印机22
- 下一篇: ASCII 在线转换器