-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy path2d_primitives.js
More file actions
1312 lines (1263 loc) · 36.8 KB
/
2d_primitives.js
File metadata and controls
1312 lines (1263 loc) · 36.8 KB
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @module Shape
* @submodule 2D Primitives
* @for p5
*/
import * as constants from '../core/constants';
import canvas from '../core/helpers';
function primitives(p5, fn){
/**
* This function does 3 things:
*
* 1. Bounds the desired start/stop angles for an arc (in radians) so that:
*
* 0 <= start < TWO_PI ; start <= stop < start + TWO_PI
*
* This means that the arc rendering functions don't have to be concerned
* with what happens if stop is smaller than start, or if the arc 'goes
* round more than once', etc.: they can just start at start and increase
* until stop and the correct arc will be drawn.
*
* 2. Optionally adjusts the angles within each quadrant to counter the naive
* scaling of the underlying ellipse up from the unit circle. Without
* this, the angles become arbitrary when width != height: 45 degrees
* might be drawn at 5 degrees on a 'wide' ellipse, or at 85 degrees on
* a 'tall' ellipse.
*
* 3. Flags up when start and stop correspond to the same place on the
* underlying ellipse. This is useful if you want to do something special
* there (like rendering a whole ellipse instead).
*/
fn._normalizeArcAngles = (
start,
stop,
width,
height,
correctForScaling
) => {
const epsilon = 0.00001; // Smallest visible angle on displays up to 4K.
let separation;
// The order of the steps is important here: each one builds upon the
// adjustments made in the steps that precede it.
// Constrain both start and stop to [0,TWO_PI).
start = start - constants.TWO_PI * Math.floor(start / constants.TWO_PI);
stop = stop - constants.TWO_PI * Math.floor(stop / constants.TWO_PI);
// Get the angular separation between the requested start and stop points.
//
// Technically this separation only matches what gets drawn if
// correctForScaling is enabled. We could add a more complicated calculation
// for when the scaling is uncorrected (in which case the drawn points could
// end up pushed together or pulled apart quite dramatically relative to what
// was requested), but it would make things more opaque for little practical
// benefit.
//
// (If you do disable correctForScaling and find that correspondToSamePoint
// is set too aggressively, the easiest thing to do is probably to just make
// epsilon smaller...)
separation = Math.min(
Math.abs(start - stop),
constants.TWO_PI - Math.abs(start - stop)
);
// Optionally adjust the angles to counter linear scaling.
if (correctForScaling) {
if (start <= constants.HALF_PI) {
start = Math.atan(width / height * Math.tan(start));
} else if (start > constants.HALF_PI && start <= 3 * constants.HALF_PI) {
start = Math.atan(width / height * Math.tan(start)) + constants.PI;
} else {
start = Math.atan(width / height * Math.tan(start)) + constants.TWO_PI;
}
if (stop <= constants.HALF_PI) {
stop = Math.atan(width / height * Math.tan(stop));
} else if (stop > constants.HALF_PI && stop <= 3 * constants.HALF_PI) {
stop = Math.atan(width / height * Math.tan(stop)) + constants.PI;
} else {
stop = Math.atan(width / height * Math.tan(stop)) + constants.TWO_PI;
}
}
// Ensure that start <= stop < start + TWO_PI.
if (start > stop) {
stop += constants.TWO_PI;
}
return {
start,
stop,
correspondToSamePoint: separation < epsilon
};
};
/**
* Draws an arc.
*
* An arc is a section of an ellipse defined by the `x`, `y`, `w`, and
* `h` parameters. `x` and `y` set the location of the arc's center. `w` and
* `h` set the arc's width and height. See
* <a href="#/p5/ellipse">ellipse()</a> and
* <a href="#/p5/ellipseMode">ellipseMode()</a> for more details.
*
* The fifth and sixth parameters, `start` and `stop`, set the angles
* between which to draw the arc. Arcs are always drawn clockwise from
* `start` to `stop`. Angles are always given in radians.
*
* The seventh parameter, `mode`, is optional. It determines the arc's fill
* style. The fill modes are a semi-circle (`OPEN`), a closed semi-circle
* (`CHORD`), or a closed pie segment (`PIE`).
*
* The eighth parameter, `detail`, is also optional. It determines how many
* vertices are used to draw the arc in WebGL mode. The default value is 25.
*
* @method arc
* @param {Number} x x-coordinate of the arc's ellipse.
* @param {Number} y y-coordinate of the arc's ellipse.
* @param {Number} w width of the arc's ellipse by default.
* @param {Number} h height of the arc's ellipse by default.
* @param {Number} start angle to start the arc, specified in radians.
* @param {Number} stop angle to stop the arc, specified in radians.
* @param {(CHORD|PIE|OPEN)} [mode] optional parameter to determine the way of drawing
* the arc. either CHORD, PIE, or OPEN.
* @param {Integer} [detail] optional parameter for WebGL mode only. This is to
* specify the number of vertices that makes up the
* perimeter of the arc. Default value is 25. Won't
* draw a stroke for a detail of more than 50.
* @chainable
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* arc(50, 50, 80, 80, 0, PI + HALF_PI);
*
* describe('A white circle on a gray canvas. The top-right quarter of the circle is missing.');
* }
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* arc(50, 50, 80, 40, 0, PI + HALF_PI);
*
* describe('A white ellipse on a gray canvas. The top-right quarter of the ellipse is missing.');
* }
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Bottom-right.
* arc(50, 55, 50, 50, 0, HALF_PI);
*
* noFill();
*
* // Bottom-left.
* arc(50, 55, 60, 60, HALF_PI, PI);
*
* // Top-left.
* arc(50, 55, 70, 70, PI, PI + QUARTER_PI);
*
* // Top-right.
* arc(50, 55, 80, 80, PI + QUARTER_PI, TWO_PI);
*
* describe(
* 'A shattered outline of an circle with a quarter of a white circle at the bottom-right.'
* );
* }
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Default fill mode.
* arc(50, 50, 80, 80, 0, PI + QUARTER_PI);
*
* describe('A white circle with the top-right third missing. The bottom is outlined in black.');
* }
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // OPEN fill mode.
* arc(50, 50, 80, 80, 0, PI + QUARTER_PI, OPEN);
*
* describe(
* 'A white circle missing a section from the top-right. The bottom is outlined in black.'
* );
* }
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // CHORD fill mode.
* arc(50, 50, 80, 80, 0, PI + QUARTER_PI, CHORD);
*
* describe('A white circle with a black outline missing a section from the top-right.');
* }
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // PIE fill mode.
* arc(50, 50, 80, 80, 0, PI + QUARTER_PI, PIE);
*
* describe('A white circle with a black outline. The top-right third is missing.');
* }
*
* @example
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* background(200);
*
* // PIE fill mode.
* arc(0, 0, 80, 80, 0, PI + QUARTER_PI, PIE);
*
* describe('A white circle with a black outline. The top-right third is missing.');
* }
*
* @example
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* background(200);
*
* // PIE fill mode with 5 vertices.
* arc(0, 0, 80, 80, 0, PI + QUARTER_PI, PIE, 5);
*
* describe('A white circle with a black outline. The top-right third is missing.');
* }
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* describe('A yellow circle on a black background. The circle opens and closes its mouth.');
* }
*
* function draw() {
* background(0);
*
* // Style the arc.
* noStroke();
* fill(255, 255, 0);
*
* // Update start and stop angles.
* let biteSize = PI / 16;
* let startAngle = biteSize * sin(frameCount * 0.1) + biteSize;
* let endAngle = TWO_PI - startAngle;
*
* // Draw the arc.
* arc(50, 50, 80, 80, startAngle, endAngle, PIE);
* }
*/
fn.arc = function(x, y, w, h, start, stop, mode, detail) {
// this.validate("p5.arc", arguments);
// p5._validateParameters('arc', arguments);
// if the current stroke and fill settings wouldn't result in something
// visible, exit immediately
if (
!this._renderer.states.strokeColor &&
!this._renderer.states.fillColor
) {
return this;
}
if (start === stop) {
return this;
}
start = this._toRadians(start);
stop = this._toRadians(stop);
const vals = canvas.modeAdjust(
x, y,
w, h,
this._renderer.states.ellipseMode
);
const angles = this._normalizeArcAngles(start, stop, vals.w, vals.h, true);
if (angles.correspondToSamePoint) {
// If the arc starts and ends at (near enough) the same place, we choose to
// draw an ellipse instead. This is preferable to faking an ellipse (by
// making stop ever-so-slightly less than start + TWO_PI) because the ends
// join up to each other rather than at a vertex at the centre (leaving
// an unwanted spike in the stroke/fill).
this._renderer.ellipse([vals.x, vals.y, vals.w, vals.h, detail]);
} else {
this._renderer.arc(
vals.x,
vals.y,
vals.w,
vals.h,
angles.start, // [0, TWO_PI)
angles.stop, // [start, start + TWO_PI)
mode,
detail
);
//accessible Outputs
if (this._accessibleOutputs.grid || this._accessibleOutputs.text) {
this._accsOutput('arc', [
vals.x,
vals.y,
vals.w,
vals.h,
angles.start,
angles.stop,
mode
]);
}
}
return this;
};
/**
* Draws an ellipse (oval).
*
* An ellipse is a round shape defined by the `x`, `y`, `w`, and
* `h` parameters. `x` and `y` set the location of its center. `w` and
* `h` set its width and height. See
* <a href="#/p5/ellipseMode">ellipseMode()</a> for other ways to set
* its position.
*
* If no height is set, the value of width is used for both the width and
* height. If a negative height or width is specified, the absolute value is
* taken.
*
* The fifth parameter, `detail`, is also optional. It determines how many
* vertices are used to draw the ellipse in WebGL mode. The default value is
* 25.
*
* @method ellipse
* @param {Number} x x-coordinate of the center of the ellipse.
* @param {Number} y y-coordinate of the center of the ellipse.
* @param {Number} w width of the ellipse.
* @param {Number} [h] height of the ellipse.
* @chainable
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* ellipse(50, 50, 80, 80);
*
* describe('A white circle on a gray canvas.');
* }
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* ellipse(50, 50, 80);
*
* describe('A white circle on a gray canvas.');
* }
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* ellipse(50, 50, 80, 40);
*
* describe('A white ellipse on a gray canvas.');
* }
*
* @example
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* background(200);
*
* ellipse(0, 0, 80, 40);
*
* describe('A white ellipse on a gray canvas.');
* }
*
* @example
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* background(200);
*
* // Use 6 vertices.
* ellipse(0, 0, 80, 40, 6);
*
* describe('A white hexagon on a gray canvas.');
* }
*/
/**
* @method ellipse
* @param {Number} x
* @param {Number} y
* @param {Number} w
* @param {Number} h
* @param {Integer} [detail] optional parameter for WebGL mode only. This is to
* specify the number of vertices that makes up the
* perimeter of the ellipse. Default value is 25. Won't
* draw a stroke for a detail of more than 50.
*/
fn.ellipse = function(x, y, w, h, detailX) {
// p5._validateParameters('ellipse', arguments);
return this._renderEllipse(...arguments);
};
/**
* Draws a circle.
*
* A circle is a round shape defined by the `x`, `y`, and `d` parameters.
* `x` and `y` set the location of its center. `d` sets its width and height (diameter).
* Every point on the circle's edge is the same distance, `0.5 * d`, from its center.
* `0.5 * d` (half the diameter) is the circle's radius.
* See <a href="#/p5/ellipseMode">ellipseMode()</a> for other ways to set its position.
*
* @method circle
* @param {Number} x x-coordinate of the center of the circle.
* @param {Number} y y-coordinate of the center of the circle.
* @param {Number} d diameter of the circle.
* @chainable
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* circle(50, 50, 25);
*
* describe('A white circle with black outline in the middle of a gray canvas.');
* }
*
* @example
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* background(200);
*
* circle(0, 0, 25);
*
* describe('A white circle with black outline in the middle of a gray canvas.');
* }
*/
fn.circle = function(...args) {
// p5._validateParameters('circle', args);
const argss = args.slice( 0, 2);
argss.push(args[2], args[2]);
return this._renderEllipse(...argss);
};
// internal method for drawing ellipses (without parameter validation)
fn._renderEllipse = function(x, y, w, h, detailX) {
// if the current stroke and fill settings wouldn't result in something
// visible, exit immediately
if (
!this._renderer.states.strokeColor &&
!this._renderer.states.fillColor
) {
return this;
}
// Duplicate 3rd argument if only 3 given.
if (typeof h === 'undefined') {
h = w;
}
const vals = canvas.modeAdjust(
x, y,
w, h,
this._renderer.states.ellipseMode
);
this._renderer.ellipse([vals.x, vals.y, vals.w, vals.h, detailX]);
//accessible Outputs
if (this._accessibleOutputs.grid || this._accessibleOutputs.text) {
this._accsOutput('ellipse', [vals.x, vals.y, vals.w, vals.h]);
}
return this;
};
/**
* Draws a straight line between two points.
*
* A line's default width is one pixel. The version of `line()` with four
* parameters draws the line in 2D. To color a line, use the
* <a href="#/p5/stroke">stroke()</a> function. To change its width, use the
* <a href="#/p5/strokeWeight">strokeWeight()</a> function. A line
* can't be filled, so the <a href="#/p5/fill">fill()</a> function won't
* affect the line's color.
*
* The version of `line()` with six parameters allows the line to be drawn in
* 3D space. Doing so requires adding the `WEBGL` argument to
* <a href="#/p5/createCanvas">createCanvas()</a>.
*
* @method line
* @param {Number} x1 the x-coordinate of the first point.
* @param {Number} y1 the y-coordinate of the first point.
* @param {Number} x2 the x-coordinate of the second point.
* @param {Number} y2 the y-coordinate of the second point.
* @chainable
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* line(30, 20, 85, 75);
*
* describe(
* 'A black line on a gray canvas running from top-center to bottom-right.'
* );
* }
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Style the line.
* stroke('magenta');
* strokeWeight(5);
*
* line(30, 20, 85, 75);
*
* describe(
* 'A thick, magenta line on a gray canvas running from top-center to bottom-right.'
* );
* }
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Top.
* line(30, 20, 85, 20);
*
* // Right.
* stroke(126);
* line(85, 20, 85, 75);
*
* // Bottom.
* stroke(255);
* line(85, 75, 30, 75);
*
* describe(
* 'Three lines drawn in grayscale on a gray canvas. They form the top, right, and bottom sides of a square.'
* );
* }
*
* @example
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* background(200);
*
* line(-20, -30, 35, 25);
*
* describe(
* 'A black line on a gray canvas running from top-center to bottom-right.'
* );
* }
*
* @example
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* describe('A black line connecting two spheres. The scene spins slowly.');
* }
*
* function draw() {
* background(200);
*
* // Rotate around the y-axis.
* rotateY(frameCount * 0.01);
*
* // Draw a line.
* line(0, 0, 0, 30, 20, -10);
*
* // Draw the center sphere.
* sphere(10);
*
* // Translate to the second point.
* translate(30, 20, -10);
*
* // Draw the bottom-right sphere.
* sphere(10);
* }
*/
/**
* @method line
* @param {Number} x1
* @param {Number} y1
* @param {Number} z1 the z-coordinate of the first point.
* @param {Number} x2
* @param {Number} y2
* @param {Number} z2 the z-coordinate of the second point.
* @chainable
*/
fn.line = function(...args) {
// p5._validateParameters('line', args);
if (this._renderer.states.strokeColor) {
this._renderer.line(...args);
}
//accessible Outputs
if (this._accessibleOutputs.grid || this._accessibleOutputs.text) {
this._accsOutput('line', args);
}
return this;
};
/**
* Draws a single point in space.
*
* A point's default width is one pixel. To color a point, use the
* <a href="#/p5/stroke">stroke()</a> function. To change its width, use the
* <a href="#/p5/strokeWeight">strokeWeight()</a> function. A point
* can't be filled, so the <a href="#/p5/fill">fill()</a> function won't
* affect the point's color.
*
* The version of `point()` with two parameters allows the point's location to
* be set with its x- and y-coordinates, as in `point(10, 20)`.
*
* The version of `point()` with three parameters allows the point to be drawn
* in 3D space with x-, y-, and z-coordinates, as in `point(10, 20, 30)`.
* Doing so requires adding the `WEBGL` argument to
* <a href="#/p5/createCanvas">createCanvas()</a>.
*
* The version of `point()` with one parameter allows the point's location to
* be set with a <a href="#/p5/p5.Vector">p5.Vector</a> object.
*
* @method point
* @param {Number} x the x-coordinate.
* @param {Number} y the y-coordinate.
* @param {Number} [z] the z-coordinate (for WebGL mode).
* @chainable
* @example
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Making point to 5 pixels
* strokeWeight(5);
*
* // Top-left.
* point(30, 20);
*
* // Top-right.
* point(85, 20);
*
* // Bottom-right.
* point(85, 75);
*
* // Bottom-left.
* point(30, 75);
*
* describe(
* 'Four small, black points drawn on a gray canvas. The points form the corners of a square.'
* );
* }
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Making point to 5 pixels.
* strokeWeight(5);
*
* // Top-left.
* point(30, 20);
*
* // Top-right.
* point(70, 20);
*
* // Style the next points.
* stroke('purple');
* strokeWeight(10);
*
* // Bottom-right.
* point(70, 80);
*
* // Bottom-left.
* point(30, 80);
*
* describe(
* 'Four points drawn on a gray canvas. Two are black and two are purple. The points form the corners of a square.'
* );
* }
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Making point to 5 pixels.
* strokeWeight(5);
*
* // Top-left.
* let a = createVector(30, 20);
* point(a);
*
* // Top-right.
* let b = createVector(70, 20);
* point(b);
*
* // Bottom-right.
* let c = createVector(70, 80);
* point(c);
*
* // Bottom-left.
* let d = createVector(30, 80);
* point(d);
*
* describe(
* 'Four small, black points drawn on a gray canvas. The points form the corners of a square.'
* );
* }
*
* @example
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* describe('Two purple points drawn on a gray canvas.');
* }
*
* function draw() {
* background(200);
*
* // Style the points.
* stroke('purple');
* strokeWeight(10);
*
* // Top-left.
* point(-20, -30);
*
* // Bottom-right.
* point(20, 30);
* }
*
* @example
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* describe('Two purple points drawn on a gray canvas. The scene spins slowly.');
* }
*
* function draw() {
* background(200);
*
* // Rotate around the y-axis.
* rotateY(frameCount * 0.01);
*
* // Style the points.
* stroke('purple');
* strokeWeight(10);
*
* // Top-left.
* point(-20, -30, 0);
*
* // Bottom-right.
* point(20, 30, -50);
* }
*/
/**
* @method point
* @param {p5.Vector} coordinateVector the coordinate vector.
* @chainable
*/
fn.point = function(...args) {
// p5._validateParameters('point', args);
if (this._renderer.states.strokeColor) {
if (args.length === 1 && args[0] instanceof p5.Vector) {
this._renderer.point.call(
this._renderer,
args[0].x,
args[0].y,
args[0].z
);
} else {
this._renderer.point(...args);
//accessible Outputs
if (this._accessibleOutputs.grid || this._accessibleOutputs.text) {
this._accsOutput('point', args);
}
}
}
return this;
};
/**
* Draws a quadrilateral (four-sided shape).
*
* Quadrilaterals include rectangles, squares, rhombuses, and trapezoids. The
* first pair of parameters `(x1, y1)` sets the quad's first point. The next
* three pairs of parameters set the coordinates for its next three points
* `(x2, y2)`, `(x3, y3)`, and `(x4, y4)`. Points should be added in either
* clockwise or counter-clockwise order.
*
* The version of `quad()` with twelve parameters allows the quad to be drawn
* in 3D space. Doing so requires adding the `WEBGL` argument to
* <a href="#/p5/createCanvas">createCanvas()</a>.
*
* The thirteenth and fourteenth parameters are optional. In WebGL mode, they
* set the number of segments used to draw the quadrilateral in the x- and
* y-directions. They're both 2 by default.
*
* @method quad
* @param {Number} x1 the x-coordinate of the first point.
* @param {Number} y1 the y-coordinate of the first point.
* @param {Number} x2 the x-coordinate of the second point.
* @param {Number} y2 the y-coordinate of the second point.
* @param {Number} x3 the x-coordinate of the third point.
* @param {Number} y3 the y-coordinate of the third point.
* @param {Number} x4 the x-coordinate of the fourth point.
* @param {Number} y4 the y-coordinate of the fourth point.
* @param {Integer} [detailX] number of segments in the x-direction.
* @param {Integer} [detailY] number of segments in the y-direction.
* @chainable
* @example
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* quad(20, 20, 80, 20, 80, 80, 20, 80);
*
* describe('A white square with a black outline drawn on a gray canvas.');
* }
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* quad(20, 30, 80, 30, 80, 70, 20, 70);
*
* describe('A white rectangle with a black outline drawn on a gray canvas.');
* }
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* quad(50, 62, 86, 50, 50, 38, 14, 50);
*
* describe('A white rhombus with a black outline drawn on a gray canvas.');
* }
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* quad(20, 50, 80, 30, 80, 70, 20, 70);
*
* describe('A white trapezoid with a black outline drawn on a gray canvas.');
* }
*
* @example
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* background(200);
*
* quad(-30, -30, 30, -30, 30, 30, -30, 30);
*
* describe('A white square with a black outline drawn on a gray canvas.');
* }
*
* @example
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* describe('A wavy white surface spins around on gray canvas.');
* }
*
* function draw() {
* background(200);
*
* // Rotate around the y-axis.
* rotateY(frameCount * 0.01);
*
* // Draw the quad.
* quad(-30, -30, 0, 30, -30, 0, 30, 30, 20, -30, 30, -20);
* }
*/
/**
* @method quad
* @param {Number} x1
* @param {Number} y1
* @param {Number} z1 the z-coordinate of the first point.
* @param {Number} x2
* @param {Number} y2
* @param {Number} z2 the z-coordinate of the second point.
* @param {Number} x3
* @param {Number} y3
* @param {Number} z3 the z-coordinate of the third point.
* @param {Number} x4
* @param {Number} y4
* @param {Number} z4 the z-coordinate of the fourth point.
* @param {Integer} [detailX]
* @param {Integer} [detailY]
* @chainable
*/
fn.quad = function(...args) {
// p5._validateParameters('quad', args);
if (this._renderer.states.strokeColor || this._renderer.states.fillColor) {
if (this._renderer.isP3D && args.length < 12) {
// if 3D and we weren't passed 12 args, assume Z is 0
this._renderer.quad.call(
this._renderer,
args[0], args[1], 0,
args[2], args[3], 0,
args[4], args[5], 0,
args[6], args[7], 0,
args[8], args[9]);
} else {
this._renderer.quad(...args);
//accessibile outputs
if (this._accessibleOutputs.grid || this._accessibleOutputs.text) {
this._accsOutput('quadrilateral', args);
}
}
}
return this;
};
/**
* Draws a rectangle.
*
* A rectangle is a four-sided shape defined by the `x`, `y`, `w`, and `h`
* parameters. `x` and `y` set the location of its top-left corner. `w` sets
* its width and `h` sets its height. Every angle in the rectangle measures
* 90˚. See <a href="#/p5/rectMode">rectMode()</a> for other ways to define
* rectangles.
*
* The version of `rect()` with five parameters creates a rounded rectangle. The
* fifth parameter sets the radius for all four corners.
*
* The version of `rect()` with eight parameters also creates a rounded
* rectangle. Each of the last four parameters set the radius of a corner. The
* radii start with the top-left corner and move clockwise around the
* rectangle. If any of these parameters are omitted, they are set to the
* value of the last radius that was set.
*
* @method rect
* @param {Number} x x-coordinate of the rectangle.
* @param {Number} y y-coordinate of the rectangle.
* @param {Number} w width of the rectangle.
* @param {Number} [h] height of the rectangle.
* @param {Number} [tl] optional radius of top-left corner.