Code

This code creates the wagon shown in Figure 132

      Copyright ©  Taligent, Inc. 1995
    1  TFillAndFrameBundle backBundle( TRGBColor( .5, .5, .5 ), TRGBColor( 0, 0, 0 ) );
    2  
    3  TFillAndFrameBundle rightBundle( TRGBColor( .25, .25, .25 ), TRGBColor( 0, 0, 0 ) );
    4  
    5  TFillAndFrameBundle frontBundle( TRGBColor( .75, .75, .75 ), TRGBColor( 0, 0, 0 ) );
    6  
    7  TFillAndFrameBundle leftBundle( TRGBColor(.75, .75, .75 ), TRGBColor( 0, 0, 0 ) );
    8  
    9  TFillAndFrameBundle wheelBundle( TRGBColor( 1,1,1 ), TRGBColor( 0,0,0 ), 1.5 );
    10  
    11  TFillBundle* hubCapBundle( TRGBColor( 0,0,0 ) );
    12  
    13  TGPolygon backPanelGeometry;
    14  backPanelGeometry.Append( TGPoint( 50, 60 ) );
    15  backPanelGeometry.Append( TGPoint( 150, 60 ) );
    16  backPanelGeometry.Append( TGPoint( 150, 110 ) );
    17  backPanelGeometry.Append( TGPoint( 50, 110 ) );
    18  
    19  TGPolygon frontPanelGeometry;
    20  frontPanelGeometry.Append( TGPoint( 70, 80 ) );
    21  frontPanelGeometry.Append( TGPoint( 170, 80 ) );
    22  frontPanelGeometry.Append( TGPoint( 170, 130 ) );
    23  frontPanelGeometry.Append( TGPoint( 70, 130 ) );
    24  
    25  TGPolygon leftPanelGeometry;
    26  leftPanelGeometry.Append( TGPoint( 50, 60 ) );
    27  leftPanelGeometry.Append( TGPoint( 70, 80 ) );
    28  leftPanelGeometry.Append( TGPoint( 70, 130 ) );
    29  leftPanelGeometry.Append( TGPoint( 50, 110 ) );
    30  
    31  TGPolygon rightPanelGeometry;
    32  rightPanelGeometry.Append( TGPoint( 150, 60 ) );
    33  rightPanelGeometry.Append( TGPoint( 170, 80 ) );
    34  rightPanelGeometry.Append( TGPoint( 170, 130 ) );
    35  rightPanelGeometry.Append( TGPoint( 150, 110 ) );
    36  
    37  TPolygon* backPanel = new TPolygon( backPanelGeometry, new TGrafBundle( backBundle ) );
    38  TPolygon* frontPanel= new TPolygon( frontPanelGeometry, new TGrafBundle( frontBundle ));
    39  TPolygon* leftPanel = new TPolygon( leftPanelGeometry, new TGrafBundle( leftBundle ) );
    40  TPolygon* rightPanel = new TPolygon( rightPanelGeometry, new TGrafBundle(rightBundle ));
    41  
    42  TEllipse* rearAwayWheel = new TEllipse( TGRect(TGPoint( 40, 100), TGPoint( 90, 150 ) ));
    43  rearAwayWheel->AdoptBundle( new TGrafBundle( wheelBundle ) );
    44  
    45  TEllipse* hubCap = new TEllipse( *rearAwayWheel );
    46  hubCap->AdoptBundle( new TGrafBundle( hubCapBundle ) );
    47  hubCap->ScaleBy( TGPoint( -.75, -.25), hubCap->GetGeometricBounds().GetCenter() );
    48  
    49  TGraphicGroup* rearAwayHubWheel = new TGraphicGroup();
    50  rearAwayHubWheel->AdoptFirst( rearAwayWheel );
    51  rearAwayHubWheel->AdoptLast( hubCap );
    52  TGraphicGroup* frontAwayHubWheel = new TGraphicGroup( *rearAwayHubWheel );
    53  frontAwayHubWheel->TranslateBy( TGPoint( 90, 0 ) );
    54  TGraphicGroup* rearTowardHubWheel = new TGraphicGroup( *rearAwayHubWheel );
    55  rearTowardHubWheel->TranslateBy( TGPoint( 20, 10 ) );
    56  
    57  TGraphicGroup* frontTowardHubWheel = new TGraphicGroup( *rearAwayHubWheel );
    58  frontTowardHubWheel->TranslateBy( TGPoint( 110, 10 ) );
    59  
    60  TGraphicGroup* wagonBody = new TGraphicGroup();
    61  wagonBody->AdoptFirst( backPanel );
    62  wagonBody->AdoptLast( leftPanel );
    63  wagonBody->AdoptLast( rightPanel );
    64  wagonBody->AdoptLast( frontPanel );
    65  
    66  TGraphicGroup* wagonAwayWheels = new TGraphicGroup();
    67  wagonAwayWheels->AdoptFirst( rearAwayHubWheel );
    68  wagonAwayWheels->AdoptLast( frontAwayHubWheel );
    69  
    70  TGraphicGroup* wagonTowardWheels = new TGraphicGroup();
    71  wagonTowardWheels->AdoptFirst( rearTowardHubWheel );
    72  wagonTowardWheels->AdoptLast( frontTowardHubWheel );
    73  
    74  TGraphicGroup* wagon = new TGraphicGroup();
    75  wagon->AdoptFirst( wagonAwayWheels );
    76  wagon->AdoptLast( wagonBody );
    77  wagon->AdoptLast( wagonTowardWheels );
    78  
    79  wagon->Draw( thePort );
    80  
    81  wagon->TranslateBy( TGPoint( 200, 0 ) );
    82  rearAwayHubWheel->RotateBy( 90, rearAwayHubWheel->GetGeometricBounds().GetCenter() );
    83  frontAwayHubWheel->RotateBy( 90, frontAwayHubWheel->GetGeometricBounds().GetCenter() );
    84  rearTowardHubWheel->RotateBy( 90, rearTowardHubWheel->GetGeometricBounds().GetCenter());
    85  frontTowardHubWheel->RotateBy( 90, 
    86                                  frontTowardHubWheel->GetGeometricBounds().GetCenter());
    87  
    88  wagon->Draw( thePort );
Lines 1 through 11: Create the attribute bundles.

Lines 13 through 40: Create the wagon body. The wagon body consists of four polygons, one each for the wagon back panel, front panel, left panel, and right panel. Graphics that will be adopted into a group must be allocated on the heap.

Lines 42 through 58: Create wagon wheels and hub caps.The wagon wheels are ellipse graphics. The rear wheel that draws behind the wagon body (rearAwayWheel) and its hubcap are created first. The hubcap is created from the original ellipse, but scaled to its correct size. The rearAwayWheel and hubCap are adopted into the group rearAwayHubWheel.

The remaining three wheels are created from a copy of rearAwayHubWheel and translated by an amount that puts them at their correct location on the wagon body. Once a graphic is transformed, its points have changed, which is why rearAwayHubWheel is used in the three remaining instantiations.

Lines 60 through 64: Create wagon body group. The wagon body polygons created above are adopted into the group wagonBody in the order that they should draw. The backPanel is first because it is covered by other panels; frontPanel draws last because no other panels are over it.

Lines 66 through 72: Create wheel groups. The wagon wheel groups created above are nested within two other groups. The wagonAwayWheels group adopts the two wheels that draw behind the wagon body. The wagonTowardWheels group adopts the two wheels that draw in front of the wagon body. The two wagon wheel groups are necessary so that the wagon draws correctly when the wagon body and wagon wheels are adopted into the wagon group.

Lines 74 through 77: Create wagon group. Here wagonAwayWheels, wagonBody, and wagonTowardWheels are adopted into the wagon group. The order of the adoption is important so that the wagon will draw correctly as described in the next step.

Line 79: Draw the group. The Draw function draws wagonAwayWheels first, and then wagonBody over the wheels, and then draws wagonTowardWheels over the wagonBody.

Lines 81 through 88: Transform and draw the wagon. This code translates the wagon group by 200 world-coordinate units in the y direction to give you the wagon on the left in Figure 132. Applying the translation on the wagon group causes the entire wagon to translate.

This code also rotates the wagon wheels by 90 degrees. Applying the rotation on the wheels directly makes each wheel rotate by 90 degrees without rotating either the wagon body or the wheel groups (which would skew the drawing).


[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker