好的,这是我第一次尝试完成我需要做的事情。我对结果不是很满意,但它似乎奏效了。
package {
import flash.display.Sprite;
public class lineSeriesPointConcat extends Sprite {
public function lineSeriesPointConcat() {
init();
}
//directions [X -> Y]
//case 1: both counterclockwise, result is counterclockwise
private var case1:Array = [
["G1", "F1"],
["F1", "E1"],
["D1", "C1"],
["C1", "B1"],
["B1", "A1"],
["E1", "D1"], //link
["G1", "A1"] //loop
];
//case 2: clockwise, counterclockwise, result is clockwise
private var case2:Array = [
["E2", "F2"],
["F2", "G2"],
["D2", "C2"],
["C2", "B2"],
["B2", "A2"],
["E2", "D2"], //link
["G2", "A2"] //loop
];
//case 3: both clockwise, result is clockwise
private var case3:Array = [
["E3", "F3"],
["F3", "G3"],
["A3", "B3"],
["B3", "C3"],
["C3", "D3"],
["E3", "D3"], //link
["G3", "A3"] //loop
];
//case 4: counterclockwise, clockwise, result is clockwise
private var case4:Array = [
["G4", "F4"],
["F4", "E4"],
["A4", "B4"],
["B4", "C4"],
["C4", "D4"],
["E4", "D4"], //link
["G4", "A4"] //loop
];
private var collectedSeries:Array = [];
private function init():void {
var directions:Array = case1.concat(case2.concat(case3.concat(case4)));
for each (var direction:Array in directions) {
connect(direction[0], direction[1]);
}
trace ("final series:\n\t" + collectedSeries.join("\n\t"));
}
private function connect(from:String, to:String):void {
var series:Array;
var seriesStart:String;
var seriesEnd:String;
var seriesIndex:int;
var n:int = collectedSeries.length;
var i:int;
for (i = 0; i < n; i++) {
series = collectedSeries[i];
seriesStart = series[0];
seriesEnd = series[series.length - 1];
if (seriesStart == to) {
seriesStart = from;
series.unshift(from);
break;
} else if (seriesStart == from) {
seriesStart = to;
series.unshift(to);
break;
} else if (seriesEnd == to) {
seriesEnd = from;
series.push(from);
break;
} else if (seriesEnd == from) {
seriesEnd = to;
series.push(to);
break;
}
}
if (i == n) {
//this is a new series segment
series = [from, to];
seriesStart = from;
seriesEnd = to;
collectedSeries.push(series);
}
for (var j:int = 0; j < n; j++) {
var compareSeries:Array = collectedSeries[j];
if (compareSeries == series) {
//don't compare the series to itself.
continue;
}
var compSeriesStart:String = compareSeries[0];
var compSeriesEnd:String = compareSeries[compareSeries.length - 1];
if (compSeriesStart == compSeriesEnd) {
//this series loops on itself, it will not concatenate further
continue;
}
if (compSeriesStart == seriesEnd) {
trace ("case 1");
series = series.concat(compareSeries.slice(1));
} else if (compSeriesStart == seriesStart) {
trace ("case 2");
series = compareSeries.reverse().concat(series.slice(1));
} else if (compSeriesEnd == seriesStart) {
trace ("case 3");
series = compareSeries.concat(series.slice(1));
} else if (compSeriesEnd == seriesEnd) {
trace ("case 4");
series = compareSeries.concat(series.reverse().slice(1));
} else {
//no linkage between these two series
continue;
}
collectedSeries[i] = series; //replace one of the two segements
collectedSeries.splice(j, 1); //splice out the other
break;
}
trace ("series: " + series + (i == n ? " new" : ""));
}
}
}
这将给出以下结果:
A1,G1,F1,E1,D1,C1,B1,A1
G2,A2,B2,C2,D2,E2,F2,G2
G3,A3,B3,C3,D3,E3,F3,G3
G4,A4,B4,C4,D4,E4,F4,G4
我仍然非常感谢我能得到的任何建议/反馈。没人用Conrec吗?!
编辑:哎呀!我原来的 splice() 有一个错误!对不起!现在修复