home *** CD-ROM | disk | FTP | other *** search
- unit RobotSeg; { Listing 10-4 }
-
- interface
-
- uses Graph, Mouse, Crt;
-
- const
- PI : real = 3.14159;
-
- type
-
- Degrees = 0..359;
-
- Point = object
- X,Y : Integer;
- procedure Init(NewX,NewY : Integer);
- procedure Show;
- procedure Hide;
- end;
-
-
- Segment = object
- Anchor : Point;
- BusyEnd : Point;
- Length : integer;
- Orientation : Real;
- RotateStatus: Boolean;
- constructor Init( AnchorX, AnchorY, SegLen : Integer;
- Position : Degrees;
- Rotatable : Boolean );
- procedure MoveAxial( var i : integer ); virtual;
- procedure MoveAnchor( NewX, NewY : integer );
- procedure Rotate( i : integer );
- procedure Show;
- procedure Hide;
- function RotateQ : Boolean;
- end;
-
- function Distance( P1, P2 : Point ) : real;
-
-
- implementation
-
- function Distance( P1, P2 : Point ) : real;
- begin
- Distance :=
- Sqrt( Sqr( P1.X - P2.X + 0.01) + Sqr( P1.Y - P2.Y + 0.01) );
- end;
-
- procedure Point.Init( NewX, NewY : integer );
- begin
- X := NewX;
- Y := NewY;
- end;
-
- procedure Point.Show;
- begin
- Graph.Rectangle( X-8, Y-8, X+8, Y+8);
- end;
-
- procedure Point.Hide;
- var TmpClr : integer;
- begin
- TmpClr := Graph.GetColor;
- Graph.SetColor(GetBkColor);
- Show;
- Graph.SetColor(TmpClr);
- end;
-
- constructor Segment.Init( AnchorX, AnchorY, SegLen : Integer;
- Position : Degrees;
- Rotatable : Boolean );
- var BX, BY : integer;
- begin
- Anchor.Init( AnchorX, AnchorY );
- RotateStatus := Rotatable;
- Orientation := Position * PI / 180.0;
- Length := SegLen;
- BX := Round(Anchor.X + cos(Orientation)*SegLen);
- BY := Round(Anchor.Y - sin(Orientation)*SegLen);
- BusyEnd.Init( BX, BY );
- end;
-
- procedure Segment.MoveAxial( var i : integer );
- var D : real;
- begin
- Hide;
- D := Distance( Anchor, BusyEnd );
- if D-i >= Length then
- D := Length+i;
- BusyEnd.X := Round(Anchor.X + cos(Orientation)*(D-i));
- BusyEnd.Y := Round(Anchor.Y - sin(Orientation)*(D-i));
- Show;
- end;
-
- procedure Segment.MoveAnchor( NewX, NewY : integer );
- var
- DeltaX, DeltaY : integer;
- begin
- Hide;
- with Anchor do
- begin
- DeltaX := X - NewX;
- DeltaY := Y - NewY;
- X := NewX;
- Y := NewY;
- end;
- with BusyEnd do
- begin
- X := X + DeltaX;
- Y := Y + DeltaY;
- end;
- Show;
- end;
-
- procedure Segment.Rotate( i : integer );
- begin
- Hide;
- Orientation := Orientation + ( i * ( PI / 90.0 ) ) ;
- Show;
- end;
-
- function Segment.RotateQ : Boolean;
- begin
- RotateQ := RotateStatus;
- end;
-
- procedure Segment.Show;
- var
- D : real;
- FX, FY : integer;
- begin
- D := Distance( Anchor, BusyEnd );
- BusyEnd.X := Round(Anchor.X + cos(Orientation)*D);
- BusyEnd.Y := Round(Anchor.Y - sin(Orientation)*D);
- FX := Round(BusyEnd.X - cos(Orientation)*Length);
- FY := Round(BusyEnd.Y + sin(Orientation)*Length);
- Graph.Line( FX, FY, BusyEnd.X, BusyEnd.Y );
- Graph.Circle( BusyEnd.X, BusyEnd.Y, 2);
- end;
-
- procedure Segment.Hide;
- var
- TmpClr : integer;
- LST : LineSettingsType;
- begin
- TmpClr := Graph.GetColor;
- Graph.SetColor(GetBkColor);
- Show;
- Graph.SetColor(TmpClr);
-
- end;
-
- end.