Anda di halaman 1dari 9

CHAPTER 8: VECTOR GRAPHICS ROBOT

Introduction
In this chapter we will develop a few applications utilizing a pen at simulated robot center that
can be lowered to allow the robot to leave a trace on the floor as it moves.
With the proper program we can convert the robot into a device for drawing vector graphics
(meaning we specify a line to be drawn with a starting point, an angle, and a length).
There are many industrial applications for a robot that can draw on a surface. A robot could cut
intricate and complex designs out of metal sheets if a laser cutter is substituted for the pen.
Imagine a robot that can draw the yard lines and other messages and designs on a football field
once the desired data has been given to it.
8.1 DrawBot
The command to lower and raise the pen is:
rPen ExprN1 {,ExprN2}

If ExprN1 is 0, the pen will be raised and, if it is any number other than 0 the pen will be lowered.
You can also use the constants Up and Down . ExprN1 has to be a number. The pen is up when you
first initialize and rLocate the robot. ExprN2 is optional. If it is specified then the color of the pen
will be set to ExprN2. If it is not specified then the color of the pen will be set to the first color on
the invisible colors list.
If you have not specified an invisible colors list then the pen color will be black. ExprN2 should be
a valid color number.
If you dont place the pens color in the list of invisible colors the robot will crash if it ever
encounters its own trace. You must specify any colors you are likely to draw on the floor with the
pen as invisible colors so the robot will be able to drive over them. You can reissue the rPen
command with a different color to draw as many colors as you desire.
8.1.1 Drawing Circles

//---Draw Circles
MainProgram:
//---Change these values to change the position of the circle
R_Init_X = 100
R_Init_Y = 300
R_Init_Heading = 0
rLocate R_Init_X,R_Init_Y,R_Init_Heading
rInvisible Magenta
LineWidth 4
//---Change these values to change the size of the circle
fStep = 1
tStep = 1
gosub DrawCircle
rTurn 90 //--move out of the way
rForward 40
End
//======================================================
DrawCircle:
rPen Down //--start drawing
for i = 1 to 360/tStep
rForward fStep
rturn tStep
next

rPen Up //--stop drawing


Return
//======================================================
FIGURE 8.1 Drawing circles.

8.1.2 Drawing Rectangle

//DrawRectangles
MainProgram:
//changethesetomaketherectanglehave
//differentorientationandposition
R_Init_X=100
R_Init_Y=300
R_Init_Heading=45
rLocateR_Init_X,R_Init_Y,R_Init_Heading
rInvisibleMagenta
LineWidth4
//changethesevariablestochangesizeoftherectangle
RectWidth=100
RectHeight=50
gosubDrawRectangle
rTurn45//moveoutoftheway
rForward40
End
//======================================================
DrawRectangle:
rPenDown//startdrawing
forI=1to2
rturn90
rForwardRectWidth
rTurn90
rForwardRectHeight
next

rPenUp//stopdrawing
Return
//======================================================
FIGURE 8.2 Drawing rectangles.

8.1.3 Drawing Triangles


To draw a triangle we need to know the angles and sides of the triangles. You can define a
triangle by two sides and their included angle, or by all the angles, or by all the sides, or by two
angles and a side, and so on. You need three parameters that can be any combination of sides
and angles. This implies that there are eight possible combinations. The ultimate outcome
though, is to be able to calculate the lengths of the three sides and the corresponding three
angles. Once the lengths of the sides and angles are known the robot can draw a triangle by
moving the length of the first side, turning 180 minus the angle, then moving the length of the
next side, turning again 180 minus the angle, and then finally moving the last length. The
subroutine (Lines 2128) in Fig. 8.4 shows this.

FIGURE 8.3 Isosceles triangle

02 MainProgram:
03
//----change these to make the triangle have

04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

//----different orientation and position


R_Init_X = 400
R_Init_Y = 300
R_Init_Heading = -90
rLocate R_Init_X,R_Init_Y,R_Init_Heading
rInvisible Magenta
LineWidth 4
//---change these values
Side = 150
Angle = 50
Data Sides;2*Side*cos(Angle*pi(1)/180),Side,Side
Data Angles;Angle,180-2*Angle,Angle
gosub DrawTriangle
rTurn -45 //--move out of the way
rForward 40

End
//======================================================
DrawTriangle:
rPen Down //--start drawing
For i = 1 to 3
rForward Sides[i-1]
rTurn 180-Angles[i-1]
next
rPen Up //--stop drawing
Return
//======================================================

FIGURE 8.4 Drawing an isosceles triangle.

The Data statements on Lines 14 and 15 create two arrays that hold the sides and angles.

8.1.4 Drawing Any Shape


One way to achieve this easily is to use the Data command to create an array of data pairs where
each pair represents a command for the robot. The first element of the pair specifies an action to
be taken. The second element provides a parameter associated with the action.
Example 1

01
02
03
04
05
06
07
08
09
10

//---Draw Any Shape


MainProgram:
//----change these to make the shape have
//----different orientation and position
R_Init_X = 400
R_Init_Y = 300
R_Init_Heading = 90
rLocate R_Init_X,R_Init_Y,R_Init_Heading
rInvisible Magenta
LineWidth 4

14 for I = 1 to MaxDim(SomeShape,1)/2
15
J = (I-1)*2
16
if SomeShape[J] = "f" then rForward SomeShape[J+1]
17
if SomeShape[J] = "t" then rTurn SomeShape[J+1]
18
if SomeShape[J] = "p" then rPen SomeShape[J+1]
19 next
20 End
FIGURE 8.5 Drawing any shape.

8.2 ABC Robot


In the program of Fig. 8.5 we developed a program that draws any shape given an array of data
pairs. If we create shapes that are letters, we can give the robot the ability to write words.
Remove Lines 11 to 13 from the program in Fig. 8.5 and in their place put the lines in Fig. 8.6
then run the program. The principles used in this program will be extended to develop a program
that is able to accept a string and write it on the screen. The size of the font will be scalable and
the robot will write the string at any angle. But before we can do any of this, we need to develop
a font array and a way of making the robot write any letter individually. See Fig. 8.7 for a sample
output of such a program.

t = "t"
f = "f"
p = "p"
d = 10*sqrt(2)
//--A
data SomeShape;
data SomeShape;
data SomeShape;
//--B
data SomeShape;
data SomeShape;
data SomeShape;
data SomeShape;
//--C
data SomeShape;
data SomeShape;
data SomeShape;
data SomeShape;

p,down, t,-90, f,50, t,45, f,d, t,45


f,40, t,45, f,d, t,45, f,20, t,90, f,60
f,-60, t,-90, f,30, t,-90, p, up, f,15
p,down, t,-90, f,60,t,90, f,50, t,45, f,d/2
t,45, f,15, t,45, f,d/2, t,45, f,50, f,-50
t,-135, f,d, t,45, f,20, t,45, f,d/2, t,45
f,55, p,up, f,-60, t,180, f,15
t,-90, f,10, p,down, f,40, t,45, f,d, t,45, f,40
t,45, f,d, t,45, p,up, f,40, t,45, p,down, f,d
t,45, f,40, t,45, f,d, p,up, f,-d, t,-45
t,180, f,80

FIGURE 8.6 The robot can write (well, only three letters so far).

FIGURE 8.7 Writing robot.

MainProgram:
goSubCreate_Font
Scale=5
rLocate100,500,30
rInvisibleDarkGray
Message="HelloWorld"
gosubPrint_Message
rForward30
End
//======================================================
Print_Message:
Message=upper(Message)
fori=1toLength(Message)
=Ascii(SubString(Message,i,1))Ascii("A")
ifL<0orL>26thenL=26
Inst_No=0
whiletrue
Inst=Letters[L,Inst_No*2+1]
ifInst=fthenrForwardScale*Letters[L,Inst_No*2+2]
ifInst=tthenrTurnLetters[L,Inst_No*2+2]
ifInst=pthenrPenLetters[L,Inst_No*2+2]
ifInst=ethenbreak
Inst_No=Inst_No+1
wend
rForwardScale
next
Return
FIGURE 8.8 Letter-writing program.

Create_Font:
f = 1
t = 2
p = 3
e = -1000
d = sqrt(2)
//------------Fonts
data letters; Char(Ascii("A")+26), f,6, e
data letters; "A", p,down, t,-90, f,5, t,45, f,d, t,45
data letters; f,4, t,45, f,d, t,45, f,2, t,90, f,6
data letters; f,-6, t,-90, f,3, t,-90, p,up, e
data letters; "B", p,down, t,-90, f,6, t,90, f,4, t,45, f,d
data letters; t,45, f,1, t,90, f,5, f,-5
data letters; t,-135, f,d, t,45, f,2, t,45, f,d, t,45
data letters; f,5, p,up, f,-6, t,180, e
data letters; "C", t,-90, f,1, p,down, f,4, t,45, f,d, t,45
data letters; f,4, t,45, f,d, t,45, p,up, f,4, t,45, p,down
data letters; f,d, t,45, f,4, t,45, f,d, p,up, f,-d, t,135
data letters; f,5, e
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data

letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;

"D", p,down, t,-90, f,6, t,90, f,4, t,45, f,d*2


t,45, f,2, t,45, f,d*2, t,45, f,4, t,180, p,up
f,6, e
"E", p,down, f,6, f,-6, t,-90, f,3, t,90, f,4
f,-4, t,-90, f,3, t,90, f,6, t,90, p,up, f,6
t,-90, e
"F", p,down, t,-90, f,3, t,90, f,4, f,-4
t,-90, f,3, t,90, f,6, t,90, p,up, f,6, t,-90, e
"G", t,-90, f,1, p,down, f,4, t,45, f,d, t,45
f,3, t,45, f,d, p,up, f,d, t,45, f,2, t,90
p,down, f,2, f,-1, t,-90, f,1, t,45, f,d, t,45
f,3, t,45, f,d, p,up, f,-d, t,135, f,5, e
"H", p,down, t,-90, f,6, f,-3, t,90, f,6, t,-90
f,3, f,-6, t,90, p,up, e
"I", p,down, f,3, t,-90, f,6, t,-90, f,3, f,-6
t,-90, p,up, f,6, t,90, p,down, f,3, f,-3, p,up
t,180, e
"J", t,-90, f,1, t,135, p,down, f,d, t,-45
f,2, t,-45, f,d, t,-45, f,5, t,-90, f,2, f,-4
p,up, t,-90, f,6, t,-90, e
"K", p,down, t,-90, f,6, f,-3, t,63, f,6.6
f,-4.6, t,90, f,4.5, f,-4.5, p,up, t,-90
f,-2, t,-63, f,-3, t,90, f,6, e
"L", t,-90, f,1, p,down, f,5, f,-5, t,135
f,d, t,-45, f,4, t,-45, f,d, t,135, p,up
f,1, t,-90, e
"M", t,-90, p,down, f,6, t,135, f,3*d, t,-90
f,3*d, t,135, f,6, t,-90, p,up, e
"N", t,-90, p,down, f,6, t,135, f,6*d, t,-135
f,6, p,up, f,-6, t,90, e
"O", t,-90, f,1, p,down, f,4, t,45, f,d, t,45
f,4, t,45, f,d, t,45, f,4, t,45, f,d, t,45
f,4, t,45, f,d, f,-d, p,up, t,135, f,5, e
"P", t,-90, p,down, f,6, t,90, f,4, t,45, f,d
t,45, f,1, t,45, f,d, t,45, f,4, p,up, f,-6
t,-90, f,3, t,-90, e

data
data
data
data
data

letters;
letters;
letters;
letters;
letters;

"Q", t,-90, f,1, p,down, f,4, t,45, f,d, t,45


f,4, t,45, f,d, t,45, f,4, t,45, f,d, t,45
f,4, t,45, f,d, f,-d, p,up, t,135, f,5, t,-135
p,down, f,d, p,up, f,-d, t,135, e
"R", t,-90, p,down, f,6, t,90, f,4, t,45, f,d

FIGURE Create_Font subroutine.

Anda mungkin juga menyukai