%% Add Y-shaped Intersection to Driving Scenario
% This example shows how to create a Y-shaped intersection and add it to the driving scenario to link three incoming roads.
% Create a driving scenario.

scenario = drivingScenario;

% Define lane specifications of the incoming roads.

laneSpecification = lanespec([2 2], 'Width', 2.575);

% Add the thee incoming roads to the driving scenario.

roadCenters = [-20.0 0 0;
                10.0 0 0];
road(scenario, roadCenters, 'Lanes', laneSpecification, 'Name', 'Road 1');

roadCenters = [23.0 6.5 0;
               50.0 32.5 0];
road(scenario, roadCenters, 'Lanes', laneSpecification, 'Name', 'Road 2');

roadCenters = [23.0 -6.5 0;
               50.0 -32.5 0];
road(scenario, roadCenters, 'Lanes', laneSpecification, 'Name', 'Road 3');

% Create a RoadGroup handle object.

rg = driving.scenario.RoadGroup('Name', 'Y Intersection');

% Attach a connecting road to  RoadGroup object for linking Road 1 and Road 2.

roadWidth = 10.45;
roadCenters = [23.0 6.5 0;
               21.3 5.0 0;
               14.4 0.6 0;
               10.0 0 0];
road(rg, roadCenters, roadWidth)

% Attach a connecting road to  RoadGroup object for linking Road 1 and Road 3.

roadCenters = [23.0 -6.5 0;
               21.3 -5.0 0;
               14.4 -0.6 0;
               10.0 0 0];
road(rg, roadCenters, roadWidth)

% Attach a connecting road to  RoadGroup object for linking Road 2 and Road 3.

roadCenters = [23.0 6.5 0;
               21.2 3.7 0;
               21.2 -3.7 0;
               23.0 -6.5 0];
road(rg, roadCenters, roadWidth)

% Add the intersection to the driving scenario and plot the scenario.

roadGroup(scenario, rg)
plot(scenario,'roadCenters','on')

% Copyright 2020 The MathWorks, Inc.