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.

Point 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
// a generic Point implementation
class Point
{
  // coordinates and offsets
  private float x=0, y=0, ox=0, oy=0;
  
  // this is a "we acknowledge the cursor is close enough" value.
  private float threshold = 3;
  
  // drawing points may or may not benefit from showing the label, too.
  boolean showCoordinateText = true;

  // completely generic constructor
  Point(float _x, float _y) { x=_x; y=_y; }
  
  float getX() { return x+ox; }
  float getY() { return y+oy; }

  // set the coordinate values
  void setOffsets(float _ox, float _oy) { ox=_ox; oy=_oy; }
 
  // commit offset
  void commitOffsets() { x+=ox; ox=0; y+=oy; oy=0; }

  /**
   * Determine whether the cursor is close enough
   * to this point to treat it as hovering over it.
   */ 
  boolean over(float _x, float _y) {
    return abs(x+ox-_x)<=threshold && abs(y+oy-_y)<=threshold;
  }

  /**
   * draw the point, as well as the coordinate as
   * text label, if requested based on showCoordinateText.
   */
  void draw() {
    stroke(0);
    fill(255);
    ellipse(getX(),getY(),3,3);
    if(showCoordinateText) {
      fill(0);
      text(int(getX())+"/"+int(getY()),getX()+5,getY()+5);
    }
  }
}

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.