Skip to content

Commit a4fbdaa

Browse files
authored
Merge pull request #8070 from perminder-17/spline-work-2.0
Adding some examples for shapes.
2 parents 5787faf + 70d661f commit a4fbdaa

File tree

2 files changed

+280
-0
lines changed

2 files changed

+280
-0
lines changed

src/shape/curves.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,48 @@ function curves(p5, fn){
515515
* <div>
516516
* <code>
517517
* function setup() {
518+
* createCanvas(200, 200);
519+
* background(245);
520+
*
521+
* // Ensure the curve includes both end spans p0->p1 and p2->p3
522+
* splineProperty('ends', INCLUDE);
523+
*
524+
* // Control / anchor points
525+
* const p0 = createVector(30, 160);
526+
* const p1 = createVector(60, 40);
527+
* const p2 = createVector(140, 40);
528+
* const p3 = createVector(170, 160);
529+
*
530+
* // Draw the spline that passes through ALL four points (INCLUDE)
531+
* noFill();
532+
* stroke(0);
533+
* strokeWeight(2);
534+
* spline(p0.x, p0.y, p1.x, p1.y, p2.x, p2.y, p3.x, p3.y);
535+
*
536+
* // Draw markers + labels
537+
* fill(255);
538+
* stroke(0);
539+
* const r = 6;
540+
* circle(p0.x, p0.y, r);
541+
* circle(p1.x, p1.y, r);
542+
* circle(p2.x, p2.y, r);
543+
* circle(p3.x, p3.y, r);
544+
*
545+
* noStroke();
546+
* fill(0);
547+
* text('p0', p0.x - 14, p0.y + 14);
548+
* text('p1', p1.x - 14, p1.y - 8);
549+
* text('p2', p2.x + 4, p2.y - 8);
550+
* text('p3', p3.x + 4, p3.y + 14);
551+
*
552+
* describe('A black Catmull-Rom spline passes through p0, p1, p2, p3 with endpoints included.');
553+
* }
554+
* </code>
555+
* </div>
556+
*
557+
* <div>
558+
* <code>
559+
* function setup() {
518560
* createCanvas(100, 100);
519561
*
520562
* background(200);
@@ -806,6 +848,68 @@ function curves(p5, fn){
806848
* }
807849
* </code>
808850
* </div>
851+
*
852+
* <div>
853+
* <code>
854+
* let p0, p1, p2, p3;
855+
*
856+
* function setup() {
857+
* createCanvas(200, 200);
858+
* splineProperty('ends', INCLUDE); // make endpoints part of the curve
859+
*
860+
* // Four points forming a gentle arch
861+
* p0 = createVector(30, 160);
862+
* p1 = createVector(60, 50);
863+
* p2 = createVector(140, 50);
864+
* p3 = createVector(170, 160);
865+
*
866+
* describe('Black spline through p0–p3. A red dot marks the location at parameter t on p1->p2 using splinePoint.');
867+
* }
868+
*
869+
* function draw() {
870+
* background(245);
871+
*
872+
* // Draw the spline for context
873+
* noFill();
874+
* stroke(0);
875+
* strokeWeight(2);
876+
* spline(p0.x, p0.y, p1.x, p1.y, p2.x, p2.y, p3.x, p3.y);
877+
*
878+
* // Map mouse X to t in [0, 1] (span p1->p2)
879+
* let t = constrain(map(mouseX, 0, width, 0, 1), 0, 1);
880+
*
881+
* // Evaluate the curve point by axis (splinePoint works one axis at a time)
882+
* let x = splinePoint(p0.x, p1.x, p2.x, p3.x, t);
883+
* let y = splinePoint(p0.y, p1.y, p2.y, p3.y, t);
884+
*
885+
* // Marker at the evaluated position
886+
* noStroke();
887+
* fill('red');
888+
* circle(x, y, 8);
889+
*
890+
* // Draw control/anchor points
891+
* stroke(0);
892+
* strokeWeight(1);
893+
* fill(255);
894+
* const r = 6;
895+
* circle(p0.x, p0.y, r);
896+
* circle(p1.x, p1.y, r);
897+
* circle(p2.x, p2.y, r);
898+
* circle(p3.x, p3.y, r);
899+
*
900+
* // Labels + UI hint
901+
* noStroke();
902+
* fill(20);
903+
* textSize(10);
904+
* text('p0', p0.x - 12, p0.y + 14);
905+
* text('p1', p1.x - 12, p1.y - 8);
906+
* text('p2', p2.x + 4, p2.y - 8);
907+
* text('p3', p3.x + 4, p3.y + 14);
908+
* text('t = ' + nf(t, 1, 2) + ' (p1→p2)', 8, 16);
909+
* }
910+
* </code>
911+
* </div>
912+
*
809913
*/
810914

811915
fn.splinePoint = function(a, b, c, d, t) {

src/shape/custom_shapes.js

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,6 +1823,93 @@ function customShapes(p5, fn) {
18231823
* }
18241824
* </code>
18251825
* </div>
1826+
*
1827+
* @example
1828+
* <div>
1829+
* <code>
1830+
* let vertexA;
1831+
* let vertexB;
1832+
* let vertexC;
1833+
* let vertexD;
1834+
* let vertexE;
1835+
* let vertexF;
1836+
*
1837+
* let markerRadius;
1838+
*
1839+
* let vectorAB;
1840+
* let vectorFE;
1841+
*
1842+
* let endOfTangentB;
1843+
* let endOfTangentE;
1844+
*
1845+
* function setup() {
1846+
* createCanvas(100, 100);
1847+
*
1848+
* // Initialize variables
1849+
* // Adjusting vertices A and F affects the slopes at B and E
1850+
*
1851+
* vertexA = createVector(35, 85);
1852+
* vertexB = createVector(25, 70);
1853+
* vertexC = createVector(30, 30);
1854+
* vertexD = createVector(70, 30);
1855+
* vertexE = createVector(75, 70);
1856+
* vertexF = createVector(65, 85);
1857+
*
1858+
* markerRadius = 4;
1859+
*
1860+
* vectorAB = p5.Vector.sub(vertexB, vertexA);
1861+
* vectorFE = p5.Vector.sub(vertexE, vertexF);
1862+
*
1863+
* endOfTangentB = p5.Vector.add(vertexC, vectorAB);
1864+
* endOfTangentE = p5.Vector.add(vertexD, vectorFE);
1865+
*
1866+
* splineProperty(`ends`, EXCLUDE);
1867+
*
1868+
* // Draw figure
1869+
*
1870+
* background(220);
1871+
*
1872+
* noFill();
1873+
*
1874+
* beginShape();
1875+
* splineVertex(vertexA.x, vertexA.y);
1876+
* splineVertex(vertexB.x, vertexB.y);
1877+
* splineVertex(vertexC.x, vertexC.y);
1878+
* splineVertex(vertexD.x, vertexD.y);
1879+
* splineVertex(vertexE.x, vertexE.y);
1880+
* splineVertex(vertexF.x, vertexF.y);
1881+
* endShape();
1882+
*
1883+
* stroke('red');
1884+
* line(vertexA.x, vertexA.y, vertexC.x, vertexC.y);
1885+
* line(vertexB.x, vertexB.y, endOfTangentB.x, endOfTangentB.y);
1886+
*
1887+
* stroke('blue');
1888+
* line(vertexD.x, vertexD.y, vertexF.x, vertexF.y);
1889+
* line(vertexE.x, vertexE.y, endOfTangentE.x, endOfTangentE.y);
1890+
*
1891+
* fill('white');
1892+
* stroke('black');
1893+
* circle(vertexA.x, vertexA.y, markerRadius);
1894+
* circle(vertexB.x, vertexB.y, markerRadius);
1895+
* circle(vertexC.x, vertexC.y, markerRadius);
1896+
* circle(vertexD.x, vertexD.y, markerRadius);
1897+
* circle(vertexE.x, vertexE.y, markerRadius);
1898+
* circle(vertexF.x, vertexF.y, markerRadius);
1899+
*
1900+
* fill('black');
1901+
* noStroke();
1902+
* text('A', vertexA.x - 15, vertexA.y + 5);
1903+
* text('B', vertexB.x - 15, vertexB.y + 5);
1904+
* text('C', vertexC.x - 5, vertexC.y - 5);
1905+
* text('D', vertexD.x - 5, vertexD.y - 5);
1906+
* text('E', vertexE.x + 5, vertexE.y + 5);
1907+
* text('F', vertexF.x + 5, vertexF.y + 5);
1908+
*
1909+
* describe('On a gray background, a black spline passes through vertices A, B, C, D, E, and F, shown as white circles. A red line segment joining vertices A and C has the same slope as the red tangent segment at B. Similarly, the blue line segment joining vertices D and F has the same slope as the blue tangent at E.');
1910+
* }
1911+
* </code>
1912+
* </div>
18261913
*/
18271914

18281915
/**
@@ -2098,6 +2185,95 @@ function customShapes(p5, fn) {
20982185
* }
20992186
* </code>
21002187
* </div>
2188+
*
2189+
* @example
2190+
*
2191+
* <div>
2192+
* <code>
2193+
* let vertexA;
2194+
* let vertexB;
2195+
* let vertexC;
2196+
* let vertexD;
2197+
* let vertexE;
2198+
* let vertexF;
2199+
*
2200+
* let markerRadius;
2201+
*
2202+
* let vectorAB;
2203+
* let vectorFE;
2204+
*
2205+
* let endOfTangentB;
2206+
* let endOfTangentE;
2207+
*
2208+
* function setup() {
2209+
* createCanvas(100, 100);
2210+
*
2211+
* // Initialize variables
2212+
* // Adjusting vertices A and F affects the slopes at B and E
2213+
*
2214+
* vertexA = createVector(35, 85);
2215+
* vertexB = createVector(25, 70);
2216+
* vertexC = createVector(30, 30);
2217+
* vertexD = createVector(70, 30);
2218+
* vertexE = createVector(75, 70);
2219+
* vertexF = createVector(65, 85);
2220+
*
2221+
* markerRadius = 4;
2222+
*
2223+
* vectorAB = p5.Vector.sub(vertexB, vertexA);
2224+
* vectorFE = p5.Vector.sub(vertexE, vertexF);
2225+
*
2226+
* endOfTangentB = p5.Vector.add(vertexC, vectorAB);
2227+
* endOfTangentE = p5.Vector.add(vertexD, vectorFE);
2228+
*
2229+
* splineProperty(`ends`, EXCLUDE);
2230+
*
2231+
* // Draw figure
2232+
*
2233+
* background(220);
2234+
*
2235+
* noFill();
2236+
*
2237+
* beginShape();
2238+
* splineVertex(vertexA.x, vertexA.y);
2239+
* splineVertex(vertexB.x, vertexB.y);
2240+
* splineVertex(vertexC.x, vertexC.y);
2241+
* splineVertex(vertexD.x, vertexD.y);
2242+
* splineVertex(vertexE.x, vertexE.y);
2243+
* splineVertex(vertexF.x, vertexF.y);
2244+
* endShape();
2245+
*
2246+
* stroke('red');
2247+
* line(vertexA.x, vertexA.y, vertexC.x, vertexC.y);
2248+
* line(vertexB.x, vertexB.y, endOfTangentB.x, endOfTangentB.y);
2249+
*
2250+
* stroke('blue');
2251+
* line(vertexD.x, vertexD.y, vertexF.x, vertexF.y);
2252+
* line(vertexE.x, vertexE.y, endOfTangentE.x, endOfTangentE.y);
2253+
*
2254+
* fill('white');
2255+
* stroke('black');
2256+
* circle(vertexA.x, vertexA.y, markerRadius);
2257+
* circle(vertexB.x, vertexB.y, markerRadius);
2258+
* circle(vertexC.x, vertexC.y, markerRadius);
2259+
* circle(vertexD.x, vertexD.y, markerRadius);
2260+
* circle(vertexE.x, vertexE.y, markerRadius);
2261+
* circle(vertexF.x, vertexF.y, markerRadius);
2262+
*
2263+
* fill('black');
2264+
* noStroke();
2265+
* text('A', vertexA.x - 15, vertexA.y + 5);
2266+
* text('B', vertexB.x - 15, vertexB.y + 5);
2267+
* text('C', vertexC.x - 5, vertexC.y - 5);
2268+
* text('D', vertexD.x - 5, vertexD.y - 5);
2269+
* text('E', vertexE.x + 5, vertexE.y + 5);
2270+
* text('F', vertexF.x + 5, vertexF.y + 5);
2271+
*
2272+
* describe('On a gray background, a black spline passes through vertices A, B, C, D, E, and F, shown as white circles. A red line segment joining vertices A and C has the same slope as the red tangent segment at B. Similarly, the blue line segment joining vertices D and F has the same slope as the blue tangent at E.');
2273+
* }
2274+
* </code>
2275+
* </div>
2276+
*
21012277
*/
21022278

21032279
/**

0 commit comments

Comments
 (0)