Advertisement
yclee126

Angular cross section (Processing 3)

Jul 3rd, 2022
1,265
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.24 KB | None
  1. // Display 360 angular cross section of an ellipse and a rectangle
  2. // Left click = ellipse
  3. // Right click = rectangle
  4.  
  5. // Code for Processing 3
  6.  
  7. PGraphics shapeGraphics;
  8. int shape = 0;
  9.  
  10. void setup(){
  11.   size(500, 500);
  12.   shapeGraphics = createGraphics(1, height);
  13. }
  14.  
  15. void draw(){
  16.   // draw each x slices
  17.   for(int i = 0; i < width; i ++){
  18.     shapeGraphics.beginDraw();
  19.    
  20.     shapeGraphics.background(0);
  21.     shapeGraphics.fill(128);
  22.     shapeGraphics.stroke(0, 0);
  23.     shapeGraphics.rectMode(RADIUS);
  24.    
  25.     // draw rotated shape
  26.     shapeGraphics.rotate(2*PI * (float)i/width);
  27.     drawShape(shapeGraphics, shape);
  28.    
  29.     shapeGraphics.endDraw();
  30.    
  31.     // paste drawn shape onto current context
  32.     copy(
  33.       shapeGraphics.get(),
  34.       0, 0, 1, height,
  35.       i, 0, 1, height
  36.     );  
  37.   }
  38.  
  39.   // draw shape for reference
  40.   fill(0, 0);
  41.   stroke(255);
  42.   rectMode(RADIUS);
  43.   drawShape(this.g, shape);
  44. }
  45.  
  46. void drawShape(PGraphics context, int shape){
  47.   if(shape == 0){
  48.     context.ellipse(0, 0, mouseX*2, mouseY*2);
  49.   } else {
  50.     context.rect(0, 0, mouseX, mouseY);
  51.   }
  52. }
  53.  
  54. void mousePressed() {
  55.   if (mouseButton == LEFT) {
  56.     shape = 0;
  57.   } else if (mouseButton == RIGHT) {
  58.     shape = 1;
  59.   }
  60. }
Advertisement
RAW Paste Data Copied
Advertisement