Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.3/p5.js"></script>     

              
            
!

CSS

              
                body {margin:0px; padding:0px; overflow: hidden}
              
            
!

JS

              
                let mirror, ray;

function setup() {
    canvas = createCanvas(windowWidth, windowHeight);
    mirror = new Line();
    ray = new Line();
}

function reflect(d, n) {
  return d.copy().add(n.copy().mult(d.copy().mult(2).dot(n) / pow(n.mag(), 2) * -1));
}

function draw() {
    clear();
    background(235, 255, 160);
    const boundary = {x: -width / 2, y: -height / 2, w: width, h: height};
    
    const mx = cos(frameCount / 360 * PI + PI * 0.251);
    const my = 1;
    mirror.fromTwoPoints({x:0, y: 0}, {x: mx, y: my});

    const ty = sin(frameCount / 360 * PI) * height / 4;
    ray.fromTwoPoints({x: -width / 2, y:0}, {x: 0, y: ty});
    const rayVec = createVector(width / 2, ty).normalize();
    
    const intersection = mirror.getIntersectionPoint(ray);
    const normal = createVector(-my, mx);
    if (normal.dot(rayVec) > 0) { normal.mult(-1);}
    const reflectionVec = reflect(rayVec, normal);

    const rayHead = intersection.copy().add(rayVec.copy().mult(-32));
    const rayTail = intersection.copy().add(rayVec.copy().mult(-56));
    const normalHead = intersection.copy().add(normal.copy().mult(24));
    const reflectionHead = intersection.copy().add(reflectionVec.copy().mult(56));
    const reflectionTail = intersection.copy().add(reflectionVec.copy().mult(32));
    const reflectionLineEnd = intersection.copy().add(reflectionVec.copy().mult(width));

    push();

    fill(255); stroke(0); strokeWeight(1);
    translate(width / 2, height / 2);
    line(-width / 2, 0, intersection.x, intersection.y);
    line(intersection.x, intersection.y, reflectionLineEnd.x, reflectionLineEnd.y);

    strokeWeight(2);
    mirror.draw(boundary);
    drawArrow(intersection.x, intersection.y, normalHead.x, normalHead.y);
    strokeWeight(4);
    drawArrow(rayTail.x, rayTail.y, rayHead.x, rayHead.y);
    drawArrow(reflectionTail.x, reflectionTail.y, reflectionHead.x, reflectionHead.y);

    fill(0); noStroke(0);
    drawLabel(rayHead.x + rayVec.x * 16, rayHead.y + rayVec. y * 16, "i");
    drawLabel(reflectionHead.x + reflectionVec.x * 16, reflectionHead.y + reflectionVec.y * 16, "r");
    drawLabel(normalHead.x + normal.x * 16, normalHead.y + normal.y * 16, "n");

    pop();
}

class Line {
  constructor(a, b, c) {
    this.a = a;
    this.b = b;
    this.c = c;
  }

  fromTwoPoints(p0, p1) {
    let dx = p1.x - p0.x;
    let dy = p1.y - p0.y;
    this.a = dy;
    this.b = -dx;
    this.c = dx * p0.y - dy * p0.x;
    return this;
  }

  fromPointAndAngle(p0, angle) {
    let p1 = {x: p0.x + cos(angle), y: p0.y + sin(angle)};
    return this.fromTwoPoints(p0, p1);
  }

  fromPointAndVector(p0, v) {
    let p1 = {x: p0.x + v.x, y: p0.y + v.y};
    return this.fromTwoPoints(p0, p1);
  }

  intersects(o) {
    if (o instanceof Line) {
      let d = this.a * o.b - o.a * this.b;
      return d != 0.0;
    } else if (o instanceof LineSegment) {
      let t1 = this.a * o.p0.x + this.b * o.p0.y + this.c;
      let t2 = this.a * o.p1.x + this.b * o.p1.y + this.c;
      return t1 * t2 <= 0;
    }
    return undefined;
  }

  getIntersectionPoint(o) {
    if (o instanceof Line) {
      let d = this.a * o.b - o.a * this.b;
      if (d == 0.0) { return undefined; }
      let x = (this.b * o.c - o.b * this.c) / d;
      let y = (o.a * this.c - this.a * o.c) / d;
      return createVector(x, y);
    } else if (o instanceof LineSegment) {
      if (!this.intersects(o)) { return undefined; }
      return this.getIntersectionPoint(o.toLine());
    }
    return undefined;
  }

  getAngle() {
    return atan2(this.a, -this.b);
  }

  getPerpendicular(p) {
    return new Line(this.b, -this.a, this.a * p.y - this.b * p.x);
  }

  getParallel(p) {
    return new Line(this.a, this.b, -this.a * p.x - this.b * p.y);
  }

  getNearestPoint(p) {
    let l = this.getPerpendicular(p);
    return this.getIntersectionPoint(l);
  }

  draw(rect) {
    if (!rect) { rect = {x:0, y:0, w:0, h:0}}
    let l0, l1;
    if (abs(this.a) > abs(this.b)) {
      l0 = new Line().fromTwoPoints({x:rect.x, y:rect.y}, {x:rect.x + width, y:rect.y});
      l1 = new Line().fromTwoPoints({x:rect.x, y:rect.y + height}, {x:rect.x + width,   y:height});
    } else {
        l0 = new Line().fromTwoPoints({x:rect.x, y:rect.y}, {x:rect.x, y:height});
      l1 = new Line().fromTwoPoints({x:rect.x + width, y:rect.y}, {x:rect.x + width, y:rect.y + height});
    }

    let p0 = this.getIntersectionPoint(l0);
    let p1 = this.getIntersectionPoint(l1);
    line(p0.x, p0.y, p1.x, p1.y);
  }
}

function drawArrow(x0, y0, x1, y1) {
  line(x0, y0, x1, y1);
  let v = createVector(x1 - x0, y1 - y0).normalize();
  line(x1, y1, x1 - v.y * 4 - v.x * 4, y1 + v.x * 4 - v.y * 4);
  line(x1, y1, x1 + v.y * 4 - v.x * 4, y1 - v.x * 4 - v.y * 4);
}

function drawLabel(x, y, label, align = CENTER) {
  push();
  strokeWeight(0);
  textFont("monospace");
  textSize(14);
  textAlign(align);
  if (align == LEFT) {x += 6;}
  if (align == RIGHT) {x -= 6;}
  text(label, x, y);
  pop();
}

              
            
!
999px

Console