I don't want to keep writing the same code over and over.

I have written generic Point classes, line/line intersection algorithms and Layer administration code more times than I can remember. I don't like menial programming, so this is effectively my code library. But you can use it too!

The language used is Processing, which is a mostly-Java-syntax language. I hardly ever use Java-specific ninja code, so rewriting any of these snippets to your favourite language is less than a minute's worth of work, typically.

Drawable snippet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// REQUIRES: Point.pde

/**
 * An abstract Drawable thing. These
 * things must implement a draw() and
 * and over() method. Event handling is
 * implemented already in the sense that
 * it does nothing, and you can simply
 * override it in a subclass.
 */
abstract class Drawable {

  // all controlling points in this thing
  protected ArrayList<Point> points = new ArrayList<Point>();
  
  // simple constructor
  Drawable(float x, float y) { addPoint(new Point(x,y)); }
  
  // coordinate getters
  float getX() { return points.get(0).x; }
  float getY() { return points.get(0).y; }

  // coordinate setters
  void setX(float x) { points.get(0).x = x; }
  void setY(float y) { points.get(0).y = y; }

  // coordinate modifiers
  void addX(float x) { points.get(0).x += x; }
  void addY(float y) { points.get(0).y += y; }
 
  /**
   * subclasses must implement this
   * method so that they can be drawn.
   */
  abstract void draw();

  /**
   * draw all points in this thing
   */
  void drawPoints() { for(Point p: points) { p.draw(); }}
  
  /**
   * subclasses must implement this
   * method so that they can be manipulated.
   */
  abstract boolean over(float x, float y);
  
  /**
   * subclasses must implement this method
   * so that their controlling points can be
   * used by the sketch.
   */
  ArrayList<Point> getPoints() { return points; };

  /**
   * Add a point to this thing's list of controlling points
   */
  void addPoint(Point p) { points.add(p); }

  // event hanlding is implemented as "does nothing",
  // so subclasses can override it, but don't have to.

  boolean keyPressed(char key, int keyCode) { return false; }
  boolean keyReleased(char key, int keyCode){ return false; }
  boolean keyTyped(char key, int keyCode) { return false; }

  boolean mousePressed(float mouseX, float mouseY, int mouseButton) { return false; }
  boolean mouseReleased(float mouseX, float mouseY, int mouseButton) { return false; }
  boolean mouseDragged(float mouseX, float mouseY, int mouseButton) { return false; }
  boolean mouseClicked(float mouseX, float mouseY, int mouseButton) { return false; }
  boolean mouseMoved(float mouseX, float mouseY) { return false; }
}

Back to the main page

Download all


Written and maintained by Mike "Pomax" Kamermans. All code snippets on this site are "do whatever you want with it" licensed.