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 | // REQUIRES: skeleton.pde // REQUIRES: Movable.pde /** * Demonstrator class for the skeleton. * we're modelling a rectangular thing. */ class Demonstrator extends Movable { // width and height Point dims; // rectangular thing Demonstrator(float x, float y, float w, float h) { super(x,y); dims = new Point(w,h); } // draw rectangle! void draw() { fill(255,0,0,30); stroke(0); rect(getX(), getY(), dims.x, dims.y); drawPoints(); } // standard rectangular bounds check boolean over(float x, float y) { return getX() <= x && x <= getX() + dims.x && getY() <= y && y <= getY() + dims.y; } } /** * set up several demonstrator things */ void setupAdditional() { for(int i=0; i<20; i++) { Demonstrator d = new Demonstrator(getCoordinate(), getCoordinate(), getDimension(), getDimension()); addDrawable(d); } } // random coordinate generator float getCoordinate() { return random(40,width-80); } // random dimension generator float getDimension() { return random(40,80); } // No additional things to draw void drawAdditional() {} |
Written and maintained by Mike "Pomax" Kamermans. All code snippets on this site are "do whatever you want with it" licensed.