-
Notifications
You must be signed in to change notification settings - Fork 0
/
first-sixth-chapter-exercise.html
319 lines (270 loc) · 24.3 KB
/
first-sixth-chapter-exercise.html
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<!DOCTYPE html>
<html lang="en"
>
<head>
<title>First (Sixth Chapter) Exercise - Computer Science Notes</title>
<!-- Using the latest rendering mode for IE -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="./first-sixth-chapter-exercise.html">
<meta name="author" content="Sergio Ugalde Marcano" />
<meta name="keywords" content="Fifth-Semester" />
<meta name="description" content="This program simulates an object steering randomly." />
<meta property="og:site_name" content="Computer Science Notes" />
<meta property="og:type" content="article"/>
<meta property="og:title" content="First (Sixth Chapter) Exercise"/>
<meta property="og:url" content="./first-sixth-chapter-exercise.html"/>
<meta property="og:description" content="This program simulates an object steering randomly."/>
<meta property="article:published_time" content="2015-10-18" />
<meta property="article:section" content="Modelling" />
<meta property="article:tag" content="Fifth-Semester" />
<meta property="article:author" content="Sergio Ugalde Marcano" />
<!-- Bootstrap -->
<link rel="stylesheet" href="./theme/css/bootstrap.min.css" type="text/css"/>
<link href="./theme/css/font-awesome.min.css" rel="stylesheet">
<link href="./theme/css/pygments/friendly.css" rel="stylesheet">
<link href="./theme/css/typogrify.css" rel="stylesheet">
<link rel="stylesheet" href="./theme/css/style.css" type="text/css"/>
</head>
<body>
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="./" class="navbar-brand">
Computer Science Notes </a>
</div>
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav">
<li><a href="./pages/about.html">
About
</a></li>
<li class="active">
<a href="./category/modelling.html">Modelling</a>
</li>
<li >
<a href="./category/parcial_1.html">Parcial_1</a>
</li>
<li >
<a href="./category/parcial_2.html">Parcial_2</a>
</li>
<li >
<a href="./category/parcial_3.html">Parcial_3</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="./archives.html"><i class="fa fa-th-list"></i><span class="icon-label">Archives</span></a></li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
</div> <!-- /.navbar -->
<!-- Banner -->
<!-- End Banner -->
<div class="container">
<div class="row">
<div class="col-sm-9">
<section id="content">
<article>
<header class="page-header">
<h1>
<a href="./first-sixth-chapter-exercise.html"
rel="bookmark"
title="Permalink to First (Sixth Chapter) Exercise">
First (Sixth Chapter) Exercise
</a>
</h1>
</header>
<div class="entry-content">
<div class="panel">
<div class="panel-body">
<footer class="post-info">
<span class="label label-default">Date</span>
<span class="published">
<i class="fa fa-calendar"></i><time datetime="2015-10-18T00:00:00-05:00"> Sun 18 October 2015</time>
</span>
<span class="label label-default">Tags</span>
<a href="./tag/fifth-semester.html">Fifth-Semester</a>
</footer><!-- /.post-info --> </div>
</div>
<h2 id="experience">Experience</h2>
<p>By basing the code on the example (6.1) provided by the author, the modification was relatively simple, as we only required to vary the speed according to separate rules in the environment, in this case, its x coordinate. The program also draws these regions and a predicition on the object’s future position based on its current velocity. </p>
<h2 id="code">Code</h2>
<h3 id="main-class">Main class</h3>
<div class="highlight"><pre> <span class="c1">// BASED ON The Nature of Code</span>
<span class="c1">// ORIGINALY BY Daniel Shiffman</span>
<span class="c1">// http://natureofcode.com</span>
<span class="c1">// ADAPTED BY SERGIO UGALDE MARCANO </span>
<span class="c1">// Two "vehicles" follow the mouse position</span>
<span class="c1">// Implements Craig Reynold's autonomous steering behaviors</span>
<span class="c1">// One vehicle "seeks"</span>
<span class="c1">// One vehicle "arrives"</span>
<span class="c1">// See: http://www.red3d.com/cwr/</span>
<span class="n">Vehicle</span> <span class="n">v</span><span class="o">;</span>
<span class="n">PVector</span> <span class="n">follow</span><span class="o">;</span>
<span class="kt">void</span> <span class="nf">setup</span><span class="o">()</span> <span class="o">{</span>
<span class="n">size</span><span class="o">(</span><span class="mi">1000</span><span class="o">,</span> <span class="mi">1000</span><span class="o">);</span>
<span class="n">v</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Vehicle</span><span class="o">(</span><span class="n">width</span><span class="o">/</span><span class="mi">2</span><span class="o">,</span> <span class="n">height</span><span class="o">/</span><span class="mi">2</span><span class="o">);</span>
<span class="n">predict</span><span class="o">();</span>
<span class="c1">//pixelDensity(2);</span>
<span class="o">}</span>
<span class="kt">void</span> <span class="nf">predict</span><span class="o">(){</span>
<span class="n">follow</span> <span class="o">=</span> <span class="n">PVector</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="n">v</span><span class="o">.</span><span class="na">velocity</span><span class="o">,</span><span class="n">v</span><span class="o">.</span><span class="na">location</span><span class="o">);</span>
<span class="n">follow</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="n">v</span><span class="o">.</span><span class="na">velocity</span><span class="o">.</span><span class="na">mult</span><span class="o">(</span><span class="mi">100</span><span class="o">));</span>
<span class="o">}</span>
<span class="kt">void</span> <span class="nf">draw</span><span class="o">()</span> <span class="o">{</span>
<span class="n">background</span><span class="o">(</span><span class="mi">255</span><span class="o">);</span>
<span class="n">predict</span><span class="o">();</span>
<span class="c1">// Draw an ellipse at the mouse location</span>
<span class="n">fill</span><span class="o">(</span><span class="mi">125</span><span class="o">);</span>
<span class="n">stroke</span><span class="o">(</span><span class="mi">0</span><span class="o">);</span>
<span class="n">strokeWeight</span><span class="o">(</span><span class="mi">2</span><span class="o">);</span>
<span class="c1">//ellipse(follow.x, follow.y, 48, 48);</span>
<span class="n">v</span><span class="o">.</span><span class="na">display</span><span class="o">(</span><span class="n">follow</span><span class="o">.</span><span class="na">x</span><span class="o">,</span> <span class="n">follow</span><span class="o">.</span><span class="na">y</span><span class="o">);</span>
<span class="n">fill</span><span class="o">(</span><span class="mi">125</span><span class="o">,</span><span class="mi">0</span><span class="o">,</span><span class="mi">0</span><span class="o">,</span><span class="mi">50</span><span class="o">);</span>
<span class="n">rect</span><span class="o">(</span><span class="mi">0</span><span class="o">,</span><span class="mi">0</span><span class="o">,</span><span class="n">width</span><span class="o">/</span><span class="mi">3</span><span class="o">,</span><span class="n">height</span><span class="o">);</span>
<span class="n">fill</span><span class="o">(</span><span class="mi">0</span><span class="o">,</span><span class="mi">125</span><span class="o">,</span><span class="mi">0</span><span class="o">,</span><span class="mi">50</span><span class="o">);</span>
<span class="n">rect</span><span class="o">(</span><span class="n">width</span><span class="o">/</span><span class="mi">3</span><span class="o">,</span><span class="mi">0</span><span class="o">,</span><span class="mi">2</span><span class="o">*</span><span class="n">width</span><span class="o">/</span><span class="mi">3</span><span class="o">,</span><span class="n">height</span><span class="o">);</span>
<span class="n">fill</span><span class="o">(</span><span class="mi">125</span><span class="o">,</span><span class="mi">125</span><span class="o">,</span><span class="mi">0</span><span class="o">,</span><span class="mi">50</span><span class="o">);</span>
<span class="n">rect</span><span class="o">(</span><span class="mi">2</span><span class="o">*</span><span class="n">width</span><span class="o">/</span><span class="mi">3</span><span class="o">,</span><span class="mi">0</span><span class="o">,</span><span class="n">width</span><span class="o">/</span><span class="mi">3</span><span class="o">,</span><span class="n">height</span><span class="o">);</span>
<span class="c1">// Call the appropriate steering behaviors for our agents</span>
<span class="n">v</span><span class="o">.</span><span class="na">seek</span><span class="o">(</span><span class="n">follow</span><span class="o">);</span>
<span class="n">v</span><span class="o">.</span><span class="na">update</span><span class="o">();</span>
<span class="n">v</span><span class="o">.</span><span class="na">checkEdges</span><span class="o">();</span>
<span class="n">fill</span><span class="o">(</span><span class="mi">100</span><span class="o">);</span>
<span class="n">v</span><span class="o">.</span><span class="na">display</span><span class="o">(</span><span class="n">v</span><span class="o">.</span><span class="na">location</span><span class="o">.</span><span class="na">x</span><span class="o">,</span> <span class="n">v</span><span class="o">.</span><span class="na">location</span><span class="o">.</span><span class="na">y</span><span class="o">);</span>
<span class="o">}</span>
</pre></div>
<h3 id="vehicle-class">Vehicle Class</h3>
<div class="highlight"><pre> <span class="c1">// BASED ON The Nature of Code</span>
<span class="c1">// ORIGINALY BY Daniel Shiffman</span>
<span class="c1">// http://natureofcode.com</span>
<span class="c1">// ADAPTED BY SERGIO UGALDE MARCANO </span>
<span class="c1">// Seek_Arrive</span>
<span class="c1">// The "Vehicle" class</span>
<span class="kd">class</span> <span class="nc">Vehicle</span> <span class="o">{</span>
<span class="n">PVector</span> <span class="n">location</span><span class="o">;</span>
<span class="n">PVector</span> <span class="n">velocity</span><span class="o">;</span>
<span class="n">PVector</span> <span class="n">acceleration</span><span class="o">;</span>
<span class="kt">float</span> <span class="n">r</span><span class="o">;</span>
<span class="kt">float</span> <span class="n">maxforce</span><span class="o">;</span> <span class="c1">// Maximum steering force</span>
<span class="kt">float</span> <span class="n">maxspeed</span><span class="o">;</span> <span class="c1">// Maximum speed</span>
<span class="n">Vehicle</span><span class="o">(</span><span class="kt">float</span> <span class="n">x</span><span class="o">,</span> <span class="kt">float</span> <span class="n">y</span><span class="o">)</span> <span class="o">{</span>
<span class="n">acceleration</span> <span class="o">=</span> <span class="k">new</span> <span class="n">PVector</span><span class="o">(</span><span class="mi">0</span><span class="o">,</span><span class="mi">0</span><span class="o">);</span>
<span class="n">velocity</span> <span class="o">=</span> <span class="k">new</span> <span class="n">PVector</span><span class="o">(</span><span class="mi">1</span><span class="o">,-</span><span class="mi">2</span><span class="o">);</span>
<span class="n">location</span> <span class="o">=</span> <span class="k">new</span> <span class="n">PVector</span><span class="o">(</span><span class="n">x</span><span class="o">,</span><span class="n">y</span><span class="o">);</span>
<span class="n">r</span> <span class="o">=</span> <span class="mi">6</span><span class="o">;</span>
<span class="n">maxspeed</span> <span class="o">=</span> <span class="mi">3</span><span class="o">;</span>
<span class="n">maxforce</span> <span class="o">=</span> <span class="mf">0.1</span><span class="o">;</span>
<span class="o">}</span>
<span class="kt">void</span> <span class="nf">calculate</span><span class="o">(){</span>
<span class="k">if</span> <span class="o">(</span><span class="n">location</span><span class="o">.</span><span class="na">x</span><span class="o"><=</span><span class="n">width</span><span class="o">/</span><span class="mi">3</span><span class="o">){</span>
<span class="n">maxspeed</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span>
<span class="n">maxforce</span> <span class="o">=</span> <span class="mf">0.01</span><span class="o">;</span>
<span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="n">location</span><span class="o">.</span><span class="na">x</span><span class="o"><=</span><span class="mi">2</span><span class="o">*</span><span class="n">width</span><span class="o">/</span><span class="mi">3</span><span class="o">){</span>
<span class="n">maxspeed</span> <span class="o">=</span> <span class="mi">3</span><span class="o">;</span>
<span class="n">maxforce</span> <span class="o">=</span> <span class="mf">0.1</span><span class="o">;</span>
<span class="o">}</span><span class="k">else</span> <span class="o">{</span>
<span class="n">maxspeed</span> <span class="o">=</span> <span class="mi">5</span><span class="o">;</span>
<span class="n">maxforce</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="c1">// Method to update location</span>
<span class="kt">void</span> <span class="nf">update</span><span class="o">()</span> <span class="o">{</span>
<span class="n">calculate</span><span class="o">();</span>
<span class="c1">// Update velocity</span>
<span class="n">velocity</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="n">acceleration</span><span class="o">);</span>
<span class="c1">// Limit speed</span>
<span class="n">velocity</span><span class="o">.</span><span class="na">limit</span><span class="o">(</span><span class="n">maxspeed</span><span class="o">);</span>
<span class="n">location</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="n">velocity</span><span class="o">);</span>
<span class="c1">// Reset accelerationelertion to 0 each cycle</span>
<span class="n">acceleration</span><span class="o">.</span><span class="na">mult</span><span class="o">(</span><span class="mi">0</span><span class="o">);</span>
<span class="o">}</span>
<span class="kt">void</span> <span class="nf">applyForce</span><span class="o">(</span><span class="n">PVector</span> <span class="n">force</span><span class="o">)</span> <span class="o">{</span>
<span class="c1">// We could add mass here if we want A = F / M</span>
<span class="n">acceleration</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="n">force</span><span class="o">);</span>
<span class="o">}</span>
<span class="c1">// A method that calculates a steering force towards a target</span>
<span class="c1">// STEER = DESIRED MINUS VELOCITY</span>
<span class="kt">void</span> <span class="nf">seek</span><span class="o">(</span><span class="n">PVector</span> <span class="n">target</span><span class="o">)</span> <span class="o">{</span>
<span class="n">PVector</span> <span class="n">desired</span> <span class="o">=</span> <span class="n">PVector</span><span class="o">.</span><span class="na">sub</span><span class="o">(</span><span class="n">target</span><span class="o">,</span><span class="n">location</span><span class="o">);</span> <span class="c1">// A vector pointing from the location to the target</span>
<span class="c1">// Scale to maximum speed</span>
<span class="n">desired</span><span class="o">.</span><span class="na">setMag</span><span class="o">(</span><span class="n">maxspeed</span><span class="o">);</span>
<span class="c1">// Steering = Desired minus velocity</span>
<span class="n">PVector</span> <span class="n">steer</span> <span class="o">=</span> <span class="n">PVector</span><span class="o">.</span><span class="na">sub</span><span class="o">(</span><span class="n">desired</span><span class="o">,</span><span class="n">velocity</span><span class="o">);</span>
<span class="n">steer</span><span class="o">.</span><span class="na">limit</span><span class="o">(</span><span class="n">maxforce</span><span class="o">);</span> <span class="c1">// Limit to maximum steering force</span>
<span class="n">applyForce</span><span class="o">(</span><span class="n">steer</span><span class="o">);</span>
<span class="o">}</span>
<span class="kt">void</span> <span class="nf">checkEdges</span><span class="o">(){</span>
<span class="k">if</span> <span class="o">(</span><span class="n">location</span><span class="o">.</span><span class="na">x</span><span class="o">></span><span class="n">width</span><span class="o">)</span><span class="n">location</span><span class="o">.</span><span class="na">x</span><span class="o">=</span><span class="mi">0</span><span class="o">;</span>
<span class="k">if</span> <span class="o">(</span><span class="n">location</span><span class="o">.</span><span class="na">y</span><span class="o">></span><span class="n">height</span><span class="o">)</span><span class="n">location</span><span class="o">.</span><span class="na">y</span><span class="o">=</span><span class="mi">0</span><span class="o">;</span>
<span class="k">if</span> <span class="o">(</span><span class="n">location</span><span class="o">.</span><span class="na">x</span><span class="o"><</span><span class="mi">0</span><span class="o">)</span><span class="n">location</span><span class="o">.</span><span class="na">x</span><span class="o">=</span><span class="n">width</span><span class="o">;</span>
<span class="k">if</span> <span class="o">(</span><span class="n">location</span><span class="o">.</span><span class="na">y</span><span class="o"><</span><span class="mi">0</span><span class="o">)</span><span class="n">location</span><span class="o">.</span><span class="na">y</span><span class="o">=</span><span class="n">height</span><span class="o">;</span>
<span class="o">}</span>
<span class="kt">void</span> <span class="nf">display</span><span class="o">(</span><span class="kt">float</span> <span class="n">x</span><span class="o">,</span> <span class="kt">float</span> <span class="n">y</span><span class="o">)</span> <span class="o">{</span>
<span class="c1">// Draw a triangle rotated in the direction of velocity</span>
<span class="kt">float</span> <span class="n">theta</span> <span class="o">=</span> <span class="n">velocity</span><span class="o">.</span><span class="na">heading2D</span><span class="o">()</span> <span class="o">+</span> <span class="n">PI</span><span class="o">/</span><span class="mi">2</span><span class="o">;</span>
<span class="n">stroke</span><span class="o">(</span><span class="mi">0</span><span class="o">);</span>
<span class="n">strokeWeight</span><span class="o">(</span><span class="mi">1</span><span class="o">);</span>
<span class="n">pushMatrix</span><span class="o">();</span>
<span class="n">translate</span><span class="o">(</span><span class="n">x</span><span class="o">,</span><span class="n">y</span><span class="o">);</span>
<span class="n">rotate</span><span class="o">(</span><span class="n">theta</span><span class="o">);</span>
<span class="n">beginShape</span><span class="o">();</span>
<span class="n">vertex</span><span class="o">(</span><span class="mi">0</span><span class="o">,</span> <span class="o">-</span><span class="n">r</span><span class="o">*</span><span class="mi">2</span><span class="o">);</span>
<span class="n">vertex</span><span class="o">(-</span><span class="n">r</span><span class="o">,</span> <span class="n">r</span><span class="o">*</span><span class="mi">2</span><span class="o">);</span>
<span class="n">vertex</span><span class="o">(</span><span class="n">r</span><span class="o">,</span> <span class="n">r</span><span class="o">*</span><span class="mi">2</span><span class="o">);</span>
<span class="n">endShape</span><span class="o">(</span><span class="n">CLOSE</span><span class="o">);</span>
<span class="n">popMatrix</span><span class="o">();</span>
<span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
<!-- /.entry-content -->
</article>
</section>
</div>
<div class="col-sm-3" id="sidebar">
<aside>
<section class="well well-sm">
<ul class="list-group list-group-flush">
<li class="list-group-item"><h4><i class="fa fa-home fa-lg"></i><span class="icon-label">Social</span></h4>
<ul class="list-group" id="social">
<li class="list-group-item"><a href="https://www.facebook.com/profile.php?id=100001791786184"><i class="fa fa-facebook-square fa-lg"></i> facebook</a></li>
<li class="list-group-item"><a href="https://github.com/serch037"><i class="fa fa-github-square fa-lg"></i> github</a></li>
</ul>
</li>
<li class="list-group-item"><a href="./"><h4><i class="fa fa-tags fa-lg"></i><span class="icon-label">Tags</span></h4></a>
<ul class="list-group " id="tags">
</ul>
</li>
</ul>
</section>
</aside>
</div>
</div>
</div>
<footer>
<div class="container">
<hr>
<div class="row">
<div class="col-xs-10">© 2015 Sergio Ugalde Marcano
· Powered by <a href="https://github.com/DandyDev/pelican-bootstrap3" target="_blank">pelican-bootstrap3</a>,
<a href="http://docs.getpelican.com/" target="_blank">Pelican</a>,
<a href="http://getbootstrap.com" target="_blank">Bootstrap</a> </div>
<div class="col-xs-2"><p class="pull-right"><i class="fa fa-arrow-up"></i> <a href="#">Back to top</a></p></div>
</div>
</div>
</footer>
<script src="./theme/js/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="./theme/js/bootstrap.min.js"></script>
<!-- Enable responsive features in IE8 with Respond.js (https://github.com/scottjehl/Respond) -->
<script src="./theme/js/respond.min.js"></script>
</body>
</html>