Generating code from recorded mouse data

Program 1 records mouse data and then prints it to the console:

// TRACE AN IMAGE,
// PRINT OUT THE DATA POINTS
 
PImage kitty;
int count = 0;
int x[] = new int[1000];
int y[] = new int[1000];
 
void setup(){
  size(500,500);
 
  // YOU'LL NEED TO HAVE AN IMAGE CALLED
  // "kitten.jpg" IN YOUR SKETCH'S "data" FOLDER!
  kitty = loadImage("kitten.jpg");
}
 
//---------------------------------------
void draw(){
  background(0,0,0);
  int W = kitty.width;
  int H = kitty.height;
  image(kitty,0,0,W,H);
 
  noFill();
  strokeWeight(3);
  stroke(0,200,0);
  beginShape();
  for (int i=0; i<count; i++){
    vertex(x[i], y[i]);
  }
  endShape();
}
 
//---------------------------------------
// WHEN I CLICK, STORE THE MOUSE DATA
void mousePressed(){
  x[count] = mouseX;
  y[count] = mouseY;
  count++;
}
 
//---------------------------------------
// WHEN I HIT SPACEBAR, PRINT IT OUT
void keyPressed(){
  if (key == ' '){
 
    print("int[] x = {");
    for (int i=0; i<count; i++){
      print(x[i]);
      if (i<(count-1)){
        print(",");
      }
      if (i%10 == 0){
        print("\n");
      }
    }
    println("};");
 
    print("int[] y = {");
    for (int i=0; i<count; i++){
      print(y[i]);
      if (i<(count-1)){
        print(",");
      }
      if (i%10 == 0){
        print("\n");
      }
    }
    println("};");
 
  }
}

Program 2 the takes the code which was generated from the previous program, and draws it.

int[] x = {
  297,
  298,298,290,286,282,282,286,285,299,300,
  300,300,300,301,301,309,312,304,292,279,
  266,254,240,228,219,214,201,182,168,158,
  145,132,122,96,92,85,85,92,105,107,
  118,129,138,155,156,153,155,159,167,171,
  174,175,174,166,159,159,161,161,161,162,
  176,181,182,192,194,194,195,211,221,228,
  238,248,260,264,270,279,287,294};
 
int[] y = {
  21,
  40,62,83,97,114,127,157,163,202,212,
  227,246,259,285,295,310,319,328,338,336,
  327,326,328,328,332,334,338,339,327,323,
  319,314,308,286,277,258,250,236,249,260,
  268,271,277,278,263,239,227,210,190,177,
  152,139,119,106,91,82,56,40,28,19,
  13,21,23,29,37,43,47,47,40,40,
  43,43,40,30,23,20,16,11};
 
void setup(){
  size(500,500);
}
void draw(){
  background(70,70,100);
  fill(255,200,200);
  strokeWeight(3);
  stroke(0,0,0);
  beginShape();
  for (int i=0; i<x.length; i++){
    vertex(x[i], y[i]);
  }
  endShape(CLOSE);
}

Comments are closed.