public void joinRoads(Intersection intersection){
  if (intersection.getRoadCount() != 2)   throw new IllegalArgumentException("Illegal intersection conversion");
  Road road0=intersection.getRoad(0);
  Road road1=intersection.getRoad(1);
  if (road0 == road1) {
    roads.remove(road1);
    intersections.remove(intersection);
    return;
  }
  if (road0.getRoadClass() != road1.getRoadClass())   throw new IllegalArgumentException("Illegal intersection conversion");
  intersections.remove(intersection);
  roads.remove(road1);
  RoadPoint[] road0Points=road0.getRoadPoints();
  RoadPoint[] road1Points=road1.getRoadPoints();
  RoadPoint[] innerPoints=new RoadPoint[road0Points.length + road1Points.length + 1];
  int j=0;
  Intersection firstIntersection;
  if (intersection == road0.getFirstIntersection()) {
    firstIntersection=road0.getSecondIntersection();
    for (int i=road0Points.length; --i >= 0; )     innerPoints[j++]=road0Points[i];
  }
 else {
    firstIntersection=road0.getFirstIntersection();
    System.arraycopy(road0Points,0,innerPoints,j,road0Points.length);
    j+=road0Points.length;
  }
  Intersection otherIntersection=road1.getOtherIntersection(intersection);
  otherIntersection.removeRoad(road1);
  road0.setIntersections(firstIntersection,otherIntersection);
  otherIntersection.addRoad(road0);
  innerPoints[j++]=new RoadPoint(road0,intersection.getLocation(),this);
  if (intersection == road1.getFirstIntersection()) {
    System.arraycopy(road1Points,0,innerPoints,j,road1Points.length);
    j+=road1Points.length;
  }
 else {
    for (int i=road1Points.length; --i >= 0; )     innerPoints[j++]=road1Points[i];
  }
  road0.setRoadPoints(innerPoints);
  road0.setName(mergeRoadNames(road0.getName(),road1.getName()));
}

public void joinRoads ( Intersection intersection ) { if ( intersection . getRoadCount ( ) != 2 ) throw new IllegalArgumentException ( "Illegal intersection conversion" ) ; Road road0 = intersection . getRoad ( 0 ) ; Road road1 = intersection . getRoad ( 1 ) ; if ( road0 == road1 ) { roads . remove ( road1 ) ; intersections . remove ( intersection ) ; return ; } if ( road0 . getRoadClass ( ) != road1 . getRoadClass ( ) ) throw new IllegalArgumentException ( "Illegal intersection conversion" ) ; intersections . remove ( intersection ) ; roads . remove ( road1 ) ; RoadPoint [ ] road0Points = road0 . getRoadPoints ( ) ; RoadPoint [ ] road1Points = road1 . getRoadPoints ( ) ; RoadPoint [ ] innerPoints = new RoadPoint [ road0Points . length + road1Points . length + 1 ] ; int j = 0 ; Intersection firstIntersection ; if ( intersection == road0 . getFirstIntersection ( ) ) { firstIntersection = road0 . getSecondIntersection ( ) ; for ( int i = road0Points . length ; -- i >= 0 ; ) innerPoints [ j ++ ] = road0Points [ i ] ; } else { firstIntersection = road0 . getFirstIntersection ( ) ; System . arraycopy ( road0Points , 0 , innerPoints , j , road0Points . length ) ; j += road0Points . length ; } Intersection otherIntersection = road1 . getOtherIntersection ( intersection ) ; otherIntersection . removeRoad ( road1 ) ; road0 . setIntersections ( firstIntersection , otherIntersection ) ; otherIntersection . addRoad ( road0 ) ; innerPoints [ j ++ ] = new RoadPoint ( road0 , intersection . getLocation ( ) , this ) ; if ( intersection == road1 . getFirstIntersection ( ) ) { System . arraycopy ( road1Points , 0 , innerPoints , j , road1Points . length ) ; j += road1Points . length ; } else { for ( int i = road1Points . length ; -- i >= 0 ; ) innerPoints [ j ++ ] = road1Points [ i ] ; } road0 . setRoadPoints ( innerPoints ) ; road0 . setName ( mergeRoadNames ( road0 . getName ( ) , road1 . getName ( ) ) ) ; }
join two roads into one . the roads must be the only two roads at the intersection and must be of the same class . if the roads are not distinct , then we quietly delete the road and remove the intersection . the roads might not be distinct if they form an isolated loop ( such as a racetrack ) . thus situation is particularly problematic if the road has no inner points . the roadpoints of both roads are concatenated with a new roadpoint where the intersection was between them . this code is a little complicated because the roadpoints must be assembled in a valid order . the order used is to start from the other intersection of the first road to the given intersection and from the given intersection of the second road to the other intersection of the second road .
