pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

philho private pastebin - collaborative debugging tool What's a private pastebin?


Posted by PhiLho on Sat 8 Nov 21:18 (modification of post by PhiLho view diff)
report abuse | download | new post

  1. int timer = 0;
  2.  
  3. // I like to use variables, make easier to change / adapt
  4. int centerX = 300;
  5. int centerY = 250;
  6.  
  7. int diskW = 250;
  8. int diskH = 50;
  9. float ratioV = 0.3;
  10. float offset = ratioV * diskH;
  11.  
  12. MovingShape ms;
  13.  
  14. void setup() {
  15.   size(600, 600);
  16.   smooth();
  17.  
  18. //~   ms = new MovingEllipse();
  19.   ms = new MovingIcon();
  20. }
  21.  
  22. void draw() {
  23.   ms.move();
  24.  
  25.   drawBackground();
  26.   if (ms.afterMiddleOfMove())
  27.   {
  28.     drawUpperDivider();
  29.     ms.draw();
  30.   }
  31.   else
  32.   {
  33.     ms.draw();
  34.     drawUpperDivider();
  35.   }
  36. }
  37.  
  38. void mousePressed() {
  39.   if (ms.isReady() && isMouseInDisk()) {
  40.     link("http://Processing.org");
  41.   }
  42. }
  43.  
  44. void mouseMoved() {
  45.   cursor(ARROW);
  46.   if (isMouseInDisk()) {
  47.     if (ms.isReady()) {
  48.       cursor(HAND);
  49.     } else {
  50.       ms.setGoing(true);
  51.     }
  52.   } else {
  53.     if (ms.isReady()) {
  54.       ms.setReady(false);
  55.       ms.setGoing(true);
  56.       ms.invertSpeed();
  57.     }
  58.   }
  59. }
  60.  
  61. void drawBackground() {
  62.   background(255);
  63.  
  64.   // Disk
  65.   fill(255);
  66.   ellipse(centerX, centerY, diskW, diskH);
  67.  
  68.   // Bottom divider
  69.   stroke(0);
  70.   line(centerX, centerY + 50, centerX, height);
  71.  
  72.   // Bottom
  73.   noFill();
  74.   arc(centerX, centerY, diskW, diskH * 2, 0, PI);
  75.  
  76.   // Big dot
  77.   fill(0);
  78.   ellipse(centerX, centerY + offset, 50, 15);
  79. }
  80.  
  81. void drawUpperDivider() {
  82.   // Divider
  83.   stroke(0);
  84.   line(centerX, centerY - offset, centerX, 0);
  85.  
  86.   // Little dot
  87.   fill(0);
  88.   ellipse(centerX, centerY - offset, 10, 10);
  89. }
  90.  
  91. boolean isMouseInDisk() {
  92.   return isPointInEllipse(mouseX, mouseY,
  93.       centerX, centerY, diskW, diskH);
  94. }
  95.  
  96. boolean isPointInEllipse(int x, int y,
  97.     int ellipseX, int ellipseY, int ellipseW, int ellipseH) {
  98.   // Compute position of focal points
  99.   float c = sqrt(ellipseW * ellipseW - ellipseH * ellipseH) / 2.0;
  100.   float f1x = ellipseX - c;
  101.   float f2x = ellipseX + c;
  102.   // Compute distances from focal points to given point
  103.   float d1 = dist(x, y, f1x, ellipseY);
  104.   float d2 = dist(x, y, f2x, ellipseY);
  105.   // Distance from focal points to point on ellipse is constant, equal to width
  106.   // (twice the distance between foci)
  107.   return d1 + d2 <= ellipseW;
  108. }
  109.  
  110. abstract class MovingShape
  111. {
  112.   // Constants to be set in sub-class
  113.   float SHAPE_WIDTH_MIN;
  114.   float SHAPE_WIDTH_MAX;
  115.   float SHAPE_HEIGHT_MIN;
  116.   float SHAPE_HEIGHT_MAX;
  117.  
  118.   float shapeX = centerX;
  119.   float shapeY = centerY - offset;
  120.  
  121.   float shapeWidth = SHAPE_WIDTH_MIN;
  122.   float shapeHeight = SHAPE_HEIGHT_MIN;
  123.  
  124.   float pos = -HALF_PI;
  125.   float speed = 0.07;
  126.  
  127.   boolean ready;
  128.   boolean going;
  129.  
  130.   MovingShape()
  131.   {
  132.   }
  133.  
  134.   void move()
  135.   {
  136.     if (going) {
  137.       pos += speed;
  138.       shapeX = centerX - 0.4 * diskW * cos(pos);
  139.       shapeY = centerY + offset * sin(pos);
  140.       if (pos >= HALF_PI) {
  141.         going = false;
  142.         ready = true;
  143.         timer = millis();
  144.       }
  145.       if (pos <= -HALF_PI) {
  146.         going = false;
  147.         speed = -speed;
  148.       }
  149.       recomputeShapeSize();
  150.     }
  151.  
  152.     if (ready) { // Wait for user input
  153.       if (millis() - timer > 2000 && !isMouseInDisk()) {
  154.         ready = false;
  155.         speed = -speed;
  156.         going = true;
  157.       }
  158.     }
  159.   }
  160.  
  161.   void recomputeShapeSize()
  162.   {
  163.     float amount = (HALF_PI + pos) / PI;
  164.     shapeWidth = lerp(SHAPE_WIDTH_MIN, SHAPE_WIDTH_MAX, amount);
  165.     shapeHeight = lerp(SHAPE_HEIGHT_MIN, SHAPE_HEIGHT_MAX, amount);
  166.   }
  167.  
  168.   // To be overridden
  169.   void draw()
  170.   {
  171.   }
  172.  
  173.   void invertSpeed()
  174.   {
  175.     speed = -speed;
  176.   }
  177.  
  178.   boolean isReady()
  179.   {
  180.     return ready;
  181.   }
  182.   void setReady(boolean r)
  183.   {
  184.     ready = r;
  185.   }
  186.  
  187.   boolean isGoing()
  188.   {
  189.     return going;
  190.   }
  191.   void setGoing(boolean g)
  192.   {
  193.     going = g;
  194.   }
  195.  
  196.   boolean afterMiddleOfMove()
  197.   {
  198.     return pos > 0;
  199.   }
  200. }
  201.  
  202. class MovingEllipse extends MovingShape
  203. {
  204.   // Constants
  205.   static final color SHAPE_COLOR = #7ADE21;
  206.   {
  207.     SHAPE_WIDTH_MIN = 10;
  208.     SHAPE_WIDTH_MAX = 25;
  209.     SHAPE_HEIGHT_MIN = 3;
  210.     SHAPE_HEIGHT_MAX = 6;
  211.   }
  212.  
  213.   MovingEllipse()
  214.   {
  215.     super();
  216.   }
  217.  
  218.   void draw()
  219.   {
  220.     // Moving circle shape size and colour details
  221.     stroke(0);
  222.     fill(SHAPE_COLOR);
  223.     ellipse(shapeX, shapeY, shapeWidth, shapeHeight);
  224.   }
  225. }
  226.  
  227. class MovingIcon extends MovingShape
  228. {
  229.   // Constants
  230.   {
  231.     SHAPE_WIDTH_MIN = 7;
  232.     SHAPE_WIDTH_MAX = 72;
  233.     SHAPE_HEIGHT_MIN = 7;
  234.     SHAPE_HEIGHT_MAX = 71;
  235.   }
  236.  
  237.   PImage icon;
  238.  
  239.   MovingIcon()
  240.   {
  241.     super();
  242.  
  243.     icon = loadImage("icon.png");
  244.   }
  245.  
  246.   void draw()
  247.   {
  248.     image(icon,
  249.         shapeX - shapeWidth/2, shapeY - shapeHeight/2,
  250.         shapeWidth, shapeHeight);
  251.   }
  252. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post