d3 javascript、svgパスの2点間の距離を取得

Nivedhitha Mathan Kumar

パスを描画するための一連のポイントがあります。

let path=svg.append("path").attr("id","path")
        .data([points])
        .attr("d", d3.line()
        .curve(d3.curveCatmullRom));

ここで、ポイント間のパスの距離を取得して、セグメントに分割できるようにします。距離を増やして、ポイント(5に四捨五入)が最初のポイントセットのポイントと一致するかどうかを確認し、一致するたびに距離を取得してみました。次に、そこまでポイントをリストとして保存します。

ここに、xyArray私が追加するポイントdのリストとリストsegもあります。

function distanceBetween2Points(path, xyArray) {
    let distance = 0;
    xyArray.forEach(function(d,i)
    {
        let flag=1;
        seg=[];

        while(flag)
            {let pt=path.getPointAtLength(distance);
            if(round5(pt.x)==round5(d.x) && round5(pt.y)==round5(d.y))
                {console.log("d",i);
                d.d=distance;
                d.seg=seg;
                flag=0;
                break;}
            seg.push([pt.x,pt.y]);
            distance++;}
        return 0;
    });
}

データによっては、機能する場合もありますが(正確ではありませんが)、機能しない場合もあります。距離を取得するためのより良い方法はありますか?

enxaneta

これはd3ではなくバニラJavaScriptを使用したデモですが、お役に立てば幸いです。

この関数getLengthForPoint(p,thePath)は、指定された点pのパス上の距離を計算しています。変数を設定していますlet precision = 100;パスの長さによっては、この値を別の値に変更することもできます。

また、パスが同じポイントを複数回通過する可能性があることにも注意してください。これは注意が必要で、エラーが発生する可能性があります。

また、ご存知かもしれませんが、ポイントまでのおおよその距離がわかります。この例では、ポイントp1 = {x:93.5,y:60}計算された長さの点には、次の座標があります。{x:93.94386291503906,y: 59.063079833984375}

// some points on the path
let p1 = {x:93.5,y:60}
let p2 = {x:165,y:106}
//the total length of the path
let pathLength = thePath.getTotalLength();
let precision = 100;
let division = pathLength / precision;

function getLengthForPoint(p,thePath){
    let theRecord = pathLength;
    let theSegment;
    for (let i = 0; i < precision; i++) {
    // get a point on the path for thia distance
    let _p = thePath.getPointAtLength(i * division);
    // get the distance between the new point _p and the point p
    let theDistance = dist(_p, p);
    if (theDistance < theRecord) {
    // if the distance is smaller than the record set the new record
      theRecord = theDistance; 
      theSegment = i;
    }
  }
  return(theSegment * division);
  }


let theDistanceOnThePath = getLengthForPoint(p1,thePath);

//if you calculate the coords of a point at the calculated distance you'll see that is very near the point 
console.log(thePath.getPointAtLength(theDistanceOnThePath));


let theDistanceBetween2PointsOnThePath = getLengthForPoint(p2,thePath) - getLengthForPoint(p1,thePath);





// a helper function to measure the distance between 2 points
function dist(p1, p2) {
  let dx = p2.x - p1.x;
  let dy = p2.y - p1.y;
  return Math.sqrt(dx * dx + dy * dy);
}
svg{border:solid}
<svg viewBox="0 10 340 120">
<path id="thePath" fill="none" stroke="black" d="M10, 24Q10,24,40,67Q70,110,93.5,60Q117,10,123.5,76Q130,142,165,106Q200,70,235,106.5Q270,143, 320,24"></path>
  <circle cx="93.5" cy="60" r="2" fill="red"/>
  <circle cx="165" cy="106" r="2" fill="red"/>
</svg>

パス上の2点間の距離を取得するには、次のようにします。

let theDistanceBetween2PointsOnThePath = getLengthForPoint(p2,thePath) - getLengthForPoint(p1,thePath);

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

2点間の角距離を計算し、3点目まで

分類Dev

3Dでの2点間の距離の計算

分類Dev

OpenLayers 3:2点間の距離を計算する方法は?

分類Dev

特定の距離にある2点間の3D空間の点を計算します

分類Dev

D3でドラッグの距離を見つける

分類Dev

GoogleマップV3の2点間の距離を計算する

分類Dev

d3、複数のパスを更新

分類Dev

GeoDjangoの2点間の3D距離(高度を含む)を計算する方法

分類Dev

Finding coordinates in d3 svg path

分類Dev

Use svg as icon with D3 and htmlwidgets

分類Dev

d3 svg attrtween each by each

分類Dev

Unity 3Dの2点間の距離を計算するにはどうすればよいですか?

分類Dev

D3:要素の削除

分類Dev

d3の管理図?

分類Dev

同心の放射円d3

分類Dev

D3多軸補間

分類Dev

d3ブラシのsvgでパスを作成する方法

分類Dev

d3 JavaScript translation to ClojureScript

分類Dev

JavaScript D3:ズーム

分類Dev

D3 Pie in Angular 2?

分類Dev

D3クラス間の遷移

分類Dev

d3 - understanding the documentation for d3.svg.line

分類Dev

D3折りたたみツリーの子ノード間の距離を増やす方法は?

分類Dev

d3軸パスのストローク幅属性を取得

分類Dev

D3 変更パスの y

分類Dev

d3パスd = MNaN、NaNLNaN、NaN

分類Dev

svgパスのd3 js三角形

分類Dev

D3とjavascriptを使用したSVGrectとSVGテキスト間の垂直方向の配置

分類Dev

2D空間で最も遠い2つの点を見つける(マンハッタン距離)