Skip to content

Commit 0461584

Browse files
author
lauren mccarthy
committed
Merge branch 'master' of github.com:lmccart/p5.js
2 parents 6db04bb + 41c5042 commit 0461584

File tree

6 files changed

+743
-35
lines changed

6 files changed

+743
-35
lines changed

src/math/noise.js

+52-7
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,13 @@ define(function (require) {
8181
*
8282
* @method noise
8383
* @param {Number} x x-coordinate in noise space
84-
* @param {Number} [y] y-coordinate in noise space
85-
* @param {Number} [z] z-coordinate in noise space
84+
* @param {Number} y y-coordinate in noise space
85+
* @param {Number} z z-coordinate in noise space
8686
* @return {Number} Perlin noise value (between 0 and 1) at specified
8787
* coordinates
8888
* @example
8989
* <div>
90-
* <code>
91-
* var xoff = 0.0;
90+
* <code>var xoff = 0.0;
9291
*
9392
* function draw() {
9493
* background(204);
@@ -99,8 +98,7 @@ define(function (require) {
9998
* </code>
10099
* </div>
101100
* <div>
102-
* <code>
103-
* var noiseScale=0.02;
101+
* <code>var noiseScale=0.02;
104102
*
105103
* function draw() {
106104
* background(0);
@@ -215,19 +213,66 @@ define(function (require) {
215213
* @method noiseDetail
216214
* @param {Number} lod number of octaves to be used by the noise
217215
* @param {Number} falloff falloff factor for each octave
216+
* @example
217+
* <div>
218+
* <code>
219+
*
220+
* var noiseVal;
221+
* var noiseScale=0.02;
222+
*
223+
* function setup() {
224+
* createCanvas(100,100);
225+
* }
226+
*
227+
* function draw() {
228+
* background(0);
229+
* for (var y = 0; y < height; y++) {
230+
* for (var x = 0; x < width/2; x++) {
231+
* noiseDetail(2,0.2);
232+
* noiseVal = noise((mouseX+x) * noiseScale,
233+
* (mouseY+y) * noiseScale);
234+
* stroke(noiseVal*255);
235+
* point(x,y);
236+
* noiseDetail(8,0.65);
237+
* noiseVal = noise((mouseX + x + width/2) * noiseScale,
238+
* (mouseY + y) * noiseScale);
239+
* stroke(noiseVal*255);
240+
* point(x + width/2, y);
241+
* }
242+
* }
243+
* }
244+
* </code>
245+
* </div>
218246
*/
219247
p5.prototype.noiseDetail = function(lod, falloff) {
220248
if (lod>0) { perlin_octaves=lod; }
221249
if (falloff>0) { perlin_amp_falloff=falloff; }
222250
};
223251

224252
/**
225-
* TODO: NOT IMPLEMENTED YET!
226253
* Sets the seed value for <b>noise()</b>. By default, <b>noise()</b>
227254
* produces different results each time the program is run. Set the
228255
* <b>value</b> parameter to a constant to return the same pseudo-random
229256
* numbers each time the software is run.
230257
*
258+
* @method noiseSeed
259+
* @param {Number} seed the seed value
260+
* @example
261+
* <div>
262+
* <code>var xoff = 0.0;
263+
*
264+
* function setup() {
265+
* noiseSeed(99);
266+
* stroke(0, 10);
267+
* }
268+
*
269+
* function draw() {
270+
* xoff = xoff + .01;
271+
* var n = noise(xoff) * width;
272+
* line(n, 0, n, height);
273+
* }
274+
* </code>
275+
* </div>
231276
*/
232277
p5.prototype.noiseSeed = function(seed) {
233278
// Linear Congruential Generator

src/math/random.js

+87-7
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,27 @@ define(function (require) {
4141
};
4242
}());
4343

44+
/**
45+
* Sets the seed value for random().
46+
*
47+
* By default, random() produces different results each time the program
48+
* is run. Set the seed parameter to a constant to return the same
49+
* pseudo-random numbers each time the software is run.
50+
*
51+
* @method randomSeed
52+
* @param {Number} seed the seed value
53+
* @example
54+
* <div>
55+
* <code>
56+
* randomSeed(99);
57+
* for (var i=0; i < 100; i++) {
58+
* var r = random(0, 255);
59+
* stroke(r);
60+
* line(i, 0, i, 100);
61+
* }
62+
* </code>
63+
* </div>
64+
*/
4465
p5.prototype.randomSeed = function(seed) {
4566
lcg.setSeed(seed);
4667
seeded = true;
@@ -56,9 +77,35 @@ define(function (require) {
5677
* inclusive.
5778
*
5879
* @method random
59-
* @param {x} min the lower bound
60-
* @param {y} max the upper bound
61-
* @return {Number} the random number
80+
* @param {Number} min the lower bound
81+
* @param {Number} max the upper bound
82+
* @return {Number} the random number
83+
* @example
84+
* <div>
85+
* <code>
86+
* for (var i = 0; i < 100; i++) {
87+
* var r = random(50);
88+
* stroke(r*5);
89+
* line(50, i, 50+r, i);
90+
* }
91+
* </code>
92+
* </div>
93+
* <div>
94+
* <code>
95+
* for (var i = 0; i < 100; i++) {
96+
* var r = random(-50, 50);
97+
* line(50,i,50+r,i);
98+
* }
99+
* </code>
100+
* </div>
101+
* <div>
102+
* <code>
103+
* // Get a random element from an array
104+
* var words = [ "apple", "bear", "cat", "dog" ];
105+
* var index = floor(random(words.length)); // Convert to integer
106+
* text(words[index],10,50); // Displays one of the four words
107+
* </code>
108+
* </div>
62109
*/
63110
p5.prototype.random = function (min, max) {
64111

@@ -101,10 +148,43 @@ define(function (require) {
101148
* If two args, first is mean, second is standard deviation
102149
*
103150
* @method randomGaussian
104-
* @param {mean} mean
105-
* @param {sd} standard deviation
106-
* @return {Number} the random number
107-
*/
151+
* @param {Number} mean the mean
152+
* @param {Number} sd the standard deviation
153+
* @return {Number} the random number
154+
* @example
155+
* <div>
156+
* <code>for (var y = 0; y < 100; y++) {
157+
* var x = randomGaussian(50,15);
158+
* line(50, y, x, y);
159+
*}
160+
* </code>
161+
* </div>
162+
* <div>
163+
* <code>
164+
*var distribution = new Array(360);
165+
*
166+
*function setup() {
167+
* createCanvas(100, 100);
168+
* for (var i = 0; i < distribution.length; i++) {
169+
* distribution[i] = floor(randomGaussian(0,15));
170+
* }
171+
*}
172+
*
173+
*function draw() {
174+
* background(204);
175+
*
176+
* translate(width/2, width/2);
177+
*
178+
* for (var i = 0; i < distribution.length; i++) {
179+
* rotate(TWO_PI/distribution.length);
180+
* stroke(0);
181+
* var dist = abs(distribution[i]);
182+
* line(0, 0, dist, 0);
183+
* }
184+
*}
185+
* </code>
186+
* </div>
187+
*/
108188
var y2;
109189
var previous = false;
110190
p5.prototype.randomGaussian = function(mean, sd) {

src/shape/2d_primitives.js

+45-9
Original file line numberDiff line numberDiff line change
@@ -170,17 +170,30 @@ define(function (require) {
170170
* the stroke() function. A line cannot be filled, therefore the fill()
171171
* function will not affect the color of a line. 2D lines are drawn with a
172172
* width of one pixel by default, but this can be changed with the
173-
* strokeWeight() function. The version with six parameters allows the line
174-
* to be placed anywhere within XYZ space. Drawing this shape in 3D with the
175-
* z parameter requires the P3D parameter in combination with size() as shown
176-
* in the above example.
173+
* strokeWeight() function.
177174
*
178175
* @method line
179176
* @param {Number} x1 the x-coordinate of the first point
180177
* @param {Number} y1 the y-coordinate of the first point
181178
* @param {Number} x2 the x-coordinate of the second point
182179
* @param {Number} y2 the y-coordinate of the second point
183180
* @return {p5} the p5 object
181+
* @example
182+
* <div>
183+
* <code>
184+
* line(30, 20, 85, 75);
185+
* </code>
186+
* </div>
187+
*
188+
* <div>
189+
* <code>
190+
* line(30, 20, 85, 20);
191+
* stroke(126);
192+
* line(85, 20, 85, 75);
193+
* stroke(255);
194+
* line(85, 75, 30, 75);
195+
* </code>
196+
* </div>
184197
*/
185198
p5.prototype.line = function(x1, y1, x2, y2) {
186199
var ctx = this.canvas.getContext('2d');
@@ -198,15 +211,21 @@ define(function (require) {
198211
/**
199212
* Draws a point, a coordinate in space at the dimension of one pixel.
200213
* The first parameter is the horizontal value for the point, the second
201-
* value is the vertical value for the point, and the optional third value is
202-
* the depth value. Drawing this shape in 3D with the z parameter requires
203-
* the P3D parameter in combination with size() as shown in the above
204-
* example.
214+
* value is the vertical value for the point.
205215
*
206216
* @method point
207217
* @param {Number} x the x-coordinate
208218
* @param {Number} y the y-coordinate
209219
* @return {p5} the p5 object
220+
* @example
221+
* <div>
222+
* <code>
223+
* point(30, 20);
224+
* point(85, 20);
225+
* point(85, 75);
226+
* point(30, 75);
227+
* </code>
228+
* </div>
210229
*/
211230
p5.prototype.point = function(x, y) {
212231
var ctx = this.canvas.getContext('2d');
@@ -255,6 +274,12 @@ define(function (require) {
255274
* @param {type} x4 the x-coordinate of the fourth point
256275
* @param {type} y4 the y-coordinate of the fourth point
257276
* @return {p5} the p5 object
277+
* @example
278+
* <div>
279+
* <code>
280+
* quad(38, 31, 86, 20, 69, 63, 30, 76);
281+
* </code>
282+
* </div>
258283
*/
259284
p5.prototype.quad = function(x1, y1, x2, y2, x3, y3, x4, y4) {
260285
var ctx = this.canvas.getContext('2d');
@@ -283,7 +308,12 @@ define(function (require) {
283308
* @param {Number} c width of the rectangle
284309
* @param {Number} d height of the rectangle
285310
* @return {p5} the p5 object
286-
*
311+
* @example
312+
* <div>
313+
* <code>
314+
* rect(30, 20, 55, 55);
315+
* </code>
316+
* </div>
287317
*/
288318
p5.prototype.rect = function(a, b, c, d) {
289319
var vals = canvas.modeAdjust(a, b, c, d, this._rectMode);
@@ -309,6 +339,12 @@ define(function (require) {
309339
* @param {Number} x3 x-coordinate of the third point
310340
* @param {Number} y3 y-coordinate of the third point
311341
* @return {p5} the p5 object
342+
* @example
343+
* <div>
344+
* <code>
345+
* triangle(30, 75, 58, 20, 86, 75);
346+
* </code>
347+
* </div>
312348
*/
313349
p5.prototype.triangle = function(x1, y1, x2, y2, x3, y3) {
314350
var ctx = this.canvas.getContext('2d');

0 commit comments

Comments
 (0)