Guest User

applyMatrix() edit

a guest
Sep 18th, 2019
527
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function draw() {
  2.   background(220);
  3.   fill(0);
  4.   text('X-axis rotation:', 20, 25);
  5.   text('Y-axis rotation:', 20, 55);
  6.   text(xRotation.value(), 250, 25);
  7.   text(yRotation.value(), 250, 55);
  8.   noFill();
  9.  
  10.  
  11.  
  12.   // Math
  13.   // https://math.stackexchange.com/a/3361015/599209
  14.  
  15.   const a = xRotation.value();
  16.   const b = yRotation.value();
  17.  
  18.   translate(width / 2, height / 2); // Set origin at center of canvas
  19.   scale(1, -1); // Make y axis point up instead of down
  20.   applyMatrix(cos(b), 0, -sin(a) * sin(b), cos(a), 0, 0); // Apply 2x2 transformation matrix, in column-major order
  21.   ellipse(0, 0, size, size);
  22.   ellipse(0.2*size, 0.2*size, 0.2*size, 0.2*size);
  23.   ellipse(-0.2*size, 0.2*size, 0.2*size, 0.2*size);
  24.   arc(0, 0.2*size, size, size, 1.25*PI, 1.75*PI);
  25.  
  26. }
  27.  
  28.  
  29. let xRotation;
  30. let yRotation;
  31. const size = 200;
  32. function setup() {
  33.   createCanvas(400, 400);
  34.  
  35.   // X range
  36.   xRotation = createSlider(0, 2*PI, 0, 0.01);
  37.   xRotation.position(110, 10);
  38.   xRotation.style('width', '120px');
  39.  
  40.   // Y range
  41.   yRotation = createSlider(0, 2*PI, 0, 0.01);
  42.   yRotation.position(110, 40);
  43.   yRotation.style('width', '120px');
  44.  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment