- Timestamp:
- Mar 27, 2015, 12:49:17 PM (10 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UEngine.pas
r12 r13 82 82 Line: TMetroLine; 83 83 TrackPos: Integer; 84 //Angle: Double; 84 85 Direction: Integer; 85 86 InStation: Boolean; … … 87 88 TargetStationIndex: Integer; 88 89 function GetPosition: TPoint; 90 function GetAngle: Double; 89 91 constructor Create; 90 92 destructor Destroy; override; … … 142 144 procedure DrawLine(Canvas: TCanvas; Pos: TPoint); 143 145 procedure DrawShape(Canvas: TCanvas; Position: TPoint; Shape: TStationShape; 144 Size: Integer );146 Size: Integer; Angle: Double); 145 147 procedure DrawClock(Canvas: TCanvas); 148 procedure DrawTrains(Canvas: TCanvas); 146 149 procedure ComputeShapeDistance; 147 150 procedure ComputeShapeDistanceStation(Station: TMetroStation; … … 420 423 Sum: Integer; 421 424 begin 425 Result := Point(0, 0); 422 426 if Assigned(Line) then 423 427 with Line do begin … … 434 438 end else Sum := Sum + D; 435 439 end; 436 Result := TrackPoints[High(TrackPoints)].Point; 437 end else Result := Point(0, 0); 440 if Length(TrackPoints) > 0 then 441 Result := TrackPoints[High(TrackPoints)].Point; 442 end; 443 end; 444 445 function TMetroTrain.GetAngle: Double; 446 var 447 I: Integer; 448 D: Integer; 449 Sum: Integer; 450 begin 451 Result := 0; 452 if Assigned(Line) then 453 with Line do begin 454 Sum := 0; 455 if Length(TrackPoints) > 1 then 456 for I := 1 to High(TrackPoints) do begin 457 D := Distance(TrackPoints[I].Point, TrackPoints[I - 1].Point); 458 if (Sum + D) > Self.TrackPos then begin 459 Result := ArcTan2(TrackPoints[I].Point.Y - TrackPoints[I - 1].Point.Y, 460 TrackPoints[I].Point.X - TrackPoints[I - 1].Point.X); 461 Exit; 462 end else Sum := Sum + D; 463 end; 464 end; 438 465 end; 439 466 … … 588 615 589 616 procedure TEngine.DrawShape(Canvas: TCanvas; Position: TPoint; Shape: TStationShape; 590 Size: Integer );617 Size: Integer; Angle: Double); 591 618 var 592 619 Points: array of TPoint; 593 620 I: Integer; 594 Angle : Double;621 Angle2: Double; 595 622 begin 596 623 case Shape of 597 ssSquare: Canvas.Rectangle( 598 Position.X - Size div 2, Position.Y - Size div 2, 599 Position.X + Size div 2, Position.Y + Size div 2); 624 ssSquare: begin 625 SetLength(Points, 4); 626 Points[0] := Point(Position.X - Size div 2, Position.Y - Size div 2); 627 Points[1] := Point(Position.X + Size div 2, Position.Y - Size div 2); 628 Points[2] := Point(Position.X + Size div 2, Position.Y + Size div 2); 629 Points[3] := Point(Position.X - Size div 2, Position.Y + Size div 2); 630 Points := RotatePoints(Position, Points, Angle); 631 Canvas.Polygon(Points); 632 end; 600 633 ssCircle: Canvas.Ellipse( 601 634 Position.X - Size div 2, Position.Y - Size div 2, … … 606 639 Points[1] := Point(Position.X + Size div 2, Position.Y + Size div 2); 607 640 Points[2] := Point(Position.X - Size div 2, Position.Y + Size div 2); 641 Points := RotatePoints(Position, Points, Angle); 608 642 Canvas.Polygon(Points); 609 643 end; … … 611 645 SetLength(Points, 10); 612 646 for I := 0 to 9 do begin 613 Angle := I / 10 * 2 * Pi - Pi / 2;647 Angle2 := I / 10 * 2 * Pi - Pi / 2; 614 648 if (I mod 2) = 0 then 615 Points[I] := Point(Round(Position.X + Cos(Angle ) * Size / 2),616 Round(Position.Y + Sin(Angle ) * Size / 2))649 Points[I] := Point(Round(Position.X + Cos(Angle2) * Size / 2), 650 Round(Position.Y + Sin(Angle2) * Size / 2)) 617 651 else 618 Points[I] := Point(Round(Position.X + Cos(Angle ) * Size / 5),619 Round(Position.Y + Sin(Angle ) * Size / 5));652 Points[I] := Point(Round(Position.X + Cos(Angle2) * Size / 5), 653 Round(Position.Y + Sin(Angle2) * Size / 5)); 620 654 end; 655 Points := RotatePoints(Position, Points, Angle); 621 656 Canvas.Polygon(Points); 622 657 end; … … 635 670 Points[10] := Point(Position.X - Size div 6, Position.Y - Size div 2); 636 671 Points[11] := Point(Position.X + Size div 6, Position.Y - Size div 2); 672 Points := RotatePoints(Position, Points, Angle); 637 673 Canvas.Polygon(Points); 638 674 end; … … 640 676 SetLength(Points, 5); 641 677 for I := 0 to 4 do begin 642 Angle := I / 5 * 2 * Pi - Pi / 2;643 Points[I] := Point(Round(Position.X + Cos(Angle ) * Size / 2),644 Round(Position.Y + Sin(Angle ) * Size / 2));678 Angle2 := I / 5 * 2 * Pi - Pi / 2; 679 Points[I] := Point(Round(Position.X + Cos(Angle2) * Size / 2), 680 Round(Position.Y + Sin(Angle2) * Size / 2)); 645 681 end; 682 Points := RotatePoints(Position, Points, Angle); 646 683 Canvas.Polygon(Points); 647 684 end; … … 652 689 Points[2] := Point(Position.X, Position.Y + Size div 2); 653 690 Points[3] := Point(Position.X - Size div 2, Position.Y); 691 Points := RotatePoints(Position, Points, Angle); 654 692 Canvas.Polygon(Points); 655 693 end; … … 763 801 end; 764 802 803 procedure TEngine.DrawTrains(Canvas: TCanvas); 804 var 805 I: Integer; 806 P: Integer; 807 Pos: TPoint; 808 Points: array of TPoint; 809 Angle: Double; 810 ShapePos: TPoint; 811 begin 812 // Draw trains 813 for I := 0 to Trains.Count - 1 do 814 with TMetroTrain(Trains[I]) do begin 815 if Assigned(Line) then begin 816 Canvas.Brush.Color := Line.Color; 817 Canvas.Brush.Style := bsSolid; 818 Canvas.Pen.Style := psClear; 819 Pos := GetPosition; 820 Angle := GetAngle; 821 822 SetLength(Points, 4); 823 Points[0] := RotatePoint(Pos, Point(Pos.X - TrainSize div 2, Pos.Y - TrainSize div 3), Angle); 824 Points[1] := RotatePoint(Pos, Point(Pos.X + TrainSize div 2, Pos.Y - TrainSize div 3), Angle); 825 Points[2] := RotatePoint(Pos, Point(Pos.X + TrainSize div 2, Pos.Y + TrainSize div 3), Angle); 826 Points[3] := RotatePoint(Pos, Point(Pos.X - TrainSize div 2, Pos.Y + TrainSize div 3), Angle); 827 Canvas.Polygon(Points); 828 Canvas.Brush.Color := clWhite; 829 for P := 0 to Passengers.Count - 1 do 830 with TMetroPassenger(Passengers[P]) do begin 831 ShapePos := Point(Pos.X - Trunc(TrainSize div 3 * 1) + (P mod 3) * TrainSize div 3, 832 Pos.Y - Trunc(TrainSize div 6 * 1) + (P div 3) * TrainSize div 3); 833 ShapePos := RotatePoint(Pos, ShapePos, Angle); 834 DrawShape(Canvas, ShapePos, Shape, TrainSize div 3, Angle + Pi / 2); 835 end; 836 end; 837 end; 838 839 end; 840 765 841 procedure TEngine.Tick; 766 842 const … … 1125 1201 end; 1126 1202 1127 // Draw trains 1128 for I := 0 to Trains.Count - 1 do 1129 with TMetroTrain(Trains[I]) do begin 1130 if Assigned(Line) then begin 1131 Canvas.Brush.Color := Line.Color; 1132 Canvas.Brush.Style := bsSolid; 1133 Canvas.Pen.Style := psClear; 1134 Pos := GetPosition; 1135 Canvas.Rectangle(Pos.X - TrainSize div 2, Pos.Y - TrainSize div 2, 1136 Pos.X + TrainSize div 2, Pos.Y + TrainSize div 2); 1137 Canvas.Brush.Color := clWhite; 1138 for P := 0 to Passengers.Count - 1 do 1139 with TMetroPassenger(Passengers[P]) do begin 1140 DrawShape(Canvas, Point(Pos.X - Trunc(TrainSize div 3 * 1) + (P mod 3) * TrainSize div 3, 1141 Pos.Y - Trunc(TrainSize div 3 * 1) + (P div 3) * TrainSize div 3), Shape, TrainSize div 3); 1142 end; 1143 end; 1144 end; 1203 DrawTrains(Canvas); 1145 1204 1146 1205 // Draw stations … … 1152 1211 Canvas.Brush.Style := bsClear; 1153 1212 Canvas.Pen.Color := SelectedLine.Color; 1154 DrawShape(Canvas, Position, Shape, StationSize + Canvas.Pen.Width + 6 );1213 DrawShape(Canvas, Position, Shape, StationSize + Canvas.Pen.Width + 6, 0); 1155 1214 end; 1156 1215 Canvas.Brush.Color := clWhite; 1157 1216 Canvas.Brush.Style := bsSolid; 1158 1217 Canvas.Pen.Color := clBlack; 1159 DrawShape(Canvas, Position, Shape, StationSize );1218 DrawShape(Canvas, Position, Shape, StationSize, 0); 1160 1219 1161 1220 // Draw passengers … … 1165 1224 with TMetroPassenger(Passengers[P]) do begin 1166 1225 DrawShape(Canvas, Point(Position.X + StationSize + P * (PassengerSize + 2), Position.Y - StationSize div 2), 1167 Shape, PassengerSize );1226 Shape, PassengerSize, 0); 1168 1227 end; 1169 1228 -
trunk/UGeometric.pas
r4 r13 8 8 Classes, SysUtils, Math; 9 9 10 type 11 TPointArray = array of TPoint; 12 10 13 function Distance(P1, P2: TPoint): Integer; 11 14 function Dot(const P1, P2: TPoint): Double; … … 13 16 function PointToLineDistance(const P, V, W: TPoint): Integer; 14 17 function ComparePoint(P1, P2: TPoint): Boolean; 18 function RotatePoint(Center, P: TPoint; Angle: Double): TPoint; 19 function RotatePoints(Center: TPoint; P: TPointArray; Angle: Double): TPointArray; 15 20 16 21 implementation … … 67 72 end; 68 73 74 function RotatePoint(Center, P: TPoint; Angle: Double): TPoint; 75 begin 76 P := Point(P.X - Center.X, P.Y - Center.Y); 77 Result := Point(Center.X + Round(P.X * Cos(Angle) - P.Y * Sin(Angle)), 78 Center.Y + Round(P.X * Sin(Angle) + P.Y * Cos(Angle))); 79 end; 80 81 function RotatePoints(Center: TPoint; P: TPointArray; Angle: Double): TPointArray; 82 var 83 I: Integer; 84 begin 85 SetLength(Result, Length(P)); 86 for I := 0 to High(P) do 87 Result[I] := RotatePoint(Center, P[I], Angle); 88 end; 89 69 90 70 91 end.
Note:
See TracChangeset
for help on using the changeset viewer.