No description provided
The showcase player uses a modified version of Processing.js in combination with jsweet to let students program their apps in Java code while still allowing for browser support.
Content created by students is scaled to fit the showcase frame while maintaining aspect ratio and cursor position. This is why some projects may appear blurry in fullscreen, or why some small details may not be visible on a small screen
<iframe width='500px' height='400px' src='https://nest.ktbyte.com/nest#95461' allowfullscreen></iframe>
//========================================================== // sketch: PG_RaySphereIntersection.pde // intersection of a ray with a sphere // version: v1.0 2011-11-08, // v1.1 2012-11-25 // v1.2 2017-01-11 running with JavaScript //========================================================== Ray ray = new Ray (450,250,160, 20,30,10); float radius = 200; PVector center = new PVector (300,300); PSphere testSphere = new PSphere(center, radius); PSphere helpSphere = new PSphere(center, 3); //---------------------------------------------------------- void setup() { size(600, 600, P3D); sphereDetail(44); smooth(); textSize(16); // ortho(-width/2, width/2, -height/2, height/2, -1, 1); } //---------------------------------------------------------- void draw() { background(144); lights(); ray.lookAt (mouseX, mouseY, 160); // println (ray.origin + " " + ray.direction); // draw ray strokeWeight(2); stroke (0); // black color ray.draw3d(); // draw sphere fill(244,77); // transparent gray color noFill(); stroke (244,44); testSphere.draw(); PVector S1 = new PVector(); PVector S2 = new PVector(); // calulate S1 and S2 int ip = raySphereIntersection(ray, testSphere, S1, S2); noLights(); fill(0); showText ("press right mouse button to set ray origin",10,20); showText ("intersectionPoints=" + ip,10,40); // draw intersection points if (ip > 0) { fill(255,88,88); noStroke(); helpSphere.set (S1, 4.0); helpSphere.draw(); showText ("S1: ["+(int)S1.x+", "+(int)S1.y+", "+(int)S1.z+"]",10,60); fill(44,255,44); helpSphere.set (S2, 4.0); helpSphere.draw(); showText ("S2: ["+(int)S2.x+", "+(int)S2.y+", "+(int)S2.z+"]",10,80); } } //---------------------------------------------------------- void mousePressed() { if (mouseButton == RIGHT) ray.setOrigin (mouseX, mouseY, 80); } //---------------------------------------------------------- // show 2d text //---------------------------------------------------------- void showText(String infoText, int x, int y) { //textMode(SCREEN); text(infoText, x, y); } //========================================================== // file: RaySphereIntersection.pde // handle intersection of a ray with a sphere // date: 2011-11-08, 2012-11-25 // // functions: raySphereIntersection // classes: class Ray // class PSphere // //========================================================== //---------------------------------------------------------- // intersection of a ray with a circle in 2d // or of a ray with a sphere in 3d !!! // input: ray ray origin and direction point // c sphere // output: S1,S2 intersection points // return: int number of intersection points 0,1,2 //---------------------------------------------------------- int raySphereIntersection(Ray ray, PSphere c, PVector S1, PVector S2) { PVector e = new PVector(ray.direction.x, ray.direction.y, ray.direction.z); // e=ray.dir e.normalize(); // e=g/|g| PVector h = PVector.sub(c.center,ray.origin); // h=r.o-c.M float lf = e.dot(h); // lf=e.h float s = sq(c.radius)-h.dot(h)+sq(lf); // s=r^2-h^2+lf^2 if (s < 0.0) return 0; // no intersection points ? s = sqrt(s); // s=sqrt(r^2-h^2+lf^2) int result = 0; if (lf < s) // S1 behind A ? { if (lf+s >= 0) // S2 before A ?} { s = -s; // swap S1 <-> S2} result = 1; // one intersection point } } else result = 2; // 2 intersection points S1.set(PVector.mult(e, lf-s)); S1.add(ray.origin); // S1=A+e*(lf-s) S2.set(PVector.mult(e, lf+s)); S2.add(ray.origin); // S2=A+e*(lf+s) // println (" s=" + nf( s,0,2)+ " lf=" + nf(lf,0,2)); // only for testing return result; } //---------------------------------------------------------- // rays are defined by origin and direction //---------------------------------------------------------- class Ray { PVector origin; PVector direction; Ray(float Ax, float Ay, float Az ,float Bx, float By, float Bz) { origin = new PVector(Ax, Ay, Az); direction = new PVector(Bx-Ax, By-Ay, Bz-Az); } Ray(PVector A, PVector B) { origin = new PVector(A.x, A.y, A.z); direction = PVector.sub(B,A); } Ray(Ray r) { origin = new PVector(r.origin.x, r.origin.y, r.origin.z); direction = new PVector(r.direction.x, r.direction.y, r.direction.z); } void set(PVector A, PVector B) { origin = new PVector(A.x, A.y, A.z); direction = PVector.sub(B,A); } void setOrigin(float x, float y, float z) { origin = new PVector(x, y, z); } void setOrigin(PVector pos) { origin = new PVector(pos.x, pos.y, pos.z); } void setDirection(float x, float y, float z) { direction = new PVector(x, y, z); } void setDirection(PVector dir) { direction = new PVector(dir.x, dir.y, dir.z); } void lookAt (float x, float y, float z) { direction = new PVector(x-origin.x, y-origin.y, z-origin.z); } void draw2d() { line(origin.x, origin.y, origin.x+direction.x, origin.y+direction.y); ellipse (origin.x, origin.y, 2,2); } void draw3d() { line(origin.x, origin.y, origin.z, origin.x+direction.x, origin.y+direction.y, origin.z+direction.z); } } //---------------------------------------------------------- // spheres are defined by center point and radius //---------------------------------------------------------- class PSphere { PVector center; float radius; PSphere() { center = new PVector(222,222); radius = 200; } PSphere(PVector center, float radius) { this.center = center; this.radius = radius; } PSphere(PSphere sph) { this.center = sph.center; this.radius = sph.radius; } void set(PVector center, float radius) { this.center = center; this.radius = radius; } void setCenter(PVector center) { this.center = center; } void draw() { pushMatrix(); translate (center.x, center.y, center.z); sphere(radius); popMatrix(); } }