File: mn23.html

package info (click to toggle)
tex4ht 19990804-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,384 kB
  • ctags: 799
  • sloc: ansic: 7,653; makefile: 116; sh: 24
file content (170 lines) | stat: -rw-r--r-- 8,873 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<html  
><head><!--mn23.html from mn.tex (TeX4ht, 1999-03-31 04:46:00)--> 
<title>source</title><link 
rel="stylesheet" type="text/css" href="mn.css"></head><body 
>
      <DIV CLASS="ShowCode"><PRE>
      &nbsp;         <SPAN CLASS="cmtt-10">%-- try.tex --</SPAN>
      &nbsp;  <SPAN CLASS="cmtt-10">\documentclass{article}</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">\usepackage{tex4ht}</SPAN>
      &nbsp;  <SPAN CLASS="cmtt-10">\begin{document}</SPAN>
      &nbsp;
      &nbsp;
      &nbsp;<SPAN CLASS="cmtt-10">\input  ProTex.sty</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">\AlProTex{java,&lt;&lt;&lt;&gt;&gt;&gt;,list,title,`}</SPAN>
      &nbsp;
      &nbsp;
      &nbsp;<SPAN CLASS="cmtt-10">\HCode{&lt;APPLET CODE=&#34;turtle.class&#34; WIDTH=&#34;300&#34; HEIGHT=&#34;300&#34;</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">&gt; &lt;/APPLET&gt;}</SPAN>
      &nbsp;
      &nbsp;
      &nbsp;<SPAN CLASS="cmtt-10">\HPage{literate code}</SPAN>
      &nbsp;
      &nbsp;<SPAN CLASS="cmtt-10">A mini-turtle program.</SPAN>
      &nbsp;
      &nbsp;<SPAN CLASS="cmtt-10">\&lt;turtle\&gt;&lt;&lt;&lt;</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">import java.applet.*;</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">import java.awt.*;</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">public class turtle extends Applet</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">{</SPAN>
      &nbsp;
      &nbsp;   <SPAN CLASS="cmtt-10">Graphics g;</SPAN>
      &nbsp;   <SPAN CLASS="cmtt-10">`&lt;vars`&gt;</SPAN>
      &nbsp;   <SPAN CLASS="cmtt-10">`&lt;turtle functions`&gt;</SPAN>
      &nbsp;   <SPAN CLASS="cmtt-10">public void init()</SPAN>
      &nbsp;   <SPAN CLASS="cmtt-10">{</SPAN>
      &nbsp;     <SPAN CLASS="cmtt-10">`&lt;initial state`&gt;</SPAN>
      &nbsp;     <SPAN CLASS="cmtt-10">`&lt;draw interface`&gt;</SPAN>
      &nbsp;   <SPAN CLASS="cmtt-10">}</SPAN>
      &nbsp;   <SPAN CLASS="cmtt-10">public void paint(Graphics g) {</SPAN>
      &nbsp;     <SPAN CLASS="cmtt-10">this.g = g;</SPAN>
      &nbsp;     <SPAN CLASS="cmtt-10">`&lt;execute commands`&gt;</SPAN>
      &nbsp;   <SPAN CLASS="cmtt-10">}</SPAN>
      &nbsp;   <SPAN CLASS="cmtt-10">public boolean action(Event e,Object o)</SPAN>
      &nbsp;   <SPAN CLASS="cmtt-10">{</SPAN>
      &nbsp;     <SPAN CLASS="cmtt-10">`&lt;get command`&gt;</SPAN>
      &nbsp;     <SPAN CLASS="cmtt-10">repaint();</SPAN>
      &nbsp;     <SPAN CLASS="cmtt-10">return true;</SPAN>
      &nbsp;   <SPAN CLASS="cmtt-10">}</SPAN>
      &nbsp;
      &nbsp;<SPAN CLASS="cmtt-10">}</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">&gt;&gt;&gt;</SPAN>
      &nbsp;
      &nbsp;
      &nbsp;<SPAN CLASS="cmtt-10">The turtle can move forward without trace, move forward</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">while tracing its path, and rotate.</SPAN>
      &nbsp;
      &nbsp;
      &nbsp;<SPAN CLASS="cmtt-10">\&lt;turtle functions\&gt;&lt;&lt;&lt;</SPAN>
      &nbsp;   <SPAN CLASS="cmtt-10">void Move(int x, int y){ this.x += x;  this.y -= y; }</SPAN>
      &nbsp;
      &nbsp;   <SPAN CLASS="cmtt-10">void Move(int r){ x += (int) (r * Math.cos(d));</SPAN>
      &nbsp;                     <SPAN CLASS="cmtt-10">y -= (int) (r * Math.sin(d)); }</SPAN>
      &nbsp;
      &nbsp;   <SPAN CLASS="cmtt-10">void Line(int x, int y){</SPAN>
      &nbsp;      <SPAN CLASS="cmtt-10">g.drawLine( this.x, this.y,  this.x+x, this.y-y );</SPAN>
      &nbsp;      <SPAN CLASS="cmtt-10">Move(x,y);</SPAN>
      &nbsp;   <SPAN CLASS="cmtt-10">}</SPAN>
      &nbsp;   <SPAN CLASS="cmtt-10">void Line(int r){</SPAN>
      &nbsp;    <SPAN CLASS="cmtt-10">Line(   (new Double(r * Math.cos(d))).intValue(),</SPAN>
      &nbsp;            <SPAN CLASS="cmtt-10">(new Double( r * Math.sin(d) )).intValue() ); }</SPAN>
      &nbsp;
      &nbsp;   <SPAN CLASS="cmtt-10">void Rotate(int d){</SPAN>
      &nbsp;      <SPAN CLASS="cmtt-10">this.d += d * 3.14  / 180;</SPAN>
      &nbsp;      <SPAN CLASS="cmtt-10">while( 6.28 &lt; this.d ){ this.d -= 6.28; }</SPAN>
      &nbsp;      <SPAN CLASS="cmtt-10">while( this.d &lt; 0 ){ this.d += 6.28; }</SPAN>
      &nbsp;   <SPAN CLASS="cmtt-10">}</SPAN>
      &nbsp;
      &nbsp;<SPAN CLASS="cmtt-10">&gt;&gt;&gt;</SPAN>
      &nbsp;
      &nbsp;
      &nbsp;<SPAN CLASS="cmtt-10">The text field may be modified by the users.</SPAN>
      &nbsp;
      &nbsp;<SPAN CLASS="cmtt-10">\&lt;vars\&gt;&lt;&lt;&lt;</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">Button moveButton,</SPAN>
      &nbsp;       <SPAN CLASS="cmtt-10">lineButton,</SPAN>
      &nbsp;       <SPAN CLASS="cmtt-10">clearButton,</SPAN>
      &nbsp;       <SPAN CLASS="cmtt-10">rotateButton;</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">TextField in;</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">&gt;&gt;&gt;</SPAN>
      &nbsp;
      &nbsp;
      &nbsp;<SPAN CLASS="cmtt-10">\&lt;draw interface\&gt;&lt;&lt;&lt;</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">clearButton  = new Button(&#34;Clear&#34;);   add(clearButton);</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">moveButton   = new Button(&#34;Move&#34;);    add(moveButton);</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">lineButton   = new Button(&#34;Line&#34;);    add(lineButton);</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">rotateButton = new Button(&#34;Rotate&#34;);  add(rotateButton);</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">in           = new TextField(3);      add(in);</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">in.setText( &#34;50&#34; );</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">&gt;&gt;&gt;</SPAN>
      &nbsp;
      &nbsp;
      &nbsp;<SPAN CLASS="cmtt-10">\&lt;execute commands\&gt;&lt;&lt;&lt;</SPAN>
      &nbsp;      <SPAN CLASS="cmtt-10">int i;</SPAN>
      &nbsp;  <SPAN CLASS="cmtt-10">x = 150; y = 150; d = 0;</SPAN>
      &nbsp;  <SPAN CLASS="cmtt-10">for( i=0; i&lt;N; i+=2 ){</SPAN>
      &nbsp;
      &nbsp;     <SPAN CLASS="cmtt-10">switch( command[i] ) {</SPAN>
      &nbsp;
      &nbsp;<SPAN CLASS="cmtt-10">case   `&lt;Line op`&gt;:{    Line( command[i+1] ); break; }</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">case   `&lt;Move op`&gt;:{    Move( command[i+1] ); break; }</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">case `&lt;Rotate op`&gt;:{  Rotate( command[i+1] ); break; }</SPAN>
      &nbsp;
      &nbsp;<SPAN CLASS="cmtt-10">}</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">}</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">&gt;&gt;&gt;</SPAN>
      &nbsp;
      &nbsp;<SPAN CLASS="cmtt-10">\&lt;get command\&gt;&lt;&lt;&lt;</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">if(&#34;Clear&#34;.equals(o)) {  `&lt;initial state`&gt;  }</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">else{</SPAN>
      &nbsp;
      &nbsp;   <SPAN CLASS="cmtt-10">if(  &#34;Line&#34;.equals(o)){  command[N]=`&lt;Line op`&gt;;   }</SPAN>
      &nbsp;   <SPAN CLASS="cmtt-10">if(  &#34;Move&#34;.equals(o)){  command[N]=`&lt;Move op`&gt;;   }</SPAN>
      &nbsp;   <SPAN CLASS="cmtt-10">if(&#34;Rotate&#34;.equals(o)){  command[N]=`&lt;Rotate op`&gt;; }</SPAN>
      &nbsp;
      &nbsp;   <SPAN CLASS="cmtt-10">String s =  new String( in.getText() );</SPAN>
      &nbsp;   <SPAN CLASS="cmtt-10">Integer i = new  Integer(     s  );</SPAN>
      &nbsp;   <SPAN CLASS="cmtt-10">command[N+1]=   i.intValue();</SPAN>
      &nbsp;   <SPAN CLASS="cmtt-10">N+=2;</SPAN>
      &nbsp;
      &nbsp;<SPAN CLASS="cmtt-10">}</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">&gt;&gt;&gt;</SPAN>
      &nbsp;
      &nbsp;<SPAN CLASS="cmtt-10">\&lt;vars\&gt;&lt;&lt;&lt;</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">int command[] = new int[300];</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">&gt;&gt;&gt;</SPAN>
      &nbsp;
      &nbsp;<SPAN CLASS="cmtt-10">\&lt;Line op\&gt;&lt;&lt;&lt;</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">1</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">&gt;&gt;&gt;</SPAN>
      &nbsp;
      &nbsp;<SPAN CLASS="cmtt-10">\&lt;Move op\&gt;&lt;&lt;&lt;</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">2</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">&gt;&gt;&gt;</SPAN>
      &nbsp;
      &nbsp;<SPAN CLASS="cmtt-10">\&lt;Rotate op\&gt;&lt;&lt;&lt;</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">3</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">&gt;&gt;&gt;</SPAN>
      &nbsp;
      &nbsp;
      &nbsp;<SPAN CLASS="cmtt-10">\&lt;initial state\&gt;&lt;&lt;&lt;</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">N = 0;</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">&gt;&gt;&gt;</SPAN>
      &nbsp;
      &nbsp;<SPAN CLASS="cmtt-10">\&lt;vars\&gt;&lt;&lt;&lt;</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">int N;</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">int x, y;</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">double d;</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">&gt;&gt;&gt;</SPAN>
      &nbsp;
      &nbsp;
      &nbsp;<SPAN CLASS="cmtt-10">\OutputCode\&lt;turtle\&gt;     \Needs{&#34;javac turtle.java&#34;}</SPAN>
      &nbsp;
      &nbsp;<SPAN CLASS="cmtt-10">\EndHPage{}</SPAN>
      &nbsp;<SPAN CLASS="cmtt-10">\end{document}</SPAN>
</PRE></DIV>
     
</body> 
</html>