home *** CD-ROM | disk | FTP | other *** search
- unit ProRobot; { Listing 10-1 }
-
- 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;
- procedure Init( AnchorX, AnchorY, SegLen : Integer;
- Position : Degrees );
- procedure MoveAxial( i : integer );
- procedure Rotate( i : integer );
- procedure Show;
- procedure Hide;
-
- 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;
-
- procedure Segment.Init( AnchorX, AnchorY, SegLen : Integer;
- Position : Degrees );
- var BX, BY : integer;
- begin
- Anchor.Init( AnchorX, AnchorY );
- 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( 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.Rotate( i : integer );
- begin
- Hide;
- Orientation := Orientation + ( i * ( PI / 90.0 ) );
- Show;
- 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.