0

値が挿入されている3つの位置に値を挿入していますが、どういうわけか残りの部分をコピーしているときに最後のポイントをコピーしません。配列のサイズは増加していません。中間の配列に新しい要素を追加する方法を教えてください。

for(indexpoint=0;indexpoint<3;indexpoint++) 
{           
    temp.points[indexpoint].x = intpoints[indexpoint].x+this.x;
    temp.points[indexpoint].y = intpoints[indexpoint].y+this.y; 
}

temp.points[3].x = (intpoints[2].x+intpoints[3].x)/2+this.x;
temp.points[3].y = (intpoints[2].y+intpoints[3].y)/2+this.y;

for(indexpoint=3;indexpoint<intpoints.length;indexpoint++)
{           
    temp.points[indexpoint+1].x = intpoints[indexpoint].x+this.x;
    temp.points[indexpoint+1].y = intpoints[indexpoint].y+this.y;       
}
4

2 に答える 2

2

配列に新しい要素を挿入するには、メソッドを使用できますがsplice()、最初に、追加するオブジェクトを作成する必要があります (Pointコードでは a のようになります)。

const point:Point = new Point();
point.x = intpoints[2].x+intpoints[3].x)/2+this.x;
point.y = intpoints[2].y+intpoints[3].y)/2+this.y;

temp.points.splice(3, 0, point);

これを行うこともできます:

temp.points.length = 0;

for each (var point:Point in intpoints) {
    temp.points.add(point.clone().add(this));
}

const newPoint:Point = new Point();
newPoint.x = intpoints[2].x+intpoints[3].x)/2+this.x;
newPoint.y = intpoints[2].y+intpoints[3].y)/2+this.y;
temp.points.splice(3, 0, newPoint);
于 2013-02-13T22:33:48.003 に答える
0

splice関数を使用しないのはなぜですか?

array.splice( positionToInsertIn, 0, newValue );
于 2013-02-13T22:37:40.133 に答える