// intersection.cc #include "simulation.h" #include "vehicle.h" #include "lane.h" #include "intersection.h" intersection::intersection(float Ax_location, float Ay_location, intersection * Anext_intersection) { x_location = Ax_location; y_location = Ay_location; next_intersection = Anext_intersection; lane_north = new lane(x_location+1.0, y_location+2.0, 44.0, north, 5.0*my_scale); lane_east = new lane(x_location+2.0, y_location-1.0, 44.0, east , 26.0*my_scale); lane_south = new lane(x_location-1.0, y_location-2.0, 44.0, south, 5.0*my_scale); lane_west = new lane(x_location-2.0, y_location+1.0, 44.0, west , 26.0*my_scale); lane_from_south = 0; lane_from_west = 0; lane_from_north = 0; lane_from_east = 0; north_south = none; east_west = none; } void intersection::set_signal( intersection * Aintersection_list, signal_state Anorth_south, signal_state Aeast_west) { intersection * a_intersection = Aintersection_list; while( a_intersection != 0 ) { a_intersection->north_south = Anorth_south; a_intersection->east_west = Aeast_west; a_intersection->lane_north->set_signal(Anorth_south); a_intersection->lane_east->set_signal(Aeast_west); a_intersection->lane_south->set_signal(Anorth_south); a_intersection->lane_west->set_signal(Aeast_west); a_intersection = a_intersection->next(); } } void intersection::operate(intersection * Aintersection_list) { intersection * Aintersection = Aintersection_list; while( Aintersection != 0) { Aintersection->lane_north->operate(); if(Aintersection->lane_from_south->vehicle_exiting()) { Aintersection->lane_north->vehicle_add( Aintersection->lane_from_south->vehicle_remove()); } Aintersection->lane_east->operate(); if(Aintersection->lane_from_west->vehicle_exiting()) { Aintersection->lane_east->vehicle_add( Aintersection->lane_from_west->vehicle_remove()); } Aintersection->lane_south->operate(); if(Aintersection->lane_from_north->vehicle_exiting()) { Aintersection->lane_south->vehicle_add( Aintersection->lane_from_north->vehicle_remove()); } Aintersection->lane_west->operate(); if(Aintersection->lane_from_east->vehicle_exiting()) { Aintersection->lane_west->vehicle_add( Aintersection->lane_from_east->vehicle_remove()); } Aintersection = Aintersection->next_intersection; } } void intersection::creator( float Alength, float Aweight, float Aaccel, float Adecell, direction Amoving) { switch(Amoving) { case north: lane_north->vehicle_start( Alength, Aweight, Aaccel, Adecell); break; case east : lane_east->vehicle_start( Alength, Aweight, Aaccel, Adecell); break; case south : lane_south->vehicle_start( Alength, Aweight, Aaccel, Adecell); break; case west : lane_west->vehicle_start( Alength, Aweight, Aaccel, Adecell); break; } } void intersection::connect_from( intersection * Aintersection_list) { intersection * a_intersection = Aintersection_list; intersection * b_intersection; float x_lane_end; float y_lane_end; while( a_intersection != 0) { a_intersection->lane_north->lane_end( x_lane_end, y_lane_end); b_intersection = Aintersection_list; while( b_intersection != 0) { if( (fabs(x_lane_end-b_intersection->x_location) + fabs(y_lane_end-b_intersection->y_location)) < 7.0) { b_intersection->lane_from_south = a_intersection->lane_north; break; } b_intersection = b_intersection->next(); } a_intersection = a_intersection->next(); } a_intersection = Aintersection_list; while( a_intersection != 0) { a_intersection->lane_east->lane_end( x_lane_end, y_lane_end); b_intersection = Aintersection_list; while( b_intersection != 0) { if( (fabs(x_lane_end-b_intersection->x_location) + fabs(y_lane_end-b_intersection->y_location)) < 7.0) { b_intersection->lane_from_west = a_intersection->lane_east; break; } b_intersection = b_intersection->next(); } a_intersection = a_intersection->next(); } a_intersection = Aintersection_list; while( a_intersection != 0) { a_intersection->lane_south->lane_end( x_lane_end, y_lane_end); b_intersection = Aintersection_list; while( b_intersection != 0) { if( (fabs(x_lane_end-b_intersection->x_location) + fabs(y_lane_end-b_intersection->y_location)) < 7.0) { b_intersection->lane_from_north = a_intersection->lane_south; break; } b_intersection = b_intersection->next(); } a_intersection = a_intersection->next(); } a_intersection = Aintersection_list; while( a_intersection != 0) { a_intersection->lane_west->lane_end( x_lane_end, y_lane_end); b_intersection = Aintersection_list; while( b_intersection != 0) { if( (fabs(x_lane_end-b_intersection->x_location) + fabs(y_lane_end-b_intersection->y_location)) < 7.0) { b_intersection->lane_from_east = a_intersection->lane_west; break; } b_intersection = b_intersection->next(); } a_intersection = a_intersection->next(); } } intersection * intersection::next(void) { return next_intersection; } void intersection::draw(intersection * Aintersection_list) { intersection * Aintersection = Aintersection_list; while( Aintersection != 0) { DRAW_CIRCLE( Aintersection->x_location, Aintersection->y_location, 2.0); Aintersection->lane_north->draw(); Aintersection->lane_east ->draw(); Aintersection->lane_south->draw(); Aintersection->lane_west ->draw(); Aintersection = Aintersection->next_intersection; } } // end intersection.cc