UNPKG

4.01 kBMarkdownView Raw
1## P5JS Conic Section
2
3The goal here is to build a demonstration of how the various Conic Sections are defined geometrically as well as parametrically by illustrating the cross-section created by a plane intersecting two mirrored cones.
4
5This demonstration is a work in progress. The ellipse drawn is incorrect, but a start.
6
7This demonstration also shows off the interaction between Smartdown variables and the P5JS sketches, where the **Show Plane** button simply sets a boolean flag called `ShowPlane`, which the P5JS will respond to.
8
9---
10
11[Show Plane](:XShowPlane)
12
13[Showing Plane](:!ShowPlane)
14
15
16```p5js/playable/autoplay
17var PI = Math.PI;
18var HALF_PI = PI / 2.0;
19
20var geom = {
21 sketchWidth: 0,
22 sketchHeight: 0,
23 coneWidth: 0,
24 coneHeight: 0,
25 planeWidth: 0,
26 planeHeight: 0,
27 cardWidth: 0,
28 cardHeight: 0,
29 planeThickness: 0
30};
31
32var speed = 0.05;
33var ax = .01;
34var ay = ax;
35var az = ay;
36var dx, dy, dz;
37var pg;
38
39p5.windowResized = function() {
40 var w = p5.windowWidth - 30;
41 var h = Math.round(1.5 * w);
42
43 geom.sketchWidth = w;
44 geom.sketchHeight = h;
45 geom.coneWidth = Math.round(w / 3);
46 geom.coneHeight = geom.coneWidth;
47 geom.planeWidth = Math.round(w / 2);
48 geom.planeHeight = 3 * geom.planeWidth;
49 geom.cardWidth = w;
50 geom.cardHeight = Math.round(0.6 * w);
51 geom.planeThickness = 10;
52 p5.resizeCanvas(geom.sketchWidth, geom.sketchHeight);
53 pg = p5.createGraphics(geom.cardHeight, geom.cardHeight);
54};
55
56p5.setup = function() {
57 dx = p5.random(-speed, speed);
58 dy = p5.random(-speed, speed);
59 dz = p5.random(-speed, speed);
60
61 p5.createCanvas(10, 10, p5.WEBGL);
62 p5.normalMaterial();
63
64 smartdown.setVariable('ShowPlane', true, 'boolean');
65 p5.frameRate(5);
66 p5.windowResized();
67};
68
69p5.draw = function() {
70 p5.background('lightgray');
71 p5.pointLight(250, 100, 100, 5 * geom.sketchWidth, 0, 5 * geom.sketchWidth);
72 p5.pointLight(100, 100, 250, 0, 5 * geom.sketchWidth, 5 * geom.sketchWidth);
73
74 var zoom = (p5.sin(p5.frameCount * 0.1) + 2) * geom.sketchWidth * 2;
75 zoom = geom.sketchWidth * 1.5;
76 p5.camera(0, 0, zoom, 0, 0, 0, 0, 1, 0);
77 ax += dx;
78 ay += dy;
79 az += dz;
80
81 p5.push();
82 p5.rotateX(ax);
83 p5.rotateY(ay);
84 p5.rotateZ(az);
85 p5.ambientLight(100, 100, 100);
86 p5.pop();
87
88 p5.push();
89 p5.normalMaterial();
90 p5.translate(
91 0,
92 -1.5 * geom.coneHeight,
93 0);
94 p5.cone(geom.coneWidth, geom.coneHeight);
95 p5.pop();
96
97 p5.push();
98 p5.normalMaterial();
99 p5.translate(
100 0,
101 -0.5 * geom.coneHeight,
102 0);
103 p5.rotateX(PI);
104 p5.cone(geom.coneWidth, geom.coneHeight);
105 p5.pop();
106
107 if (pg) {
108 pg.push();
109 pg.clear();
110 pg.rect(0, 0, geom.cardWidth * 0.58, geom.cardHeight * 0.58);
111 pg.textSize(12);
112 var ew = geom.cardWidth / 2 + (ax * 10) % 40;
113 var eh = geom.cardHeight / 2 + (ay * 10) % 40;
114 pg.text('ax ' + ax, 20, 20);
115 pg.text('ay ' + ay, 20, 40);
116 pg.text('ew ' + ew, 20, 60);
117 pg.text('eh ' + eh, 20, 80);
118 pg.text('w ' + geom.sketchWidth, 20, 100);
119 pg.text('h ' + geom.sketchHeight, 20, 120);
120 pg.pop();
121
122 pg.push();
123 pg.stroke(0, 0, 0);
124 pg.ellipse(geom.cardWidth * 0.3, geom.cardHeight * 0.7, ew, eh);
125 pg.pop();
126
127 p5.push();
128 p5.translate(
129 -5,
130 geom.cardHeight - 50,
131 50);
132 p5.texture(pg);
133 p5.ambientLight(254, 254, 254);
134 p5.plane(geom.cardWidth, geom.cardHeight);
135 p5.pop();
136 }
137
138 if (env.ShowPlane) {
139 p5.push();
140 p5.translate(0, -geom.coneHeight, 0);
141 //p5.translate(
142 // (1 * ax) % geom.planeHeight,
143 // (1 * ay) % geom.planeHeight,
144 // (1 * az) % geom.planeHeight);
145 p5.rotateX(ax);
146 p5.rotateY(ay);
147 p5.rotateZ(az);
148
149 p5.ambientLight(150, 150, 150);
150 p5.specularMaterial(254);
151 p5.box(geom.planeWidth, geom.planeHeight, geom.planeThickness);
152 // p5.sphere(10);
153 // p5.torus(geom.planeThickness, geom.planeThickness / 2, 24, 16);
154 p5.pop();
155 }
156};
157```
158
159
160---
161
162[Back to Home](:@Home)