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.
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 | // REQUIRES: Drawable.pde /** * This class implements the basic layer, * but you still need to implement the draw() * and over() methods in subclasses. */ abstract class Movable extends Drawable { // very simple constructor Movable() { super(0,0); } // very simple constructor Movable(float x, float y) { super(x,y); } // coordinate getter overrides float getX() { return super.getX() + ox; } float getY() { return super.getY() + oy; } float getOX() { return ox; } float getOY() { return oy; } // still abstract abstract void draw(); // still abstract abstract boolean over(float x, float y); // internal variables for movement administration private float mx=-1, my=-1, ox=0, oy=0; // mark mouse location when pressed boolean mousePressed(float mouseX, float mouseY, int mouseButton) { mx=mouseX; my=mouseY; return true; } // update the offsets to use as long as the mouse is being dragged boolean mouseDragged(float mouseX, float mouseY, int mouseButton) { if(mx!=-1 && my!=-1) { ox = mouseX-mx; oy = mouseY-my; for(Point p: points) { p.setOffsets(ox,oy); } return true; } return false; } // commit offsets on mouse release boolean mouseReleased(float mouseX, float mouseY, int mouseButton) { for(Point p: points) { p.commitOffsets(); } mx=-1; my=-1; ox=0; oy=0; return true; } } |
Written and maintained by Mike "Pomax" Kamermans. All code snippets on this site are "do whatever you want with it" licensed.